Results 1 to 6 of 6

Thread: Random item select in Listview

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Nov 2011
    Location
    The Netherlands
    Posts
    84

    Red face Random item select in Listview

    Hey!
    This is my code to select a random item in a listview:
    Code:
    Dim tmp As Integer
    tmp = Rnd() * ListView2.Items.Count - 1
    ListView2.Items(tmp).Selected = True
    ListView2.EnsureVisible(tmp)
    It works fine, but when I use the arrow keys to select another item, the selection jumps to a whole different place...
    Like, when I select item A manually, and then use that code to select random item B, and I press an arrow key, the listview jumps to item A again!
    Sorry if you don't understand because it's hard to explain.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Random item select in Listview

    First of all, don't use Rnd... ever! If you want to generate random numbers then use the Random class. Create one instance and then call Next each time you need a random number.

    As for the issue, using the arrow keys affects the focus, therefore it depends on the FocusedItem. When you select an item in code, that doesn't mean that that item has focus. You need to assign that item to the FocusedItem property too.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Nov 2011
    Location
    The Netherlands
    Posts
    84

    Re: Random item select in Listview

    Hey, thanks, it works!
    One more question though, why is Rnd bad?

  4. #4
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Random item select in Listview

    Quote Originally Posted by Superthijs View Post
    Hey, thanks, it works!
    One more question though, why is Rnd bad?
    Because it doesn't automatically reseed. So every time you run the program you will get exactly the same sequence of numbers!
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Random item select in Listview

    Quote Originally Posted by Superthijs View Post
    Hey, thanks, it works!
    One more question though, why is Rnd bad?
    Because it's a VB6 hangover and is ugly to use. It will still work if used properly but the Random class is a better option. Unfortunately there are a lot of people working as teachers or writing blogs or tutorials on the web who have VB6 experience and haven't bothered to update their own knowledge since migrating to VB.NET. Continuing to use what they're used to from VB6 is one thing but teaching newcomers the old, dodgy way to do things when there are better, .NET-specific ways is poor.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Random item select in Listview

    Quote Originally Posted by dunfiddlin View Post
    Because it doesn't automatically reseed. So every time you run the program you will get exactly the same sequence of numbers!
    If Rnd is used properly, i.e. the Randomize statement is used once per session before Rnd is called, then it's results will be the same* as those of the Random class because they both use the system time as a seed by default. It the usage, not the implementation that's the issue.

    *I'm not sure whether they produce exactly the same results but they produce equivalent results at least, i.e. results with the same degree of randomness.

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