lundi 24 octobre 2016

How to randomly fill checkboxes in c# - Visual Studio

So this is my first question on stack overflow. I'm working on a drum sequencer and want to implement a button to randomly fill the 80 checkboxes that indicate a drum sound being triggered. Currently what I have only filles one box of the 80 randomly, but I want each of them to have a random chance of being filled. The first part of my code simply clears the current selection. Here is my attempt in the following code:

private void button4_Click(object sender, EventArgs e)
{
    List<CheckBox> Checkboxlist = new List<CheckBox>();
    foreach (CheckBox control in this.Controls.OfType<CheckBox>())
    {
        Checkboxlist.Add(control);
        control.Checked = false;
    }

    for (int i = 0; i <= 200; i++)
    {
        var random = new Random();
        var r = random.Next(0, Checkboxlist.Count);
        var checkbox = Checkboxlist[r];
            checkbox.Checked = true;
    }
}

Thank you for looking!




Aucun commentaire:

Enregistrer un commentaire