title: C# winforms - find which control took focus on LostFocus event
body: <p>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. </p>
  
  <p>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.</p>
  
  <p>I'm also open to any suggestions on a better way to implement an onscreen keypad.</p>
  
  <pre><code>    /// &lt;summary&gt;
      /// Store current control that has focus.
      /// This object will be used by the keypad to determin which textbox to update.
      /// &lt;/summary&gt;
      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;
      }
  
      /// &lt;summary&gt;
      /// Append button's text which represent a number ranging between 0 and 9
      /// &lt;/summary&gt;
      private void buttonKeypad_Click(object sender, EventArgs e)
      {
          if (this.currentControlWithFocus != null)
          {
              this.currentControlWithFocus.Text += ((Button)sender).Text;
              this.currentControlWithFocus.Focus();
          }
      }
  
      /// &lt;summary&gt;
      /// Removes last char from a textbox
      /// &lt;/summary&gt;
      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 &gt; 0)
              {
                  text = text.Remove(text.Length - 1);
                  this.currentControlWithFocus.Text = text;
              }
              this.currentControlWithFocus.Focus();
          }
      }
  </code></pre>
  
  <p>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</p>
  
  <p>This is what I like todo if I could determine which Control took focus away from an input box.</p>
  
  <pre><code>    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;
      }
  </code></pre>