加载表单时如何将焦点放在文本框上?
我的 C# 程序中有一个 TextBox
。当程序启动时,我需要将注意力集中在这个 TextBox
上。
我在 Form_Load 上尝试过这个:
MyTextBox.Focus();
但它不起作用。
当表单加载时,如何将焦点放在此上?
I have a TextBox
in my C# program. I need focus to be on this TextBox
when the program starts.
I tried this on Form_Load:
MyTextBox.Focus();
but it doesn't work.
How do I put focus on this when the form loads?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(17)
设置表单的
ActiveControl
属性,应该没问题。Set the
ActiveControl
property of the form and you should be fine.如果控件尚未呈现,则无法将焦点设置到该控件。 Form.Load() 在呈现控件之前发生。
转到表单的事件并双击“已显示”事件。在表单的显示事件处理程序中调用 control.Focus() 方法。
You cannot set focus to a control if it has not been rendered. Form.Load() occurs before the controls are rendered.
Go to the form's events and double click the "Shown" event. In the form's shown event handler call the control.Focus() method.
检查您的 Tab 键顺序并确保文本框设置为零
check your tab order and make sure the textbox is set to zero
您可以尝试:
MyTextBox.Select();
根据文档:
您可以首先通过检查 MyTextBox.CanSelect 属性。
You could try:
MyTextBox.Select();
According to the documentation:
You can first check if the control can be selectable by inspecting the MyTextBox.CanSelect property.
如果您只想在第一次显示表单时设置焦点,请尝试处理 Form.Shown 事件并在那里执行。否则请使用 Control.VisibleChanged。
If you only want to set the focus the first time the form is shown, try handling the Form.Shown event and doing it there. Otherwise use Control.VisibleChanged.
您无法使其工作的原因是因为在绘制或呈现表单之前调用了
Load
事件。这就像告诉披萨店如何制作披萨,然后要求他们在制作披萨之前向您发送一张照片,显示披萨上有多少意大利辣香肠。
The reason you can't get it to work is because the
Load
event is called before the form is drawn or rendered.It like telling a pizza place how to make your pizza, and then asking them to send you a picture of how much pepperoni is on your pizza before they made it.
Textbox.Focus()
“尝试”将焦点设置在文本框元素上。例如,如果元素可见性被隐藏,Focus()
将不起作用。因此,在调用Focus()
之前,请确保您的元素可见。Textbox.Focus()
"Tries" to set focus on the textbox element. In case of the element visibility is hidden for example,Focus()
will not work. So make sure that your element is visible before callingFocus()
.我通过更改 TextBox 的“TabIndex”属性解决了问题。我为 TextBox 设置了 0,我想在程序启动时将其聚焦在 Form 上。
I solved my problem with changing "TabIndex" property of TextBox. I set 0 for TextBox that I want to focus it on Form when the program start.
使用表单显示的事件并设置
use form shown event and set
设置 Tab Index 属性的值 = 0,然后在表单加载函数中写入:
它将起作用。
Set Tab Index property's value = 0 and then in form load function write :
It will work.
最后我发现我使用metro框架的问题,你所有的解决方案都不适用于metroTextBox,而你所有的解决方案都将适用于普通的textBox
在 load 、 show 、visibility_change 、events 中,即使选项卡索引 = 0 也是有效的。
Finally i found the problem i was using metro framework and all your solutions will not work with metroTextBox, and all your solutions will work with normal textBox
in load , show , visibility_change ,events, even the tab index = 0 is valid.
您可以在文本框设置中使用
textBox1.select();
或 TabIndex。TabIndex=0
首先聚焦。You can use either
textBox1.select();
or the TabIndex in textbox setting.TabIndex=0
focoused first.将 Tabstop 设置为 True,并将 TabIndex 设置为您需要关注的控件的最小值。
例如,如果您有 2 个 TextBox:TextBox1 和 TextBox2,请将两者的 Tabstop 设置为 True,并将 TabIndex 分别设置为 0 和 1。加载表单时,焦点将位于 TextBox1 上,按“Tab”键时,焦点将移至 TextBox2 上。
Set Tabstop to True and TabIndex to the minimum to the control on which you need focus.
e.g. If you have 2 TextBoxes : TextBox1 and TextBox2, set Tabstop to True for both and TabIndex to 0 and 1 respectively. When the form loads, the focus will be on TextBox1 and on the press of 'Tab' key, the focus will move to TextBox2.
这对我有用
将选项卡索引设置为 0
this.yourtextbox.TabIndex = 0;
it's worked for me
set tabindex to 0
this.yourtextbox.TabIndex = 0;
这是加载 C# 表单时将文本框置于焦点的一种简单方法。它与上面的类似,但编码较少。我不记得我第一次找到解决方案的地方,但我已经使用它几年了,这要归功于他们。
This is one simple way to put the textbox into focus when loading a C# form. It's similar to the above, but with a bit less coding. I can't recall where I first found the solution, but I've used it for a few years, and credits go to them.
在您的表单上,转到属性并确保“TopMost”属性设置为 true,
这会解决你的问题。
on your form, go to properties and make sure that "TopMost" property is set to true,
that will solve your problem.
在 jquery 设置焦点
或在 Javascript 中你可以这样做
In jquery set focus
or in Javascript you can do