Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 随机更改按钮名称_C#_Button - Fatal编程技术网

C# 随机更改按钮名称

C# 随机更改按钮名称,c#,button,C#,Button,我有4个按钮btn1btn2btn3btn4 q1 = from tAns in db.Questions where tAns.idQuestion == x select tAns; q2 = from fAns1 in db.Questions where fAns1.idQuestion == x select fAns1; q3 = from fAns2 in db.Questions where fAns2.idQuestion == x select fAns2; q4 = fro

我有4个按钮
btn1
btn2
btn3
btn4

q1 = from tAns in db.Questions where tAns.idQuestion == x select tAns;
q2 = from fAns1 in db.Questions where fAns1.idQuestion == x select fAns1;
q3 = from fAns2 in db.Questions where fAns2.idQuestion == x select fAns2;
q4 = from fAns3 in db.Questions where fAns3.idQuestion == x select fAns3;
我需要一种方法,它随机改变按钮的文本。。。 可能的产出:

第一次

btn1.Text = q2.ToString();
btn2.Text = q4.ToString();
btn3.Text = q3.ToString();
btn4.Text = q1.ToString();
第二次

btn1.Text = q1.ToString();
btn2.Text = q3.ToString();
btn3.Text = q2.ToString();
btn4.Text = q4.ToString();

等等。

我猜您正在创建一个类似的测验应用程序,所以您只需要选择一个随机答案一次。不要从数组中拾取字符串,只需将所有字符串扔到一个数组中并洗牌该数组即可。洗牌数组的一种快速而简单的方法是使用

然后在
for
循环中遍历按钮,并将值分配给按钮。(您需要添加错误检查)

for(int i=0;i
非常感谢您,这正是我所需要的
for(int i = 0; i < myShuffledArray.Count(); i++)
     myButtonsCollection[i].Text = myShuffledArray[i];