-
Mar 10th, 2013, 07:29 AM
#1
Thread Starter
Lively Member
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.
-
Mar 10th, 2013, 08:30 AM
#2
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.
-
Mar 10th, 2013, 08:45 AM
#3
Thread Starter
Lively Member
Re: Random item select in Listview
Hey, thanks, it works!
One more question though, why is Rnd bad?
-
Mar 10th, 2013, 06:34 PM
#4
Re: Random item select in Listview
Originally Posted by Superthijs
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!
-
Mar 10th, 2013, 07:24 PM
#5
Re: Random item select in Listview
Originally Posted by Superthijs
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.
-
Mar 10th, 2013, 07:27 PM
#6
Re: Random item select in Listview
Originally Posted by dunfiddlin
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|