将焦点设置到文本框控件 [英] Setting focus to a textbox control

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

问题描述

如果我想在第一次打开表单时将焦点设置在文本框上,那么在设计时,我可以将它的 tabOrder 属性设置为 0,并确保没有其他表单控件的 tabOrder 为 0.

If I want to set the focus on a textbox when the form is first opened, then at design time, I can set it's tabOrder property to 0 and make sure no other form control has a tabOrder of 0.

如果我想在运行时使用代码实现相同的结果,我应该如何进行?
是否有使用 tabOrder 的替代方法?
我假设任何运行时代码都将在表单的构造函数或其 onload 事件处理程序中?

If I want to achieve the same result at run-time, using code, how should I proceed?
Are there alternatives to using tabOrder?
I assume any run-time code will be in the form's constructor or its onload event handler?

编辑
换句话说,我希望能够在表单出现后立即直接在文本框中键入内容,而无需手动使用 Tab 键或手动选择它.

EDIT
In other words I'd like to be able to type straight into the textbox as soon as the form appears without having to manually tab to it, or manually select it.

推荐答案

因为要在表单加载时进行设置,所以必须先 .Show() 表单,然后才能调用 .Focus() 方法.除非您显示表单,否则表单无法在 Load 事件中获得焦点

Because you want to set it when the form loads, you have to first .Show() the form before you can call the .Focus() method. The form cannot take focus in the Load event until you show the form

Private Sub RibbonForm1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    Me.Show()
    TextBox1.Select()
End Sub

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

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