alexb
alexb

Reputation: 11

C# winforms - find which control took focus on LostFocus event

I'm developing an application which will run on standard PCs and PCs with a touch screen. The application has number of input boxes for numeric values. Hence I added a numeric keypad to the GIU.

I use the code below link the keypad to a selected textbox, which works relatively well. However the application has several tabbed sections and I would like to set this.currentControlWithFocus to null if a focus been taken by any other control that does no belong to either keypad or set of input boxes for numeric values. This will help to avoid an incidental keypad press, which will result in an update of the last numeric input boxe referenced by currentControlWithFocus.

I'm also open to any suggestions on a better way to implement an onscreen keypad.

    /// <summary>
    /// Store current control that has focus.
    /// This object will be used by the keypad to determin which textbox to update.
    /// </summary>
    private Control currentControlWithFocus = null;

    private void EventHandler_GotFocus(object sender, EventArgs e)
    {
        ((TextBox)sender).BackColor = Color.Yellow;
        this.currentControlWithFocus = (Control)sender;
    }

    private void EventHandler_LostFocus(object sender, EventArgs e)
    {
        ((TextBox)sender).BackColor = Color.White;
    }

    /// <summary>
    /// Append button's text which represent a number ranging between 0 and 9
    /// </summary>
    private void buttonKeypad_Click(object sender, EventArgs e)
    {
        if (this.currentControlWithFocus != null)
        {
            this.currentControlWithFocus.Text += ((Button)sender).Text;
            this.currentControlWithFocus.Focus();
        }
    }

    /// <summary>
    /// Removes last char from a textbox
    /// </summary>
    private void buttonKeypad_bckspc_Click(object sender, EventArgs e)
    {
        if (this.currentControlWithFocus != null)
        {
            string text = this.currentControlWithFocus.Text;
            // remove last char if the text is not empty
            if (text.Length > 0)
            {
                text = text.Remove(text.Length - 1);
                this.currentControlWithFocus.Text = text;
            }
            this.currentControlWithFocus.Focus();
        }
    }

EventHandler_LostFocus and EventHandler_GotFocus are added to about 20 or so input boxes. buttonKeypad_Click is added to 10 buttons representing digits from 0 to 9 and buttonKeypad_bckspc_Click is added to a backspace button

This is what I like todo if I could determine which Control took focus away from an input box.

    private void EventHandler_LostFocus(object sender, EventArgs e)
    {
        // IF NEW CONTROL WITH FOCUS IS NOT ONE OF KEYPAD BUTTONS
        //   THEN 
              ((TextBox)sender).BackColor = Color.White;
              this.currentControlWithFocus = null;
    }

Upvotes: 1

Views: 4807

Answers (1)

JSJ
JSJ

Reputation: 5691

other then playing around the button and textboxes you can simply get the focused conttrol and then perform your conditionsl

to find the Focused control try below code.

public static Control FindFocusedControl(Control control)
{
    var container = control as ContainerControl;
    while (container != null)
    {
        control = container.ActiveControl;
        container = control as ContainerControl;
    }
    return control;
}

Upvotes: 2

Related Questions