0

我有一个包含 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,我愿意接受任何建议。提前致谢!

4

2 回答 2

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...)。当所有标志都为真(所有文本框都包含预期信息)时,按钮被禁用。

于 2013-07-03T11:28:08.080 回答
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
于 2013-07-03T11:39:45.400 回答