2013-11-20 165 views
0

這是Windows窗體應用程序C# - 讓4個單選按鈕更改按鈕位置點擊

該應用程序是這樣的:

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]; 
    } 
} 

但是,正如你所看到的,這只是慢騰騰的單選按鈕的文字,我不希望按鈕來改變,因爲它是一個測驗,我創造的,我不想讓例子回答無線電按鈕的頂部。

回答

1

也許你應該看起來更到您的組件的位置,而不是隻是文本屬性

編輯的;

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: 上面的代碼將隨機雙方你單選按鈕爲您的文本。

  • RB1 = 1
  • RB3 = 2
  • RB2 = 4
  • RB4 = 3
0

可以在按鈕的點擊創建動態單選按鈕,並動態地在母控制添加控制。

您還可以將表格佈局控制放在單選按鈕上方,並根據隨機生成的數字更改單元格位置。

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];