在验证事件处理程序中将Enabled设置为true时,将忽略Tabindex

时间:2015-08-12 15:58:07

标签: vb.net winforms

我有三个文本框。

txtBox1 - 启用= True - TabIndex = 1

txtBox2 - 已启用=错误 - TabIndex = 2

txtBox3 - 启用= True - TabIndex = 3

我有一个事件处理程序,当txtBox1正在验证时,它为txtBox2设置Enabled = True。我的问题是光标在离开txtBox1后没有转到txtBox2,它会跳转到txtBox3。

UITableViewController

在验证事件处理程序中启用字段后,有没有办法让TabIndex得到尊重?我可以使用Select()但是会在字段(SHIFT + TAB)中打破反向标签。

谢谢!

2 个答案:

答案 0 :(得分:3)

这是一个简单的鸡蛋问题。由于文本框失去焦点,因此将触发Validating事件。这是因为你选中了下一个控件。当然这是txtBox3,启用txtBox2不会改变它。

易于修复,ActiveControl属性告诉您哪个控件处于活动状态。所以这样写:

  txtBox2.Enabled = True
  If Me.ActiveControl Is txtBox3 Then txtBox2.Focus()

只要您有超过3个可以获得焦点的控件,反向标签就能正常工作。

答案 1 :(得分:0)

好的,所以我发布了错误的解决方案,所以这里是正确的。汉斯让我思考,这就是我想出来的,符合我的需要:

Public Class Form1

' First you have to declare a variable
Dim m_TabForward As Boolean

' Capture the keystrokes to check for direction.
Private Sub GetTabDirection(ByVal sender As Object, _
                            ByVal e As PreviewKeyDownEventArgs) Handles _
                            txtBox1.PreviewKeyDown

    If (e.KeyCode = Keys.Tab AndAlso e.Modifiers = Keys.Shift) Then
        m_TabForward = False
    ElseIf e.KeyCode = Keys.Tab Then
        m_TabForward = True
    End If

End Sub

' Enable the next textbox and select it. Because SelectNextControl
' takes a direction argument you don't have a problem with reversing order
' like you would using txtBox2.Select()
Private Sub ValidateFields(ByVal Sender As Object, _
                      ByVal e As System.ComponentModel.CancelEventArgs) _
                      Handles txtBox1.Validating

    Dim ctl As Control = Sender

    If Sender Is txtBox1 Then
        txtBox2.Enabled = True
    End If

    SelectNextControl(ctl, m_TabForward, True, True, True)

End Sub

End Class