这是在 Windows窗体应用程序
中应用程序如下所示:
RADIO1 text = 1
RADIO2 text = 2
RADIO3 text = 3
RADIO4 text = 3
当我按下名为button1
的按钮时,它会像这样随机出现:
RADIO2 text = 2
RADIO1 text = 1
RADIO4 text = 4
RADIO3 text = 3
我尝试过这样的事情:
List<string> list = new List<string>{"1","2","3","4"};
public void ShuffleText()
{
var rand = new Random();
var shuffledText = list.OrderBy(x=>rand.Next(list.Count)).ToList();
var radioButtons = new[]{radioButton1,radioButton2, radioButton3, radioButton4};
for(int i = 0; i < radioButtons.Length;i++)
{
radioButtons[i].Text = shuffledText[i];
}
}
但是,正如你所看到的,这只会改变radioButton的文本,我不希望按钮改变,因为它是我正在创建的测验,我不希望示例答案在顶部radioButtons一直都在。
答案 0 :(得分:1)
也许您应该更多地查看组件的位置,而不仅仅是文本属性
EDIT;
List<string> list = new List<string>{"1","2","3","4"};
List<int> Locations new List<int>{100,200,300,400};
var r1 = new RadioButton();
var r2 = new Radiobutton();
var r3 = new RadioButton();
var r4 = new RadioButton();
var buttons = new[]{r1,r2,r3,r4};
var rand = new Random();
var shuffledText = list.OrderBy(x=>rand.Next(list.Count)).ToList();
var ShuffledButtons = Locations.OrderBy(x=> rand.Next(locations.Count)).ToList();
for(int i = 0; i < radioButtons.Length;i++)
{
radioButtons[i].Text = shuffledText[i];
radioButtons[i].Location.Y = shuffledButtons[i];
radioButtons[i].Location.X = 50;
}
EDIT2: 前面的代码会将您的radiobutton随机化为您的文本。
答案 1 :(得分:0)
您可以在单击按钮上创建动态单选按钮,并在父控件中动态添加控件。
您还可以将表格布局控制放在单选按钮上方,并根据随机生成的数字更改单元格位置。
答案 2 :(得分:0)
List<Point> list = new List<Point>{radioButton1.Location,radioButton2.Location,radioButton3.Location,radioButton4.Location};
// Rest of the code remains the same
var shuffledLocation = list.OrderBy(x=>rand.Next(list.Count)).ToList();
for(int i = 0; i < radioButtons.Length;i++)
radioButtons[i].Location= shuffledLocation[i];