Quantcast
Channel: Windows Forms Data Controls and Databinding forum
Viewing all articles
Browse latest Browse all 2535

C# Select Random ListBox Item, Random Not Random-ing

$
0
0

private void whatever()
        {
            Random rnd2 = new Random();

            int i = listBox1.Items.Count - 1;

            int chosenItem = rnd2.Next(i);

            listBox1.SelectedIndex = i;


            label1.Text = "Choices Are: " + listBox1.SelectedItem.ToString();
        }

For whatever reason this always selects the same item from the listbox, and I'm not an expert at randoms yet.  Right now it's called from a button.click(), only works once per load, and as stated, always picks the last item in the listbox.  The above is what I managed to convert from a VB article on it.

Can someone help me mod this up a bit?  I want it to pick a random item from the list every time I click the button.  There are some articles on it, most of them far too cluttered to be any help, and most of them not quite this.

Thanks in advance!


May the fleas of a thousand camels feast happily on the lower regions of your enemies. And may their arms be too short to scratch!


Viewing all articles
Browse latest Browse all 2535

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>