0

我有 25 个文本框控件。以下代码仅适用于 2 个文本框。所以对于 25 个文本框,代码会很长,我可以缩短代码吗?

 Private Sub TextBox_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs) Handles IDTextBox.KeyDown, DateDateTimePicker.KeyDown, ItemCodeTextBox.KeyDown, BrandTextBox.KeyDown, ItemTextBox.KeyDown
    If e.KeyCode = Keys.Enter And Me.IDTextBox.Focused Then
        Me.DateDateTimePicker.Focus()
    Else
        If e.KeyCode = Keys.Down And Me.IDTextBox.Focused Then
            Me.DateDateTimePicker.Focus()
        Else
            If e.KeyCode = Keys.Up And Me.DateDateTimePicker.Focused Then
                Me.IDTextBox.Focus()
            Else
                If e.KeyCode = Keys.Enter And Me.DateDateTimePicker.Focused Then
                    Me.ItemCodeTextBox.Focus()
                Else
                    If e.KeyCode = Keys.Down And Me.DateDateTimePicker.Focused Then
                        Me.ItemCodeTextBox.Focus()
                    Else
                        If e.KeyCode = Keys.Up And Me.ItemCodeTextBox.Focused Then
                            Me.DateDateTimePicker.Focus()
                        End If
                    End If
                End If
            End If

        End If
    End If

End Sub
4

2 回答 2

1

我假设您正在尝试使用 enter 和箭头键启用表单导航。

如果是这样的话:

  1. 删除该代码
  2. 选择表单的设计视图
  3. 右键单击 VB 工具栏并确保“布局”工具栏可见
  4. 单击最右侧名为“Tab Order”的按钮
  5. 按您想要的顺序单击每个控件
  6. 将表单的“KeyPreview”属性设置为 TRUE

然后添加以下代码:

Private Sub Form1_KeyUp(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
  If Me.ActiveControl.GetType Is GetType(DateTimePicker) Then
    'DateTimePicker
    Select Case e.KeyCode
      Case Keys.Enter
        Me.SelectNextControl(Me.ActiveControl, True, True, True, True) 'forwards
    End Select
  ElseIf Me.ActiveControl.GetType Is GetType(TextBox) AndAlso Not DirectCast(Me.ActiveControl, TextBox).Multiline Then
    'normal Textbox
    Select Case e.KeyCode
      Case Keys.Up
        Me.SelectNextControl(Me.ActiveControl, False, True, True, True)  'backwards
      Case Keys.Down, Keys.Enter
        Me.SelectNextControl(Me.ActiveControl, True, True, True, True) 'forwards
    End Select
  ElseIf Me.ActiveControl.GetType Is GetType(TextBox) AndAlso DirectCast(Me.ActiveControl, TextBox).Multiline Then
    'multiline Textbox - ignore
  Else
    'all other controls
    Select Case e.KeyCode
      Case Keys.Up
        Me.SelectNextControl(Me.ActiveControl, False, True, True, True)  'backwards
      Case Keys.Down, Keys.Enter
        Me.SelectNextControl(Me.ActiveControl, True, True, True, True) 'forwards
    End Select
  End If
End Sub

上/下箭头键实际上由 DateTimePicker 使用,因此需要将其排除为导航命令。相反,多行文本框使用 ENTER 键,因此在这种情况下必须将 ENTER 作为导航命令排除。

于 2012-08-30T06:30:17.073 回答
-1

使用可以使用“选择案例”而不是“如果”条件使您的代码干净。这会干净得多。看到这个链接

@Farook ......试试这个

Private Sub Form1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
    Select Case True
        Case (e.KeyCode = Keys.Down And Me.ActiveControl.GetType IsNot GetType(DateTimePicker))
                Me.SelectNextControl(Me.ActiveControl, True, True, True, True) 'forwards
        Case (e.KeyCode = Keys.Enter And Not (Me.ActiveControl.GetType Is GetType(TextBox) AndAlso DirectCast(Me.ActiveControl, TextBox).Multiline))
            Me.Text = Me.SelectNextControl(Me.ActiveControl, True, True, True, True).ToString  'forwards
        Case (e.KeyCode = Keys.Up And Me.ActiveControl.GetType IsNot GetType(DateTimePicker))
            Me.SelectNextControl(Me.ActiveControl, False, True, True, True)  'backwards
    End Select
End Sub
于 2012-08-30T05:16:56.730 回答