我有一个简单的情况(.NET2):上的UserControl1(或Form1中)一个texbox1
我要unfocus(假)的texbox当我点击的用户控件/形式(重点放在用户控件/形式代替):
我下面的UC /形式:
受保护的覆盖子OnMouseClick _
(BYVAL E上System.Windows.Forms.MouseEventArgs)
MyBase.OnMouseClick(五)
Me.Focus()
结束小组
为什么它不能在儿童工作文本框,但工作得非常好于非独生子女一(重点TextBox2中,然后单击面板上除去从焦点在TextBox2中)?
解决方案 添加一个新的面板控件到窗体(某处的方式),并调整到0,0 请不要设置可见=假此面板上。
在你的表单中添加一个标准的MouseClick事件处理程序如下:
私人无效Form1_MouseClick(对象发件人,发送MouseEventArgs E)
{
//取消注释如果滚动控制
//点scrollPos =新的点(this.AutoScrollPosition.X,-this.AutoScrollPosition.Y);
newPanel.Select();
//取消注释如果滚动控制
//this.AutoScrollPosition = scrollPos;
}
现在,当你点击你的主窗体的任何地方,任何输入控件会失去焦点,您将能够处理常用的验证活动等。
I have a simple situation (.NET2): a texbox1 on a UserControl1(or Form1).
I want to unfocus(leave) the texbox when I click on the usercontrol/form(focus the usercontrol/form instead):
I do the following on the UC/form:
Protected Overrides Sub OnMouseClick _
(ByVal e As System.Windows.Forms.MouseEventArgs)
MyBase.OnMouseClick(e)
Me.Focus()
End Sub
Why does it not work on the child textbox, but works very well on the non-child one(focus on textBox2 then click on the panel removes the focus from the textBox2)?
In your form add a standard MouseClick event handler as follows:
private void Form1_MouseClick(object sender, MouseEventArgs e)
{
// Uncomment if in scrollable control
//Point scrollPos = new Point(this.AutoScrollPosition.X, -this.AutoScrollPosition.Y);
newPanel.Select();
// Uncomment if in scrollable control
//this.AutoScrollPosition = scrollPos;
}
Now, when you click anywhere on your main form, any input control will lose focus and you will be able to handle the usual Validating events etc.