1

Not sure how to select a textbox from multiple textboxes on a form and clear it on the click event of a button. For example, if we have multiple operand fields for a calculator and need to implement clear current field button, how do I implement it? Here is the code snippet I have so far.

private void button2_Click(object sender, EventArgs e) 
{ 
   foreach (Control t in this.Controls) 
   { 
      if (t is TextBox) 
      { 
         if (t.Focused) 
         { 
            t.Text = ""; 
         } 
      } 
   } 
}
  • When the button clicked focus will be moved out `TextBox` so this code will not work, you have to find the control by either `name` or `tag` – Hari Prasad Apr 16 '16 at 05:18
  • It's actually a very simple program. Just put two textboxes on a form and a button. Now try to get hold of the active textbox on the click of a button. If I knew beforehand which one I want, then it's easy. If I don't know, not sure which event to catch, mouse leave, focus, or something else. – Saurabh Gupta Apr 16 '16 at 05:20
  • @HariPrasad, if I knew the name, then it's easy. The problem is I don't know which one is active, means in which I had focus or which one was active before moving away from it and clicking the button. Not sure about how tag works. – Saurabh Gupta Apr 16 '16 at 05:23
  • @AlexJolig Apologies, added code to the main post. – Saurabh Gupta Apr 16 '16 at 05:24
  • 3
    See if this is what you are looking for http://stackoverflow.com/questions/4428100/find-out-the-control-with-last-focus – Mark Hall Apr 16 '16 at 05:24
  • The link which @MarkHall provided is the solution you need. – CST RAIZE Apr 16 '16 at 05:29
  • Nope none of the solution mentioned on the link are working. :( – Saurabh Gupta Apr 16 '16 at 05:42

2 Answers2

2

One option could be subscribing to TextBox LostFocus event.

Declare a class field to hold the reference of active TextBox.

private TextBox activeTextbox;

in Form_Load event subscribe to TextBox LostFocus event.

textbox1.LostFocus += (se,ev) => activeTextbox = textbox1;
textbox2.LostFocus += (se,ev) => activeTextbox = textbox2;

Now in button click event

private void button2_Click(object sender, EventArgs e) 
{
     if(activeTextbox != null)
     {
          activeTextbox.Text = "";    
     }

}
Hari Prasad
  • 16,716
  • 4
  • 21
  • 35
  • its better to use `GotFocus` or `enter` events instead of `GotFocus` event. because when a button is `acceptButton` of form, user can click and fire click event by pressing enter button even when another textBox is active. – mehrdad safa Apr 16 '16 at 07:12
0

As Hari Prasad mentioned, the active TextBox will lose the focus as soon as user clicks on the button. So I suggest using Leave event and an instance to detect which TextBox has been active before the button has been clicked.

private TextBox _tmpTextbox;

private void txt1_Leave(object sender, EventArgs e)
{
   _tmpTextbox = txt1;
}

private void txt2_Leave(object sender, EventArgs e)
{
  _tmpTextbox = txt2;
}

And for the button:

private void btnTest_Click(object sender, EventArgs e)
{
  _tmpTextbox.Text = "";
}

In order to shortened the code, you can use one even handler for all the textboxes:

private void txt1_Leave(object sender, EventArgs e)
{
  TextBox activeTextBox = (TextBox) sender;
  _tmpTextbox = activeTextBox;
}
Ghasem
  • 14,455
  • 21
  • 138
  • 171