如何聚焦文本框 VB.Net
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18633854/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Focus a TextBox VB.Net
提问by bharathivkmani
I have a text box in my form. When clicking the tray icon the form is shown and on form load I have written the code to focus on the text box. But it's not pointing the text box. Let I have 2 forms formB and formC I am calling formC from formB so that I doesn't getting the focus. how to give focus to the second form? I have find the solution to this problem . we have to overwrite the show method. Let's say formA and formB. Then FormB is in focus now.
我的表单中有一个文本框。单击托盘图标时,将显示表单,在加载表单时,我编写了代码以专注于文本框。但它没有指向文本框。让我有 2 个表单 formB 和 formC 我从 formB 调用 formC 这样我就没有得到焦点。如何关注第二种形式?我找到了这个问题的解决方案。我们必须覆盖 show 方法。假设 formA 和 formB。那么 FormB 现在是焦点。
Private Sub FormB_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Timer1.Enabled = True
Me.Timer1.Interval = 2000
End Sub`
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Timer1.Enabled = False
Dim frmquickListPHD1 As New FormC()
frmquickListPHD1.Show(CallingForm, CallingControl, Me)
End Sub
Overloads Sub Show(ByVal f1 As Form, ByVal c As Control, ByVal f2 As Form)
callingform = f1
MenuForm = f2
callingcontrol = c
Show()
End Sub
Private Sub FormC_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.TextBox1.Focus()
End Sub`
回答by Matt Wilko
The issue may be that the form does not have focus when you open it from the system tray?
问题可能是当您从系统托盘打开表单时,表单没有焦点?
Try something like:
尝试类似:
frmTest.Show
frmTest.Select
The in your Form_Activatedevent of frmTest
在你的Form_Activated事件frmTest
textbox1.Select
回答by sk2185
Set TabIndex =0
放 TabIndex =0
or
或者
after all loaded in form load
to set Textbox1.focus()
在所有加载到表单加载后设置 Textbox1.focus()
回答by 5uperdan
use the ActiveControl property of your form in the load event of the form:
在窗体的加载事件中使用窗体的 ActiveControl 属性:
Me.ActiveControl = Textbox1
Alternatively, if you're showing an instance of FormB from FormA on some event then you could use the following code in FormA:
或者,如果您在某个事件上显示来自 FormA 的 FormB 实例,那么您可以在 FormA 中使用以下代码:
Dim FormB1 as new FormB
FormB1.show()
FormB1.ActiveControl = FormB1.Textbox1
Don't use focus, from msdn (http://msdn.microsoft.com/en-us/library/system.windows.forms.control.focus.aspx):
不要使用来自 msdn ( http://msdn.microsoft.com/en-us/library/system.windows.forms.control.focus.aspx) 的焦点:
Focus is a low-level method intended primarily for custom control authors. Instead, application programmers should use the Select method or the ActiveControl property for child controls, or the Activate method for forms.
Focus 是一种低级方法,主要供自定义控件作者使用。相反,应用程序程序员应该对子控件使用 Select 方法或 ActiveControl 属性,或者对窗体使用 Activate 方法。

