lundi 23 juillet 2018

How to randomly select 2 checkboxes out of 20 checkboxes side by side in c#? [on hold]

I want to randomly checked 2 checkboxes out of 20 checkboxes which are inside a panel. I could only randomly checked 1 checkbox and unable to check the checkbox next to it.

int startIndex = -1

void SetNodeMotion(int index, bool on)
    {
        int x = 1;
        foreach (CheckBox c in panel1.Controls
        {
         if (x == index)
            {
               if (c.Checked != on)
                {
                    c.Checked = on;
                }
                break;
            } 
            x += 1;
        }
    }
if (startIndex == -1)
{     
   Random rd = new Random();
   startIndex = rd.Next(1, 20);
}

else
{
       SetNodeMotion(startIndex, false);
       if (++startIndex == 21)
       {
           startIndex = 1;
       }
}
        SetNodeMotion(startIndex, true);

This is what I had done so far.

I also need the checkboxes to uncheck as after implementing the timer function. (eg. cb1 and cb2 are currently checked, 1 sec later to check cb3 and uncheck cb1)

Is there a good way to go about doing this? just started programming only and I am still learning.




Aucun commentaire:

Enregistrer un commentaire