我开发了一个简单的计算器,比如 Windows 计算器,
但与 Windows 计算器不同的是,单击任何按钮后,该按钮上的焦点仍然存在于特定单击的按钮上。
那么如何永远不要关注计算器表单上的所有按钮......?
我不认为在每个按钮的单击事件上编写松散的焦点代码会更好......所以有更好的解决方案吗?
我开发了一个简单的计算器,比如 Windows 计算器,
但与 Windows 计算器不同的是,单击任何按钮后,该按钮上的焦点仍然存在于特定单击的按钮上。
那么如何永远不要关注计算器表单上的所有按钮......?
我不认为在每个按钮的单击事件上编写松散的焦点代码会更好......所以有更好的解决方案吗?
使用从标准按钮派生的NoFocusButton类的实例,而不是标准按钮。在此类中覆盖ShowFocusCues属性并始终返回 false。
Form f = new Form();
// Need to add manually the buttons to your form unless you build a customcontrol
NoFocusButton b = new NoFocusButton();
b.Text = "ClickMe";
f.Controls.Add(b);
f.Show();
// Class derived by the Button control, it is identical but the
// property that control the drawing of the Focus rectangle returns FALSE
// tricking the WinForm system to avoid to draw the focus rectangle
class NoFocusButton : System.Windows.Forms.Button
{
protected override bool ShowFocusCues
{
get
{
return false;
}
}
}
在没有看到您的任何代码的情况下,我将假设您有一个显示用户按下的数字的文本框,因此您需要在用户单击按钮后将焦点设置到文本框,如下所示:
TextBox1.Focus()
注意:如果您的文本框未命名TextBox1
,则将名称更改为您的文本框实际命名的名称。