我的表格中有幾個單選按鈕應該全部連接,但我希望其中一個單獨的容器將其與一些其他相關輸入專門分組。這裏是一般看:有多種方法可以將Windows窗體中的單選按鈕分組嗎?
有沒有辦法來組與其他3第四單選按鈕,即使是在自己的組框?
我的表格中有幾個單選按鈕應該全部連接,但我希望其中一個單獨的容器將其與一些其他相關輸入專門分組。這裏是一般看:有多種方法可以將Windows窗體中的單選按鈕分組嗎?
有沒有辦法來組與其他3第四單選按鈕,即使是在自己的組框?
事實上,單選按鈕只有在同一父級內時才相互排斥。據說我可以考慮兩種選擇。
Paint
事件父並得出自己手動給人的印象,他們是不同的組。單選按鈕都沒有魔法,他們的其他電臺的的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;
}
}
他們在不同的容器,所以你不能。我覺得這是這個問題的欺騙:Radiobuttons as a group in different panels
一下這個問題的談判:
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;
}
}
}
這似乎是通用的,很容易對於不同的應用程序可以重用,我可能會考慮將它與某種屬性結合使用,謝謝! –