2014-04-14 34 views

回答

2

事實上,單選按鈕只有在同一父級內時才相互排斥。據說我可以考慮兩種選擇。

  1. 讓他們在同一個父,處理Paint事件父並得出自己手動給人的印象,他們是不同的組。
  2. 你必須手動管理互斥:(

單選按鈕都沒有魔法,他們的其他電臺的的Checked酒店僅設置爲false幕後。

private List<RadioButton> radioButtons = new List<RadioButton>(); 
//Populate this with radios interested, then call HookUpEvents and that should work 

private void HookUpEvents() 
{ 
    foreach(var radio in radioButtons) 
    { 
     radio.CheckedChanged -= PerformMutualExclusion; 
     radio.CheckedChanged += PerformMutualExclusion; 
    } 
} 

private void PerformMutualExclusion(object sender, EventArgs e) 
{ 
    Radio senderRadio = (RadioButton)sender; 
    if(!senderRadio.Checked) 
    { 
     return; 
    } 
    foreach(var radio in radioButtons) 
    { 
     if(radio == sender || !radio.Checked) 
     { 
      continue; 
     } 
     radio.Checked = false; 
    } 
} 
+0

這似乎是通用的,很容易對於不同的應用程序可以重用,我可能會考慮將它與某種屬性結合使用,謝謝! –

0

一下這個問題的談判:

VB.NET Group Radio Buttons across different Panels

您可以處理CheckedChanged事件的單選按鈕來完成你所需要的:

public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 
     this.radioButton1.CheckedChanged += new System.EventHandler(this.HandleCheckedChanged); 
     this.radioButton2.CheckedChanged += new System.EventHandler(this.HandleCheckedChanged); 
     this.radioButton3.CheckedChanged += new System.EventHandler(this.HandleCheckedChanged); 
    } 

    private bool changing = false; 

    private void HandleCheckedChanged(object sender, EventArgs e) 
    { 
     if (!changing) 
     { 
      changing = true; 

      if (sender == this.radioButton1) 
      { 
       this.radioButton2.Checked = !this.radioButton1.Checked; 
       this.radioButton3.Checked = !this.radioButton1.Checked; 
      } 
      else if (sender == this.radioButton2) 
      { 
       this.radioButton1.Checked = !this.radioButton2.Checked; 
       this.radioButton3.Checked = !this.radioButton2.Checked; 
      } 
      else if (sender == this.radioButton3) 
      { 
       this.radioButton1.Checked = !this.radioButton3.Checked; 
       this.radioButton2.Checked = !this.radioButton3.Checked; 
      } 

      changing = false; 
     } 
    } 
} 
相關問題