PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
4.0 [RESOLVED] how to randomize the items inside the listbox-VBForums
Results 1 to 12 of 12

Thread: [RESOLVED] how to randomize the items inside the listbox

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2018
    Posts
    69

    Resolved [RESOLVED] how to randomize the items inside the listbox

    Hi Sir,

    I am making a program looks like a game while learning Thai language . in a label i will show a Thai word 1 x 1 in random . then beside it, a labels that has English words. so users will click which one is the correct English words for that Thai word in the middle . if it is correct the English word will disappear and the Thai word will show next Thai word using random class. I want to get words in a list box .and when user get the correct answer that word in listbox will be deleted. so that it will not be shown again.

    Name:  Capture.jpg
Views: 354
Size:  16.9 KB
    as of now i can show the words i want in a list box.
    I also know how to make random string using array.
    and i know now how to make the English word to disappear if it is correct.

    Code:
            void rGenerator()
            {
                
                string[] thaiRandomWords = { "POM/CHAN", "RAO", "KHUN" };
                Random r = new Random();
                int Question = r.Next(0, 3);
                lblThaiQ.Text = thaiRandomWords[Question];
    
                
            }
    
            private void lblI_Me_Click(object sender, EventArgs e)
            {
                if (lblThaiQ.Text == "POM/CHAN")
                {
                    lblI_Me.Hide();
                    rGenerator();
                }
            }
    
            private void lblYou_Click(object sender, EventArgs e)
            {
                if (lblThaiQ.Text == "KHUN")
                {
                    lblYou.Hide();
                    rGenerator();
                }
            }
    
            private void lblWe_Click(object sender, EventArgs e)
            {
                if (lblThaiQ.Text == "RAO")    
                    lblWe.Hide();
                    rGenerator();
                
            }
            //SHOW THAI WORDS 
            private void button1_Click(object sender, EventArgs e)
            {
               string[] words = new string [3];
               words[0] = "KHUN";
               words[1] = "POM/CHAN";
               words[2] = "RAO";
                listBox1.Items.Add(words[0]);
                listBox1.Items.Add(words[1]);
                listBox1.Items.Add(words[2]);
            }
    I want to
    1. select random items in listbox .
    2. delete item in listbox if correct match.now i can hide label only.

    thank you in advance.

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    99,788

    Re: how to randomize the items inside the listbox

    This will get a random index into a ListBox:
    csharp Code:
    1. var index = myRandom.Next(0, myListBox.Items.Count);
    You can then do whatever you want with that, e.g. get the item at that index.
    Last edited by jmcilhinney; Aug 31st, 2018 at 07:55 AM. Reason: Replaced VB code with C#.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Mar 2018
    Posts
    69

    Re: how to randomize the items inside the listbox

    hi Sir,

    it only shows the numbers 0,1,2 etc . i want to show the items inside the listbox. the string.
    Code:
    
            //SHOW THAI WORDS in list box
            private void button1_Click(object sender, EventArgs e)
            {
                string[] words = new string[3];
                words[0] = "KHUN";
                words[1] = "POM/CHAN";
                words[2] = "RAO";
                listBox1.Items.Add(words[0]);
                listBox1.Items.Add(words[1]);
                listBox1.Items.Add(words[2]);
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                Random r = new Random();
                var index = r.Next(0, listBox1.Items.Count);
              textBox1.Text = Convert.ToString(index); 
            }

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Mar 2018
    Posts
    69

    Re: how to randomize the items inside the listbox

    hi Sir,

    it only shows the numbers 0,1,2 etc . i want to show the items inside the listbox. the string.
    Code:
    
            //SHOW THAI WORDS in list box
            private void button1_Click(object sender, EventArgs e)
            {
                string[] words = new string[3];
                words[0] = "KHUN";
                words[1] = "POM/CHAN";
                words[2] = "RAO";
                listBox1.Items.Add(words[0]);
                listBox1.Items.Add(words[1]);
                listBox1.Items.Add(words[2]);
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                Random r = new Random();
                var index = r.Next(0, listBox1.Items.Count);
              textBox1.Text = Convert.ToString(index); 
            }

  5. #5
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    99,788

    Re: how to randomize the items inside the listbox

    Perhaps you should read replies carefully instead of just copying the code. I shouldn't have to waste my time repeating what I've already told you.
    Quote Originally Posted by jmcilhinney View Post
    You can then do whatever you want with that, e.g. get the item at that index.
    Where in your code are you getting the item at that index? If you're not doing that, why would you expect that item to appear by magic?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Mar 2018
    Posts
    69

    Re: how to randomize the items inside the listbox

    I tried this too but have underline in Items..error cannot be used like a methods.

    Code:
      textBox1.Text = listBox1.Items(index);

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Mar 2018
    Posts
    69

    Re: how to randomize the items inside the listbox

    I tried this too but have underline in Items..error cannot be used like a methods.

    Code:
      textBox1.Text = listBox1.Items(index);

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Mar 2018
    Posts
    69

    Re: how to randomize the items inside the listbox

    I tried this still not work.

    Code:
              textBox1.Text = listBox1.GetItemText(index).ToString();

  9. #9
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    99,788

    Re: how to randomize the items inside the listbox

    You don't index lists in C# with parentheses like you do in VB, but rather with brackets. You already know that because you're already doing it in the code you posted:
    csharp Code:
    1. lblThaiQ.Text = thaiRandomWords[Question];
    Did you forget?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  10. #10
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    99,788

    Re: how to randomize the items inside the listbox

    Quote Originally Posted by BONITO View Post
    I tried this still not work.

    Code:
              textBox1.Text = listBox1.GetItemText(index).ToString();
    Why would you expect that to work? GetItemText expects you to pass an item from the ListBox? Is 'index' and item from the ListBox?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Mar 2018
    Posts
    69

    Re: how to randomize the items inside the listbox

    aww Yes I forgot..
    thanks a lot!

    Code:
       lblThaiQ.Text = listBox1.Items[index].ToString();

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Mar 2018
    Posts
    69

    Re: how to randomize the items inside the listbox

    I tried this still not work.

    Code:
              textBox1.Text = listBox1.GetItemText(index).ToString();

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width