none
How can I sort Listboxes using (Random) RRS feed

  • Question

  • Hi Good People.

    I am trying to write code for a number generator/picker using List boxes, On My form I have...

    1. Listbox1
    2. Listbox2
    3. Listbox3
    4. Listbox4
    5. Listbox5
    6. Listbox6
    7. 1 Button called (cmdMainGenerate)
    8. cmdGenerate1.PerformClick()
    9. cmdGenerate2.PerformClick()
    10. cmdGenerate3.PerformClick()
    11. cmdGenerate4.PerformClick()
    12. cmdGenerate5.PerformClick()
    13. cmdGenerate6.PerformClick()
    1. I have used "Random" in my code to Generate one number for each of the six Listboxes
    2. When I click cmdMainGenerate, This in turn Performs Clicks on all other Buttons
    3. Then Numbers a generated, into all the Listboxes, But The number are not in sequence etc (46, 39, 9, 23, 39, 53)
    4. Does anyone know how I can make these numbers to be in order from left to right.

    The Code I have is below...

    Option Strict On
    Option Explicit On
    Public Class Form1
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
        End Sub
    
        Private Sub cmdGenerate1_Click(sender As Object, e As EventArgs) Handles cmdGenerate1.Click
            Dim Generator As New Random
            Dim MyRand As Integer
            ListBox1.Items.Clear()
    
            MyRand = Generator.Next(1, 59)
            ListBox1.Items.Add(MyRand.ToString())
        End Sub
    
    
        Private Sub cmdGenerate2_Click(sender As Object, e As EventArgs) Handles cmdGenerate2.Click
            Dim Generator As New Random
            Dim MyRand As Integer
            ListBox2.Items.Clear()
    
            MyRand = Generator.Next(1, 59)
            ListBox2.Items.Add(MyRand.ToString())
        End Sub
    
        Private Sub cmdGenerate3_Click(sender As Object, e As EventArgs) Handles cmdGenerate3.Click
            Dim Generator As New Random
            Dim MyRand As Integer
            ListBox3.Items.Clear()
    
            MyRand = Generator.Next(1, 59)
            ListBox3.Items.Add(MyRand.ToString())
        End Sub
    
        Private Sub cmdGenerate4_Click(sender As Object, e As EventArgs) Handles cmdGenerate4.Click
            Dim Generator As New Random
            Dim MyRand As Integer
            ListBox4.Items.Clear()
    
            MyRand = Generator.Next(1, 59)
            ListBox4.Items.Add(MyRand.ToString())
        End Sub
    
        Private Sub cmdGenerate5_Click(sender As Object, e As EventArgs) Handles cmdGenerate5.Click
            Dim Generator As New Random
            Dim MyRand As Integer
            ListBox5.Items.Clear()
    
            MyRand = Generator.Next(1, 59)
            ListBox5.Items.Add(MyRand.ToString())
        End Sub
    
        Private Sub cmdGenerate6_Click(sender As Object, e As EventArgs) Handles cmdGenerate6.Click
            Dim Generator As New Random
            Dim MyRand As Integer
            ListBox6.Items.Clear()
    
            MyRand = Generator.Next(1, 59)
            ListBox6.Items.Add(MyRand.ToString())
        End Sub
    
        Private Sub cmdMainGenerate_Click(sender As Object, e As EventArgs) Handles cmdMainGenerate.Click
            cmdGenerate1.PerformClick()
            cmdGenerate2.PerformClick()
            cmdGenerate3.PerformClick()
            cmdGenerate4.PerformClick()
            cmdGenerate5.PerformClick()
            cmdGenerate6.PerformClick()
    
            'NumberOrder()
        End Sub
    
        'Private Function NumberOrder()
        '    Dim Val1 As Integer = Val(ListBox1.Items.ToString)
        '    Dim Val2 As Integer = Val(ListBox2.Items.ToString)
        '    Dim Val3 As Integer = Val(ListBox3.Items.ToString)
        '    Dim Val4 As Integer = Val(ListBox4.Items.ToString)
        '    Dim Val5 As Integer = Val(ListBox5.Items.ToString)
        '    Dim Val6 As Integer = Val(ListBox6.Items.ToString)
    
        '    If Val(Val1.ToString < Val(Val2.ToString)) Then
        '        TextBox1.Text = Val1.ToString
        '    Else
        '        TextBox1.Text = Val2.ToString
        '    End If
    
        'End Function
    
    
    
    End Class

     



    Gary Simpson

    Friday, September 20, 2019 1:05 PM

Answers

  • Hi

    Still confused here.

    A ListBox doesn't have a 'value' as such - unless a selected item value.

    Here is amended code. Here, I have added 6 Labels and a Button2 to Form1. Once Button1 has been clicked to produce a set of ListBox item values, then Button2 click will cycle through the ListBoxes and put the item values into the 6 labels.

    Is that any closer to your needs?

    EDIT: just occurred to me.If all you want is a set of random numbers into 6 Labels/TextBoxes, then why the 5 Lists? The labels/TextBoxes could easily be filled directly.

    ' Form1 with ListBoxes 1-6
    ' Labels 1-6
    ' Button1 and Button2
    Option Strict On
    Option Explicit On
    Public Class Form1
    	Dim Generator As New Random
    	Dim b As Integer = 1
    	Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    		For Each c As Control In Controls
    			If c.GetType() Is GetType(ListBox) Then
    				CType(c, ListBox).DataSource = GetRand()
    			End If
    		Next
    		b = 1
    	End Sub
    	Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    		If b > 6 Then b = 1
    		For Each c As Control In Controls
    			If c.GetType() Is GetType(ListBox) Then
    				Dim lb As ListBox = CType(c, ListBox)
    				If lb.Items.Count = 6 Then
    					Dim ItemCounter As Integer = 0
    					If lb.Name.EndsWith(b.ToString) Then
    						For Each cc As Control In Controls
    							If cc.GetType() Is GetType(Label) Then
    								CType(cc, Label).Text = lb.Items(ItemCounter).ToString
    								ItemCounter += 1
    							End If
    						Next
    					End If
    				Else
    					b = 1
    				End If
    			End If
    		Next
    		b += 1
    	End Sub
    	Function GetRand() As List(Of Integer)
    		Dim lst As New List(Of Integer)
    		While lst.Count < 6
    			Dim r As Integer = Generator.Next(1, 59)
    			If Not lst.Contains(r) Then lst.Add(r)
    		End While
    		lst.Sort()
    		Return lst
    	End Function
    End Class


    Regards Les, Livingston, Scotland


    • Edited by leshay Friday, September 20, 2019 3:42 PM
    • Marked as answer by Gary Simpson Friday, September 20, 2019 3:58 PM
    Friday, September 20, 2019 3:39 PM

All replies

  • Hi

    Here is some code that willfill 6 ListBoxes with ordered unique random numbers each time Button1 is clicked.

    However, Ijustnoticed theimage at the bottom of your post and I suspect now that this is not what you want. It has confused me.

    ' Form1 with ListBoxes 1-6
    ' and Button1
    Option Strict On
    Option Explicit On
    Public Class Form1
    	Dim Generator As New Random
    	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
    		For Each c As Control In Controls
    			If c.GetType() Is GetType(ListBox) Then
    				CType(c, ListBox).DataSource = GetRand()
    			End If
    		Next
    	End Sub
    	Function GetRand() As List(Of Integer)
    		Dim lst As New List(Of Integer)
    		While lst.Count < 6
    			Dim r As Integer = Generator.Next(1, 59)
    			If Not lst.Contains(r) Then lst.Add(r)
    		End While
    		lst.Sort()
    		Return lst
    	End Function
    End Class


    Regards Les, Livingston, Scotland


    • Edited by leshay Friday, September 20, 2019 2:45 PM
    Friday, September 20, 2019 2:43 PM
  • Hi Les

    thanks again for trying to help me. Is what I am trying to achieve is once the number are selected.

    I need to sort in order the Listboxes,

    Lets say Listbox1 value = 39 and Listbox2 value = 11 and Listbox3 = 49 etc.

    what I would like to achieve is after all number have been selected in each Listbox, Is either use Textboxes or Labels to put the Listboxes values in order.

    Meaning

    textbox1 or Label1 = value of listbox2 = 11 then

    textbox2 or label2 = value of Listbox1 = 39 Then

    textbox3 or Label3 = value of Lisbox3 = 49 

    So I can show the Value/result of all Listboxes in the order from lowest value to highest value.

    Only one number at a time from 1 to 59 in each Listbox.

     Sorry for any confusion caused. 


    Gary Simpson


    Friday, September 20, 2019 3:12 PM
  • Hi

    Still confused here.

    A ListBox doesn't have a 'value' as such - unless a selected item value.

    Here is amended code. Here, I have added 6 Labels and a Button2 to Form1. Once Button1 has been clicked to produce a set of ListBox item values, then Button2 click will cycle through the ListBoxes and put the item values into the 6 labels.

    Is that any closer to your needs?

    EDIT: just occurred to me.If all you want is a set of random numbers into 6 Labels/TextBoxes, then why the 5 Lists? The labels/TextBoxes could easily be filled directly.

    ' Form1 with ListBoxes 1-6
    ' Labels 1-6
    ' Button1 and Button2
    Option Strict On
    Option Explicit On
    Public Class Form1
    	Dim Generator As New Random
    	Dim b As Integer = 1
    	Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    		For Each c As Control In Controls
    			If c.GetType() Is GetType(ListBox) Then
    				CType(c, ListBox).DataSource = GetRand()
    			End If
    		Next
    		b = 1
    	End Sub
    	Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    		If b > 6 Then b = 1
    		For Each c As Control In Controls
    			If c.GetType() Is GetType(ListBox) Then
    				Dim lb As ListBox = CType(c, ListBox)
    				If lb.Items.Count = 6 Then
    					Dim ItemCounter As Integer = 0
    					If lb.Name.EndsWith(b.ToString) Then
    						For Each cc As Control In Controls
    							If cc.GetType() Is GetType(Label) Then
    								CType(cc, Label).Text = lb.Items(ItemCounter).ToString
    								ItemCounter += 1
    							End If
    						Next
    					End If
    				Else
    					b = 1
    				End If
    			End If
    		Next
    		b += 1
    	End Sub
    	Function GetRand() As List(Of Integer)
    		Dim lst As New List(Of Integer)
    		While lst.Count < 6
    			Dim r As Integer = Generator.Next(1, 59)
    			If Not lst.Contains(r) Then lst.Add(r)
    		End While
    		lst.Sort()
    		Return lst
    	End Function
    End Class


    Regards Les, Livingston, Scotland


    • Edited by leshay Friday, September 20, 2019 3:42 PM
    • Marked as answer by Gary Simpson Friday, September 20, 2019 3:58 PM
    Friday, September 20, 2019 3:39 PM