返回当前选择的对象

时间:2019-08-30 23:48:06

标签: vb.net select

代码说明了一切,如果我按F1键并且已经选择了Button1,那么它应该选择Button2,反之亦然。

If keyData = Keys.F1 Then
     If Button1.Select() = True Then
          Button2.Select()
     ElseIf Button2.Select() = True Then
          Button1.Select()
     Else
          Button1.Select()
     End If
End If

但是表达式“ Button2.Select()= True”不会返回值。

2 个答案:

答案 0 :(得分:0)

Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
    If e.KeyValue = Keys.F1 Then
        If ActiveControl.Name = "Button1" Then
            Button2.Select()
        Else
            Button1.Select()
        End If
    End If
End Sub

实际上,F1是一个糟糕的选择,因为传统上它是“帮助”键。

答案 1 :(得分:0)

我们可以使用表单keydown(将表单keypreview属性设置为true)或使用按钮Previewkeydown,如下所示:

Private Sub Button1_PreviewKeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.PreviewKeyDownEventArgs) Handles Button1.PreviewKeyDown
    If e.KeyCode = Keys.F1 Then
        Button2.Focus()
    End If
End Sub

Private Sub Button2_PreviewKeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.PreviewKeyDownEventArgs) Handles Button2.PreviewKeyDown
    If e.KeyCode = Keys.F1 Then
        Button1.Focus()
    End If
End Sub