0

Good day! I would like to have help with my simple code from our programming project. My problem is that I can't find a solution on how to randomize text items in the listbox. The items are read from my MS Access database. Here is some of my code.

Com.CommandText = "SELECT * FROM search";
OleDbDataReader reader = Com.ExecuteReader();
object[] obj = new object[256];

while (reader.Read())
{
    reader.GetValues(obj);

    if (label1.Text == obj[0].ToString())
    {
        listBox1.Items.Add(reader.GetString(1));
        listBox1.Items.Add(reader.GetString(2));
        listBox1.Items.Add(reader.GetString(3));
        listBox1.Items.Add(reader.GetString(4));
    }
}

reader.Close();
conn.Close();

Random random = new Random();
string[] words = { listBox1.Items.ToString() };
listBox4.Items.Add(words[random.Next(0, words.Length)]);

And it won't work.

Abbas
  • 14,186
  • 6
  • 41
  • 72
  • What do you mean "it won't work"? What error do you get? What do you mean by "random text"? – Matt Feb 05 '14 at 14:53

1 Answers1

1

Assuming you want the items in the listbox in random order you can read the values into a list of strings, randomize them, and then add them to the listbox. Using the Shuffle-method in the answer to: Randomize a List<T> it would look something like:

Com.CommandText = "SELECT * FROM search";
OleDbDataReader reader = Com.ExecuteReader();
var words = new List<string>();

while (reader.Read())
{
    if (string.Compare(label1.Text, reader.GetString(0)) == 0)
    {
        for (int i = 1 ; i < 5 ; i++)
        {
            words.Add(reader.GetString(i));
        }
    }
}
reader.Close();
conn.Close();

listbox1.Items.AddRange(words.ToArray());
words.Shuffle();
listbox4.Items.AddRange(words.ToArray());

Add the following class to your system (using your namespace):

public static class ListExtensions
{
    public static void Shuffle<T>(this IList<T> list)  
    {  
        Random rng = new Random();  
        int n = list.Count;  
        while (n > 1) {  
            n--;  
            int k = rng.Next(n + 1);  
            T value = list[k];  
            list[k] = list[n];  
            list[n] = value;  
        }  
    }
}
Community
  • 1
  • 1
Fredrik Ljung
  • 1,445
  • 13
  • 28
  • sir thanks for the comment but sir i have an error here: listBox1.Items.AddRange(words.ToArray()); words.Shuffle(); listBox4.Items.AddRange(words.ToArray()); it says that it does not contain a definition for "shuffle" I've try and try to solve this but i Can't. – Spidey Eudz Aragon Feb 06 '14 at 07:25
  • You have to include the function Shuffle from the link http://stackoverflow.com/questions/273313/randomize-a-listt-in-c-sharp. It's an extension method so if you copy paste it make sure you put it in a static class that is accessible from your code. – Fredrik Ljung Feb 06 '14 at 07:39
  • In my system sir is requires another class so that the items in my listbox will shuffled randomly. Sir can you give me a codes sir on how to add class sir? named Shuffle? sorry for the disturbance sir :) – Spidey Eudz Aragon Feb 06 '14 at 08:00
  • I Added the extension class and method to the answer. – Fredrik Ljung Feb 06 '14 at 08:09
  • sorry sir hehehe i didn't notice it, cause I'm focusing in my main codes. thanks again sir you helped me a lot.. – Spidey Eudz Aragon Feb 06 '14 at 08:17
  • No problem. If you feel this is the answer to your question, please mark it 'accepted' by clicking the green check mark. :) – Fredrik Ljung Feb 06 '14 at 09:08