文本框焦点检查 [英] textbox focus check

查看:26
本文介绍了文本框焦点检查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有 3 个文本框和按钮的 win 应用程序表单作为拨号盘(它是一个触摸屏应用程序).​​..

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

按下拨号盘按钮时,我想检查这 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.

类似于:

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

但这不起作用...它一直将文本附加到 tbx3.有什么建议吗?

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

谢谢:)

推荐答案

当您单击按钮时出现问题,该按钮将获得焦点,而不是您的任何文本框.

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

您可以做的是订阅LostFocus 事件并记住哪个文本框最后获得焦点.

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

类似于:

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

这篇关于文本框焦点检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆