简体   繁体   English

文本框焦点检查

[英]textbox focus check

I have a win app form with 3 text boxes and buttons as dial pad (it's a touchscreen app)... 我有一个win应用程序表单,有3个文本框和按钮作为拨号盘(它是一个触摸屏应用程序).​​..

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. 当按下拨号盘按钮时,我想检查这3个文本框中的哪一个具有焦点,并向其附加文本。

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. 但是这不起作用......它会一直将文本附加到tbx3。 Any suggestions? 有什么建议么?

Thanks :) 谢谢 :)

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. 您可以做的是订阅LostFocus事件并记住焦点最后的文本框。

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM