vb.net 将焦点设置为 Form After Me.Show

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49314394/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 20:23:31  来源:igfitidea点击:

Set Focus to Form After Me.Show

vb.netvisual-studio-2015focus

提问by Timothy

I've come across a peculiar focusing issue. I have created the following "search" program:

我遇到了一个奇怪的聚焦问题。我创建了以下“搜索”程序:

  1. It runs in the background.
  2. When you double-tap the Ctrlkey it becomes visible.
  3. You can type in the textbox because the form has focus.
  4. If the form loses focus (I click on my desktop, for example), it disappears after 3 seconds.
  5. I double-tap the Ctrlkey again, and again it becomes visible.
  6. But this time, no matter what I try, the form is not focused and I cannot type in the textbox without first manually clicking on the form.
  1. 它在后台运行。
  2. 当您双击该Ctrl键时,它变得可见。
  3. 您可以在文本框中键入,因为表单具有焦点。
  4. 如果表单失去焦点(例如,我单击我的桌面),它会在 3 秒后消失。
  5. Ctrl再次双击该键,它再次变得可见。
  6. 但这一次,无论我尝试什么,表单都没有获得焦点,如果不先手动单击表单,我就无法在文本框中键入内容。

What's particularly interesting is that when I run this program in debug mode from Visual Studios, the program regains focus upon double-tapping Ctrlkey and becoming visible, and I can immediately start typing in the text box. However, when I build this program and run it alone, the program appears but does not regain focus upon double-tapping Ctrlkey, and therefore I cannot type in the text box until I manually click the form.

特别有趣的是,当我从 Visual Studios 以调试模式运行该程序时,该程序重新获得焦点,双击Ctrl键并变为可见,我可以立即开始在文本框中键入。但是,当我构建这个程序并单独运行它时,该程序出现但不会重新获得双击Ctrl键的焦点,因此我无法在文本框中输入,直到我手动单击表单。

After Me.Show() I have tried:

在 Me.Show() 之后,我尝试过:

  • Me.Focus()
  • Me.Validate()
  • Me.Select()
  • Textbox1.Select()
  • Textbox1.Focus()
  • 我.焦点()
  • 我.验证()
  • 我.Select()
  • Textbox1.Select()
  • Textbox1.Focus()

The form is topmost and normally running in administrator, but the same problem arises regardless.

该表单是最顶层的,通常在管理员中运行,但无论如何都会出现同样的问题。

The issue can be recreated in a more simple manner. Create a form with

可以以更简单的方式重新创建该问题。创建一个表单

  • Button ("Button1")
  • TextBox
  • Two timers ("hideForm", "showForm") both with intervals of 1000
  • 按钮(“按钮 1”)
  • 文本框
  • 两个计时器(“hideForm”、“showForm”),间隔均为 1000

Code:

代码:

Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    hideForm.Start()
    sender.Enabled = False
End Sub

Private Sub hideForm_Tick(sender As Object, e As EventArgs) Handles hideForm.Tick
    Me.Hide()
    hideForm.Stop()
    showForm.Start()
End Sub

Private Sub showForm_Tick(sender As Object, e As EventArgs) Handles showForm.Tick
    showForm.Stop()
    Me.Show()
    Me.Activate()
End Sub
End Class

Click the button, and immediately click on a different window (so the form loses focus). Wait until the form is hidden and shown again. The textbox should have focus. Try typing.

单击按钮,然后立即单击不同的窗口(因此表单失去焦点)。等到表单隐藏并再次显示。文本框应该有焦点。尝试打字。

If the program is run in debugging mode in Visual Studios, it works as expected. If you build the program and run it outside of VS, the form will reappear without focus, and you cannot type in the textbox without manually selecting the form.

如果程序在 Visual Studios 中以调试模式运行,它会按预期工作。如果您构建程序并在 VS 之外运行它,表单将重新出现而没有焦点,并且您无法在不手动选择表单的情况下在文本框中键入。

Sorry for the long-winded explanation. It's a difficult issue to properly describe.

抱歉冗长的解释。这是一个很难正确描述的问题。

回答by Hasitha Jayawardana

Try the form event handler Activate. Inside that method, you can use setFocusto gain focus for that particular Text Box. I know this answer is too late. But hope this helps someone.

试试表单事件处理程序Activate。在该方法中,您可以使用setFocus获得该特定文本框的焦点。我知道这个答案为时已晚。但希望这对某人有所帮助。

Private Sub Form_Activate()
    TextBox1.SetFocus
End Sub

回答by Javier Gómez

What I tried (and worked for me), was to set the Focus()of the Textbox in the event handler Shown():

我尝试过(并对我来说有效)是Focus()在事件处理程序中设置文本框的Shown()

Private Sub UsersForm_Shown(sender As Object, e As EventArgs) Handles MyBase.Shown
    Me.UserText.Focus()
End Sub

Note: the Select()method just didn't do the job. I hope this helps anyone else that comes with this same issue.

注意:该Select()方法没有完成这项工作。我希望这可以帮助遇到同样问题的其他人。

回答by AJD

Try an event handler for Form_Activate, and within that handler pass the focus to your textbox.

为 尝试一个事件处理程序Form_Activate,并在该处理程序中将焦点传递给您的文本框。

Instead of Focus, you can also try TextBox1.Select. This SO linkprovides some additional information and something about the difference between Focusand Select.

代替Focus,你也可以试试TextBox1.Select这个所谓的链接提供有关之差一些额外的信息和东西FocusSelect