问题
I need help with one function in VB.net.
I have 6 textboxes limited only to numbers and only to numbers from 1-25 which user must fill in. I need a way to check for duplicate numbers in textboxes when i click button.
Here is my code so far:
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
Thank you :)
Ok, i managed to get it working, here is the code:
Ok, thank you guys for answering me.
I have found solution and if anyone needs it i will post it here:
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:
Well if there is some duplicate, it means that there are at least 2.
You have to do something like this
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
}
}
To get a control by name in VB - How do I refer to a windows form control by name (C# / VB)
I hope you can translate this in VB.
来源:https://stackoverflow.com/questions/20914666/vb-net-duplicate-textbox-inputs-validation