如何在表单加载时将光标聚焦在文本框中 [英] How to focus cursor in a textbox when form load
本文介绍了如何在表单加载时将光标聚焦在文本框中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我希望当我的窗体在名为ItemCodetextBox的文本框上默认加载光标fpcus时。
我使用了Tab控件,文本框位于选项卡控件上。
我已在表单加载事件中找到以下代码。但它不起作用。
ItemCodetextBox.Focus();
请帮助我。
I want when my windows form load cursor by default fpcus on a textbox named "ItemCodetextBox".
I have used tab Control and the textbox is on the tab control.
I have witten the following code on the form load event. But it does not work.
ItemCodetextBox.Focus();
Please help me.
推荐答案
你不能直接将控件集中在表单加载上(顺便说一下,这是一个假事件,你不必使用它;它只是为了提供与真实事件相同的添加处理程序的机制而添加它,使用它相当于在表单构造函数的末尾添加代码。)这是因为只能关注一个可见的控件。
你可以在表单显示时关注它第一次。如果在load事件处理程序或构造函数中编写,它看起来像
You cannot directly focus a control on form load (by the way, this is a fake event, you don't have to use it; it is added only to provide the same mechanism of adding handlers as for "real" events", using it is equivalent to adding code at the end of a form constructor). This is because only a visible control can be focused.
You can focus it when a form is shown for the first time. If written in the load event handler or a constructor, it would look like
this.FormShown += (sender, eventArgs) => { myControl.Focus(); }
或者,您可以设置逻辑焦点,这可以提前完成,与真实焦点相反:
Alternatively, you can set "logical focus", which can be done in advance, in contrast to "real" focus:
this.ActiveControl = myControl;
询问Google [ ^ ]
试试这个
Try this
private void Form1_Load(object sender, EventArgs e)
{
ItemCodetextBox.TabIndex = 0;
ItemCodetextBox.Focus();
}
这篇关于如何在表单加载时将光标聚焦在文本框中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文