GroupBox groupBox1 = new GroupBox(); groupBox1.Text = "Select an option"; RadioButton radioButton1 = new RadioButton(); radioButton1.Text = "Option 1"; radioButton1.CheckedChanged += (sender, e) => { if (radioButton1.Checked) MessageBox.Show("Option 1 selected"); }; RadioButton radioButton2 = new RadioButton(); radioButton2.Text = "Option 2"; radioButton2.CheckedChanged += (sender, e) => { if (radioButton2.Checked) MessageBox.Show("Option 2 selected"); }; groupBox1.Controls.Add(radioButton1); groupBox1.Controls.Add(radioButton2);
GroupBox groupBox1 = new GroupBox(); groupBox1.Text = "Group 1"; CheckBox checkBox1 = new CheckBox(); checkBox1.Text = "Option 1"; checkBox1.CheckedChanged += (sender, e) => { label1.Text = $"Group 1: Option 1 {(checkBox1.Checked ? "checked" : "unchecked")}"; }; CheckBox checkBox2 = new CheckBox(); checkBox2.Text = "Option 2"; checkBox2.CheckedChanged += (sender, e) => { label2.Text = $"Group 1: Option 2 {(checkBox2.Checked ? "checked" : "unchecked")}"; }; groupBox1.Controls.Add(checkBox1); groupBox1.Controls.Add(checkBox2); GroupBox groupBox2 = new GroupBox(); groupBox2.Text = "Group 2"; CheckBox checkBox3 = new CheckBox(); checkBox3.Text = "Option 3"; checkBox3.CheckedChanged += (sender, e) => { label1.Text = $"Group 2: Option 3 {(checkBox3.Checked ? "checked" : "unchecked")}"; }; CheckBox checkBox4 = new CheckBox(); checkBox4.Text = "Option 4"; checkBox4.CheckedChanged += (sender, e) => { label2.Text = $"Group 2: Option 4 {(checkBox4.Checked ? "checked" : "unchecked")}"; }; groupBox2.Controls.Add(checkBox3); groupBox2.Controls.Add(checkBox4); Label label1 = new Label(); label1.Text = "No options selected in Group 1"; Label label2 = new Label(); label2.Text = "No options selected in Group 2"; Controls.Add(groupBox1); Controls.Add(groupBox2); Controls.Add(label1); Controls.Add(label2);These examples use the System.Windows.Forms library, which is included in the .NET Framework.