06-12-12, 02:58 PM
جرب هذا الكود بعد تعديل خفيف على الكود الأصلي
كود :
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
10:
Dim Code As String = ""
Dim N1, N2 As Integer
N1 = RandomInteger(0, 21)
N2 = RandomInteger(0, 21)
Do While N1 = N2
N2 = RandomInteger(0, 21)
Loop
TextBox1.Text = N1 & "|" & N2
If Not RepeatedRandomCount(TextBox1.Text) > 1 Then
ListView1.Items.Add(TextBox1.Text)
Else
GoTo 10 'إذا كان الرقم مكرر مرتين سيبحث عن رقم عشوائي جديد
End If
End Sub
Private Function RandomInteger(ByVal Lowerbound As Integer, ByVal Upperbound As Integer) As Integer
Dim Rnd As New Random
RandomInteger = Rnd.Next(Lowerbound, Upperbound)
End Function
Private Function RepeatedRandomCount(ByVal RandomNumber As String) As Integer
Dim Count As Integer = 0
For Each Item As ListViewItem In ListView1.Items
If Item.Text = RandomNumber Then
Count += 1
End If
Next
Return Count
End Function