使用功能键在vb.net窗口应用程序中定位特定控件

时间:2013-07-29 08:32:49

标签: vb.net winforms

我的vb.net应用程序我有一个工具条菜单。点击工具条。我一次打开四个四分钟..这都是我的孩子论坛。所以我给了代码 InvolveAllToolStripMenuItem_Click:

Private Sub InvolveAllToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles InvolveAllToolStripMenuItem.Click
    AddHandler Me.KeyUp, AddressOf HandleKeyPress
    Dim frm As New frmKeyAssignBoard
    frm.Location = New Point(625, 0)
    frm.MdiParent = Me
    AddHandler frm.KeyUp, AddressOf HandleKeyPress
    frm.Show()
    Dim frm1 As New FrmrecievedDelivaryRequest
    frm1.Location = New Point(625, 225)
    frm1.MdiParent = Me
    AddHandler frm.KeyUp, AddressOf HandleKeyPress
    frm1.Show()
    Dim frm2 As New FrmDelivary
    frm2.Location = New Point(965, 0)
    frm2.MdiParent = Me
    AddHandler frm.KeyUp, AddressOf HandleKeyPress
    frm2.Show()
    Dim frm3 As New frmCarCall
    frm3.Location = New Point(0, 0)
    frm3.MdiParent = Me
    AddHandler frm.KeyUp, AddressOf HandleKeyPress
    frm3.Show()
End Sub

我想在这里设置一些功能键:

  • 如果我点击f3,我想将光标定位到frmKeyAssignBoard中的特定文本框。
  • 如果我点击f4,我想将光标定位到FrmrecievedDelivaryRequest内的特定文本框。

那么我可以为此编写程序吗?

1 个答案:

答案 0 :(得分:0)

要捕获按键事件,通常我们会处理表单的KeyPress或KeyUp事件。

然而,在您的情况下,您打开了5个表单(父级+4个孩子)。因此我建议你在父表单中编写处理程序,但是将所有孩子的keypress事件注册到它,一些伪代码:

    Sub ShowChildren()
        ' Handle own Key up event
        AddHandler Me.KeyUp, AddressOf HandleKeyPress

        ' form 1
        Dim frm As New frmKeyAssignBoard()
        frm.Location = New Point(625, 0)
        frm.MdiParent = Me
        AddHandler frm.KeyUp, AddressOf HandleKeyPress

        ' similar for frm2, frm3, ...
        ' 
    End Sub

    Private Sub HandleKeyPress(sender As Object, e As KeyEventArgs)
        If e.KeyCode = Keys.F2 Then
            ' do whatever necessary when F2 is pressed
        ElseIf e.KeyCode = Keys.F3 Then
            ' do whatever necessary when F3 is pressed
            ' for example, if you want fo focus on frm1's particular control
            '         but have to expose TxtBox as a public readonly property beforehand
            frm1.BringToFront()
            frm1.TxtBox.Select()
            frm1.TxtBox.Focus()
        ElseIf e.KeyCode = Keys.F4 Then
            ' do whatever necessary when F4 is pressed
        Else
            '...
        End If
    End Sub