禁用按钮,直到多个文本框得到验证

时间:2013-07-03 11:08:42

标签: vb.net winforms validation

我有一个包含10个以上文本框和1个按钮的表单,我想禁用带有实时验证的按钮,直到所有文本框都填充了10或13长度的数值,到目前为止,我的代码如下:

Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
    For Each userID As Control In Me.Controls.OfType(Of TextBox)()
        AddHandler userID.TextChanged, AddressOf ValidateAllFields
    Next userID
End Sub
Private Sub userID_KeyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs) Handles Me.KeyPress
    If e.KeyChar <> ChrW(Keys.Back) Then
        If Char.IsNumber(e.KeyChar) Then
        Else
            e.Handled = True
        End If
    End If
End Sub
Private Function ValidateAllFields()
    Dim Validation As Boolean = True
    For Each userID As Control In Me.Controls.OfType(Of TextBox)()
        Dim e As New System.ComponentModel.CancelEventArgs
        e.Cancel = False
        Call userID_Validating(userID, e)
        If e.Cancel = True Then Validation = False
    Next userID
    buttonSave.Enabled = Not Me.Controls.OfType(Of TextBox).Any(Function(userID) userID.Text.Length <> 10 AndAlso userID.Text.Length <> 13)
    Return Validation
End Function
Private Sub userID_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles _
    user00.Validating, _
    user01.Validating, _
    user02.Validating, _
    user03.Validating, _
    user04.Validating, _
    user05.Validating, _
    user06.Validating, _
    user07.Validating, _
    user07.Validating, _
    user08.Validating, _
    user09.Validating, _
    user10.Validating, _
    user11.Validating
    If Not IsNumeric(sender.Text) OrElse (sender.Text.Length <> 10) AndAlso (sender.Text.Length <> 13) Then
        ErrorProvider1.SetError(sender, "")
        ErrorProvider2.SetError(sender, "Please enter a valid User ID.")
        e.Cancel = True
    Else
        ErrorProvider1.SetError(sender, "Valid User ID.")
        ErrorProvider2.SetError(sender, "")
    End If
End Sub

感谢您的帮助,它可以按照我的意愿运作,但您可以帮助我改善/清洁它吗?我还在学习vb,我对任何建议持开放态度。提前谢谢!

2 个答案:

答案 0 :(得分:0)

您可以在此处执行所需操作的代码:

Dim done1, done2, done3 As Boolean
Dim targetLength1 As Integer = 10
Dim targetLength2 As Integer = 13
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
    Try
        If (IsNumeric(TextBox1.Text)) Then
            If (TextBox1.Text.Length = targetLength1 Or TextBox1.Text.Length = targetLength2) Then
                done1 = True
            End If
        End If
        If (done1 And done2 And done3) Then
            Button1.Enabled = True
        End If
    Catch ex As Exception

    End Try
End Sub

Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged
    Try
        If (IsNumeric(TextBox2.Text)) Then
            If (TextBox2.Text.Length = targetLength1 Or TextBox2.Text.Length = targetLength2) Then
                done2 = True
            End If
        End If
        If (done1 And done2 And done3) Then
            Button1.Enabled = True
        End If
    Catch ex As Exception

    End Try
End Sub

Private Sub TextBox3_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox3.TextChanged
    Try
        If (IsNumeric(TextBox3.Text)) Then
            If (TextBox3.Text.Length = targetLength1 Or TextBox3.Text.Length = targetLength2) Then
                done3 = True
            End If
        End If
        If (done1 And done2 And done3) Then
            Button1.Enabled = True
        End If
    Catch ex As Exception

    End Try
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Button1.Enabled = False
End Sub

它仅包含3个文本框(TextBox1TextBox2TextBox3)和1个按钮(Button1),但您可以将这个想法扩展到与您一样多的文本框希望。它依赖于每个文本框的TextChanged。当条件满足时(给定文本框具有长度为10或13的数字),相应的标志被设置为真(例如,对于TextBox1,则为done1)。当所有标志都为真(所有文本框都包含预期信息)时,该按钮被禁用。

答案 1 :(得分:0)

我认为最好使用TextChanged Event ..

Private Sub TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged, TextBox2.TextChanged, ....., TextBox10.TextChanged

    Chk4ButtonEnabled()

End Sub

Sub Chk4ButtonEnabled()
Dim s as String

  For Each userID As Control In Me.Controls
      If userID.GetType Is GetType(TextBox) Then
        s = userID.Text
        If Not s.Length = 10 OR Not s.Length = 13 Then
          ErrorProvider1.SetError(userID, "")
          buttonSave.Enabled = False  
        Else
          ErrorProvider1.SetError(userID, "Valid User ID.")
          buttonSave.Enabled = True
        End If
      End If
  Next     

End Sub