VB.net重复文本框输入验证

时间:2014-01-03 23:33:27

标签: vb.net validation numbers duplicates

我需要VB.net中的一个函数的帮助。

我有6个文本框仅限于数字,仅限于1-25用户必须填写的数字。当我点击按钮时,我需要一种检查文本框中重复数字的方法。

到目前为止,这是我的代码:

Private Sub validate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)     Handles validate.Click
    For Each t In textBoxes
        If String.IsNullOrEmpty(t.Text) Then
            nr1.Text = ""
            nr2.Text = ""
            nr3.Text = ""
            nr4.Text = ""
            nr5.Text = ""
            nr6.Text = ""
            nr1.Focus()
            Exit Sub
            Exit For
        End If
    Next t

    Dim rand = GetRandom(1, 1715)
    Dim miliseconds = CLng(DateTime.Now.Subtract(New DateTime(1970, 1, 1)).TotalMilliseconds)

    strSQL = xxxxxxxx

    Dim da As New MySqlDataAdapter(strSQL, CONNECTION)
    da.Fill(ds)
    nr1.Text = ""
    nr2.Text = ""
    nr3.Text = ""
    nr4.Text = ""
    nr5.Text = ""
    nr6.Text = ""
    value.Text = "1"
    broj1.Focus()

    list()
End Sub

谢谢:)

好的,我设法让它工作,这是代码:

好的,谢谢你们回答我。

我找到了解决方案,如果有人需要,我会在这里发布:

Dim textBoxes As TextBox() = New TextBox() {nr1, nr2, nr3, nr4, nr5, nr6}
For i As Integer = 0 To textBoxes.Length - 2
    For j As Integer = i + 1 To textBoxes.Length - 1
        If textBoxes(i).Text = textBoxes(j).Text Then
            //failed to execute, found duplicates
            MessageBox.Show(Me, "Duplicate value.")
            textBoxes(j).Focus()
            Return
        End If
    Next
Next
//sucessful

1 个答案:

答案 0 :(得分:-1)

如果有一些重复,那就意味着至少有2个。

你必须做这样的事情

for(int i=1; i<6; i++)
for(int j=i+1; j<=6; j++)
{
 if(the value of the control with the name nr+i has the same value as the control with the name nr+j)
{
// there is a duplicate
}
}

要在VB中按名称获取控件 - How do I refer to a windows form control by name (C# / VB)

我希望你能用VB翻译它。