如何缩短Textbox控件的代码

时间:2012-08-30 05:13:54

标签: vb.net winforms user-interface

我有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

2 个答案:

答案 0 :(得分:1)

我假设您尝试使用输入和箭头键启用表单导航。

如果是这种情况:

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

    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排除为导航命令。

答案 1 :(得分:-1)

使用可以使用“Select Case”而不是“If”条件来清理代码。 这会更清洁。 see this link

@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