すべてのラジオボタンを同じイベントに登録するだけです。次に、各ボタンのコードを重複させるのではなく、チェックされ、それに応じて行動することができます。
以下は、チェックボックスが表示されるテキストボックスのText
プロパティを設定する簡単な例です。 .designer.csで
Formクラス
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void radioButtons_CheckedChanged(object sender, EventArgs e)
{
//Do whatever you need to do here. I'm simple setting some text based off
//the name of the checked radio button.
System.Windows.Forms.RadioButton rb = (sender as System.Windows.Forms.RadioButton);
textBox1.Text = $"{rb.Name} is checked!";
}
}
は
//Note that the EventHandler for each is the same.
this.radioButton3.CheckedChanged += new System.EventHandler(this.radioButtons_CheckedChanged);
this.radioButton2.CheckedChanged += new System.EventHandler(this.radioButtons_CheckedChanged);
this.radioButton1.CheckedChanged += new System.EventHandler(this.radioButtons_CheckedChanged);
ファイルと同じ 'CheckedChanged'イベントにすべてのラジオボタンを購読します。 –
@DangerZoneこの例を挙げてもらえますか? p.s.一度に複数のラジオボタンを選択することができます。ありがとうございました! – TheBiz
複数のグループを同時に選択できる場合は、同じグループボックスにグループを含めないでください。代わりにチェックボックスを使用してください。 –