开发者

Get Checkbox Randomly

开发者 https://www.devze.com 2023-03-01 00:39 出处:网络
I have 10 checkboxes in my application .The checkbox names are checkbox0, checkbox1, checkbox2, . . checkbox9

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号