【问题标题】:Change randomly the button names随机更改按钮名称
【发布时间】:2012-12-19 07:31:21
【问题描述】:

我有 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();

等等……

【问题讨论】:

    标签: c# button


    【解决方案1】:

    我猜您正在创建某种测验应用程序,因此您只需要能够随机选择一次答案。无需从数组中选择字符串,只需将所有字符串放入数组中并打乱数组即可。改组数组的一种快速简便的方法是使用Fisher-Yates Shuffle

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

    for(int i = 0; i < myShuffledArray.Count(); i++)
         myButtonsCollection[i].Text = myShuffledArray[i];
    

    【讨论】:

    • 谢谢你正是我所需要的
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-01
    • 2019-01-04
    • 2018-06-22
    相关资源
    最近更新 更多