Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. Visual Basic
  4. The randmize function using listboxes

The randmize function using listboxes

Scheduled Pinned Locked Moved Visual Basic
questionhelptutorial
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    China Gary
    wrote on last edited by
    #1

    Basically i have a listbox and need to take each item (string) out of that listbox to populate textboxes, so the first listbox item goes into the first textbox, second to second etc... Here is the code that i have for this that works. For i As Int16 = 0 To lstListbox.Items.Count - 1 Select Case i Case 0 : txt1.Text = lstListbox.Items(i).ToString Case 1 : txt2.Text = lstListbox.Items(i).ToString Case 2 : txt3.Text = lstListbox.Items(i).ToString Case 3 : txt4.Text = lstListbox.Items(i).ToString Case 4 : txt5.Text = lstListbox.Items(i).ToString Case 5 : txt6.Text = lstListbox.Items(i).ToString End Select Next My question is, how do i randomly do this, instead of having the first item go to the first textbox etc, so perhaps having the first item going to the txt5 or txt3 for example and so on. Help or guidance would be brilliant, thanks!

    T 1 Reply Last reply
    0
    • C China Gary

      Basically i have a listbox and need to take each item (string) out of that listbox to populate textboxes, so the first listbox item goes into the first textbox, second to second etc... Here is the code that i have for this that works. For i As Int16 = 0 To lstListbox.Items.Count - 1 Select Case i Case 0 : txt1.Text = lstListbox.Items(i).ToString Case 1 : txt2.Text = lstListbox.Items(i).ToString Case 2 : txt3.Text = lstListbox.Items(i).ToString Case 3 : txt4.Text = lstListbox.Items(i).ToString Case 4 : txt5.Text = lstListbox.Items(i).ToString Case 5 : txt6.Text = lstListbox.Items(i).ToString End Select Next My question is, how do i randomly do this, instead of having the first item go to the first textbox etc, so perhaps having the first item going to the txt5 or txt3 for example and so on. Help or guidance would be brilliant, thanks!

      T Offline
      T Offline
      TwoFaced
      wrote on last edited by
      #2

      You'll need to add each textbox to a list and then randomly select which one you want. Once you pick the textbox you set the text and remove it from the list so it's not chosen again. This should work for you.

      Private Sub Button1\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
          'This list will contain our textboxes
          Dim itemList As New List(Of TextBox)
          'Add each textbox to the list
          itemList.AddRange(New TextBox() { \_
              txt1, \_
              txt2, \_
              txt3, \_
              txt4, \_
              txt5, \_
              txt6})
      
          Dim rnd As New Random   'Random number generator
          For i As Int16 = 0 To lstListbox.Items.Count - 1
              'Pick a random textbox
              Dim index As Integer = rnd.Next(0, itemList.Count)
              itemList(index).Text = lstListbox.Items(i).ToString
              'Remove the textbox so we can't pick it again
              itemList.RemoveAt(index)
          Next
      End Sub
      

      You can write a small routine that dynamically adds each textbox to the list instead of hardcoding them in there, but if you know you'll just want to popullate these 6 you don't have to go to the trouble.

      C 1 Reply Last reply
      0
      • T TwoFaced

        You'll need to add each textbox to a list and then randomly select which one you want. Once you pick the textbox you set the text and remove it from the list so it's not chosen again. This should work for you.

        Private Sub Button1\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            'This list will contain our textboxes
            Dim itemList As New List(Of TextBox)
            'Add each textbox to the list
            itemList.AddRange(New TextBox() { \_
                txt1, \_
                txt2, \_
                txt3, \_
                txt4, \_
                txt5, \_
                txt6})
        
            Dim rnd As New Random   'Random number generator
            For i As Int16 = 0 To lstListbox.Items.Count - 1
                'Pick a random textbox
                Dim index As Integer = rnd.Next(0, itemList.Count)
                itemList(index).Text = lstListbox.Items(i).ToString
                'Remove the textbox so we can't pick it again
                itemList.RemoveAt(index)
            Next
        End Sub
        

        You can write a small routine that dynamically adds each textbox to the list instead of hardcoding them in there, but if you know you'll just want to popullate these 6 you don't have to go to the trouble.

        C Offline
        C Offline
        China Gary
        wrote on last edited by
        #3

        Brilliant, it works a charm! Thanks a million TwoFaced! :)

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups