将焦点设置为新的弹出窗口winform

时间:2013-07-09 15:16:36

标签: c# vb.net winforms focus show

我有一个在系统托盘中运行的应用程序,当用户按下按钮组合时,它会显示要填写的WinForm并发送电子邮件。一切都很好但是显示WinForm时的部分。它显示在顶部,焦点显示在文本框上,但窗口未激活。

用于调用Popup表单的代码。

My.Forms.frmpopup.ShowDialog()

弹出窗体上的代码

Private Sub frmPopup_Shown(sender As Object, e As EventArgs) Handles Me.Shown
    Activate()
    BringToFront()
End Sub

Private Sub frmPopup_Load(sender As Object, e As EventArgs) Handles Me.Load
    TextBoxName.Focus()
End Sub

裤子的真正好处在于,只要用户当前没有专注于Internet Explorer窗口,这就有效。如果我找到解决方案,我会发布它。


似乎将两个Subs合并为一个子修复了这个问题,我将继续测试。

  

Private Sub frmPopup_Shown(sender as Object,e As EventArgs)Handles   Me.Shown       启用()       BringToFront()       TextBoxName.Focus()End Sub

3 个答案:

答案 0 :(得分:1)

确保你的应用程序也有焦点......

包括以下导入...

<Runtime.InteropServices.DllImport("user32.dll", SetLastError:=True, CharSet:=Runtime.InteropServices.CharSet.Auto)> _
Private Shared Function SetForegroundWindow(ByVal hWnd As IntPtr) As Long
End Function

在您拨打弹出窗口之前,或在弹出式LOAD事件中,请致电

SetForegroundWindow(Me.Handle)

答案 1 :(得分:1)

SendToTop(true)应该将窗口带到TopMost。

Private Const SWP_NOSIZE As Integer = &H1
Private Const SWP_NOMOVE As Integer = &H2

Private Shared ReadOnly HWND_TOPMOST As New IntPtr(-1)
Private Shared ReadOnly HWND_NOTOPMOST As New IntPtr(-2)
Public Declare Function SetWindowPos Lib "user32" (ByVal hWnd As IntPtr, ByVal hWndInsertAfter As IntPtr, ByVal X As Integer, ByVal Y As Integer, ByVal cx As Integer, ByVal cy As Integer, ByVal uFlags As Integer) As Boolean

Public Sub SendToTop(toTop As Boolean)
    If toTop Then
        SetWindowPos(Me.Handle, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE)
    Else
        SetWindowPos(Me.Handle, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOSIZE Or SWP_NOMOVE)
    End If
End Sub

答案 2 :(得分:1)

在弹出窗口加载事件

上使用Active();