none
Help making a Loterry application VB RRS feed

  • Question

  • I'm trying to make a Visual Basic Loterry application, I've managed to get it running. But I need help with 2 little details:

    -I need to make the numbers in the first 5 labels not be able to be repeated across themselves

    -Making a sixth textboxt that accepts a user input and prints it alongside these other 5 numbers.

    Here is my code so far

    Any help and/or pointers would be greatly appretiated

    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
            Dim rnd As Random = New Random
    
            Dim RandomNumber As Integer
    
            Dim i, x(5), b, counter As Byte
            Dim ctrl As Control
    
    
            For i = 1 To 40
    
                RandomNumber = rnd.Next(0, 50)
    
                For Each ctrl In Me.Controls
                    If TypeOf ctrl Is Label And ctrl.Name = "Label" & i.ToString Then
                        x(i - 1) = RandomNumber
                        counter = 0
                        For b = 0 To 4
                            If x(b) = x(i - 1) Then
                                counter += 1
    
                            End If
                        Next
    
                        If counter > 1 Then
                            i -= 1
                        Else
                            ctrl.Text = RandomNumber
                        End If
                    End If
                Next
    
    
            Next
        End Sub
    

    Tuesday, January 30, 2018 3:11 AM

Answers

  • I'm trying to make a Visual Basic Loterry application, I've managed to get it running. But I need help with 2 little details:

    -I need to make the numbers in the first 5 labels not be able to be repeated across themselves

    -Making a sixth textboxt that accepts a user input and prints it alongside these other 5 numbers.

    Hi lamWAR,

    If you want to add the unduplicated numbers in the label, you can get 5 unduplicated number firstly, then assign these value to labels. Here is the method to create 5 unduplicated numbers in HashSet, please take a look.

    Dim RandomClass As New Random()
            Dim RememberSet As New HashSet(Of Integer)
    
            Dim RandomNumber As Integer
    
            While RememberSet.Count < 5
                RandomNumber = RandomClass.Next(0, 10)
                If RememberSet.Add(RandomNumber) Then
                    MsgBox(RandomNumber)
                End If
            End While

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Tuesday, January 30, 2018 6:56 AM
    Moderator
  • Hi

    Here is some code to try out. The Form has 6 textboxes, first 5 have .Enable = False and 6th one .Enabled = True. Form has Button1.On running, and each Button Click, gets 5 Unique Randoms for first 5 TextBoxes and Blanks 6th TextBox.

    The Function needs to be called with smallest number in range, largest number in range and how many needed. In this example, I am using

    GetRandom(1,50,5) to return a list of 5 unique randoms between 1 and 50 inclusive. Then filling Textboxes accordingly.

    Option Strict On
    Option Explicit On
    Public Class Form1
      Private rand As New Random
      Private LotteryNumbers As New List(Of Integer)
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    	FillBoxes()
      End Sub
      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    	FillBoxes()
      End Sub
    
      Sub FillBoxes()
    	LotteryNumbers = GetRand(1, 50, 5)
    	TextBox1.Text = LotteryNumbers(0).ToString
    	TextBox2.Text = LotteryNumbers(1).ToString
    	TextBox3.Text = LotteryNumbers(2).ToString
    	TextBox4.Text = LotteryNumbers(3).ToString
    	TextBox5.Text = LotteryNumbers(4).ToString
    	TextBox6.Text = Nothing
      End Sub
      Function GetRand(smallest As Integer, largest As Integer, tk As Integer) As List(Of Integer)
    
    	' returns list of tk unique randoms between
    	' smallest to (largest - smallest + 1)
    	' both inclusive
    	Return Enumerable.Range(smallest, largest - smallest + 1).OrderBy(Function(n) rand.Next).Take(tk).ToList
      End Function
    End Class


    Regards Les, Livingston, Scotland

    • Marked as answer by IamWAR Wednesday, January 31, 2018 4:15 AM
    Tuesday, January 30, 2018 4:08 PM

All replies

  • As an added detail the sixth number should just be limited to Integers 1 through 15 and none else
    Tuesday, January 30, 2018 3:25 AM
  • I need to make the numbers in the first 5 labels not be able to be repeated across themselves

    You know what labels are on the form at design time, so there should be no need to pass through the Controls colelction to find them.  Use a List(Of Label), put each label that needs this updating into the list, and use the list to process the labels.

    There are many ways of avoiding duplicated random numbers.  If your labels are in a List then you can write a routine so that each time you create a new random number, you can look at all the labels in the list.  If any label already has that number, return False to indicate failure, and creawte a new random number, text it agin, and repeat until it passes. Checking the number against the label text is too complicated - use the Tag property of the label control to store the number that it is displaying.

    If your random numbers are in their own collection then there will be methods like Contains that simplify that lookup and testing.  So another option is to create a List for the random numbers, using Contains to check for duplicates, and when you have the full array, copy it to the labels.
    https://msdn.microsoft.com/en-us/library/microsoft.visualbasic.collection.contains%28v=vs.110%29.aspx

    A separate list (or array) of random numbers makes it easy to upgrade the selection process to another method, if worthwhile, later on.

    For the sixth number you will use a texbox and a label, but I suspect that there will be some validation required, beyond just ensuring it is a number.
    https://msdn.microsoft.com/en-us/library/system.windows.forms.maskedtextbox(v=vs.110).aspx

    Tuesday, January 30, 2018 3:40 AM
  • I'm trying to make a Visual Basic Loterry application, I've managed to get it running. But I need help with 2 little details:

    -I need to make the numbers in the first 5 labels not be able to be repeated across themselves

    -Making a sixth textboxt that accepts a user input and prints it alongside these other 5 numbers.

    Hi lamWAR,

    If you want to add the unduplicated numbers in the label, you can get 5 unduplicated number firstly, then assign these value to labels. Here is the method to create 5 unduplicated numbers in HashSet, please take a look.

    Dim RandomClass As New Random()
            Dim RememberSet As New HashSet(Of Integer)
    
            Dim RandomNumber As Integer
    
            While RememberSet.Count < 5
                RandomNumber = RandomClass.Next(0, 10)
                If RememberSet.Add(RandomNumber) Then
                    MsgBox(RandomNumber)
                End If
            End While

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Tuesday, January 30, 2018 6:56 AM
    Moderator
  • Hi

    Here is some code to try out. The Form has 6 textboxes, first 5 have .Enable = False and 6th one .Enabled = True. Form has Button1.On running, and each Button Click, gets 5 Unique Randoms for first 5 TextBoxes and Blanks 6th TextBox.

    The Function needs to be called with smallest number in range, largest number in range and how many needed. In this example, I am using

    GetRandom(1,50,5) to return a list of 5 unique randoms between 1 and 50 inclusive. Then filling Textboxes accordingly.

    Option Strict On
    Option Explicit On
    Public Class Form1
      Private rand As New Random
      Private LotteryNumbers As New List(Of Integer)
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    	FillBoxes()
      End Sub
      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    	FillBoxes()
      End Sub
    
      Sub FillBoxes()
    	LotteryNumbers = GetRand(1, 50, 5)
    	TextBox1.Text = LotteryNumbers(0).ToString
    	TextBox2.Text = LotteryNumbers(1).ToString
    	TextBox3.Text = LotteryNumbers(2).ToString
    	TextBox4.Text = LotteryNumbers(3).ToString
    	TextBox5.Text = LotteryNumbers(4).ToString
    	TextBox6.Text = Nothing
      End Sub
      Function GetRand(smallest As Integer, largest As Integer, tk As Integer) As List(Of Integer)
    
    	' returns list of tk unique randoms between
    	' smallest to (largest - smallest + 1)
    	' both inclusive
    	Return Enumerable.Range(smallest, largest - smallest + 1).OrderBy(Function(n) rand.Next).Take(tk).ToList
      End Function
    End Class


    Regards Les, Livingston, Scotland

    • Marked as answer by IamWAR Wednesday, January 31, 2018 4:15 AM
    Tuesday, January 30, 2018 4:08 PM
  • I tried your code and it works great, I managed to set a restriction so that it only accepts INTs on the last textbox, but there's two details I've not figured out:

    1) For some reason at the time of running the random Numbers are already generated, I'd like for the program to wait until the generating button is pressed.

    2)The last textbox accepts any number of INTs, I'd like to be able to restrict it to say just INTS between 1-15


    • Edited by IamWAR Thursday, February 1, 2018 8:24 PM
    Thursday, February 1, 2018 8:22 PM
  • Hi

    Give this a try:

    Option Strict On
    Option Explicit On
    Public Class Form1
      Private rand As New Random
      Private LotteryNumbers As New List(Of Integer)
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    	'	FillBoxes()
      End Sub
      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    	FillBoxes()
      End Sub
      Sub FillBoxes()
    	LotteryNumbers = GetRand(1, 50, 5)
    	TextBox1.Text = LotteryNumbers(0).ToString
    	TextBox2.Text = LotteryNumbers(1).ToString
    	TextBox3.Text = LotteryNumbers(2).ToString
    	TextBox4.Text = LotteryNumbers(3).ToString
    	TextBox5.Text = LotteryNumbers(4).ToString
    	'	TextBox6.Text = Nothing
      End Sub
      Function GetRand(smallest As Integer, largest As Integer, tk As Integer) As List(Of Integer)
    	' returns list of tk unique randoms between
    	' smallest to (largest - smallest + 1)
    	' both inclusive
    	Return Enumerable.Range(smallest, largest - smallest + 1).OrderBy(Function(n) rand.Next).Take(tk).ToList
      End Function
      Private Sub TextBox6_Keydown(sender As Object, e As KeyEventArgs) Handles TextBox6.KeyDown
    	Dim allow As String = "0123456789" & ChrW(8) & ChrW(37) & ChrW(39)
    	If Not allow.Contains(ChrW(e.KeyValue)) Then
    	  e.SuppressKeyPress = True
    	  Exit Sub
    	End If
    	Dim v As Integer = GetInteger(TextBox6.Text & ChrW(e.KeyValue))
    	If v > 15 Or v = 0 Then
    	  TextBox6.Text = Nothing
    	  e.SuppressKeyPress = True
    	End If
      End Sub
      Function GetInteger(s As String) As Integer
    	Dim v As Integer = 0
    	If Integer.TryParse(s, v) Then Return v
    	Return 0
      End Function
    End Class


    Regards Les, Livingston, Scotland

    Thursday, February 1, 2018 9:25 PM