Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

textbox focus check

Tags:

c#

winforms

I have a win app form with 3 text boxes and buttons as dial pad (it's a touchscreen app)...

When a dial pad button is pressed I want to check which one of these 3 text boxes has focus, and append text to it.

Something like:

if (tbx1.Focused == true)
{
   tbx1.Text += "0";
}
else if (tbx2.Focused == true)
{
   tbx2.Text += "0";
}
else
{
   tbx3.Text += "0";
}

But this doesn't work... It appends text to tbx3 all the time. Any suggestions?

Thanks :)

like image 271
Alex Avatar asked Aug 17 '12 11:08

Alex


People also ask

How to check TextBox focus in c#?

Use setFocus() method of textbox to know the textbox is focused or not.

What is TextBox focus () in C#?

When showing a form that contains a TextBox, it's common courtesy to focus the TextBox so that the user can begin typing immediately. To focus a TextBox when a Windows Form first loads, simply set the TabIndex for the TextBox to zero (or the lowest TabIndex for any Control on the Form).

How do you check TextBox is focused or not in jquery?

$("#textbox"). focus(function () { alert("TextBox is Focused. The Div will Fade Out Now"); $('#div'). fadeOut(2000); });


1 Answers

The problem arises when you click the button, the button will gain focus and not any of your textboxes.

What you can do is subscribe to the LostFocus event and remember what textbox had the focus last.

Something like:

private TextBox lastFocused;
private void load(object sender, EventArgs e){
    foreach (TextBox box in new TextBox[] { txtBox1, txtBox2, txtBox3 }){
        box.LostFocus += textBoxFocusLost;
    }
}

private void textBoxFocusLost(object sender, EventArgs e){
    lastFocused = (TextBox)sender;
}
like image 152
Patrick Avatar answered Oct 18 '22 21:10

Patrick