I have 10 checkboxes in my application .The checkbox names are
checkbox0,
checkbox1,
checkbox2,
.
.
checkbox9
i want to select randomly which check开发者_StackOverflow社区box . Ie if i select checkbox0 for first time ,next time it should select say checkbox7 .
Put them in a list (List<CheckBox>
), create a new Random
and select by index:
var random = new Random();
var checkbox = list[random.Next(0, list.Length)];
(To prevent repeated selections you could remove the chosen control from the list afterwards (list.Remove(checkbox)
))
System.Random rand = new System.Random();
int x = rand.Next(0,10); //limiting the number from 0 to 9
if(x == 0)
checkbox0.checked = true;
else if (x == 1)
checkbox1.checked = true;
... and so on
This is my first time on stack overflow so if its not what you were looking for I'm sorry.
精彩评论