CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Mar 2002
    Location
    United Kingdom
    Posts
    60

    Question How can I shuffle this listbox?

    Hi,

    I have a list box on my form which has 4 items in. I also have a buton (CmdShuffle). When I press the button I want the contents of the list box to shuffle around (i.e. change their position in the list).

    Any info on how I can do this?

    Thanks

    Mark

  2. #2
    Join Date
    Nov 2002
    Posts
    39
    There is no predefinite function to shuffle the content of a list box. Since you only have four items I don't think would be a problem if you were to remove all the items from the list box and then add them again in a different order. See ResetContent, rand, srand.
    Matters of great importance must be treated lightly, matters of small importance can be treated seriously.

  3. #3
    Join Date
    Mar 2002
    Location
    United Kingdom
    Posts
    60
    Any code for using ResetContent, Rand or Srand in this way...?

    Thanks,

    Mark.

  4. #4
    Join Date
    Sep 2001
    Location
    Québec, Canada
    Posts
    1,923
    The rand and srand are from C language, in VB, you'll have to use RND function. Look this example, it remove each elements (one after the others), and put re-insert them in a random position:

    Code:
    Private Sub Command1_Click()
        Dim strItem As String
         
        'Initialise random number generation
        Randomize Timer
         
        With List1
            'Browse each list elements
            For x = 0 To .ListCount - 1
                'Remove the element, we'll add it after
                strItem = .List(x)
                .RemoveItem x
                 
                'Add the removed element to a random position
                .AddItem strItem, Int(Rnd * .ListCount)
            Next x
        End With
    
    End Sub
    JeffB - hope it helps
    CodeGuru VB FAQ Visual Basic Frequently Asked Questions
    VB Code color Tool to color your VB code on CodeGuru
    Before you post Importants informations to know before posting

  5. #5
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Nice, JeffB

    5 stars for you
    Have happy coding,

    Cesare
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  6. #6
    Join Date
    Mar 2002
    Location
    United Kingdom
    Posts
    60
    Thanks! Code works fine.

    Mark

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured