There are several RadioButton and when choosing one of them you need to block the others. How can this be realized?
- Show what youβve already done and where you donβt work. - Vladimir Glinskikh
- If when you select one of them you block the rest, how will you choose another value later? - andreycha
- github.com/Drein211/β¦ that's what it is now - Yuriy Kosarikhin
2 answers
You should write a universal method, which, when selecting radioButton will be called for each radioButton , and inside the method we will change their states:
void radioButtons_CheckedChanged(object sender, EventArgs e) { RadioButton currentRadioButton = sender as RadioButton; // Π½Π° ΠΊΠΎΠ³ΠΎ ΠΊΠ»ΠΈΠΊΠ½ΡΠ»ΠΈ if (radioButton != null) { // ΠΏΡΠΎΠΉΠ΄Π΅ΠΌ ΠΏΠΎ Π²ΡΠ΅ΠΌ RadioButton Π½Π°ΡΠ΅ΠΉ ΡΠΎΡΠΌΡ ΠΈ ΠΏΠΎΠΌΠ΅Π½ΡΠ΅ΠΌ ΡΠΎΡΡΠΎΡΠ½ΠΈΠ΅ foreach(var radioButton in this.Controls.OfType<RadioButton>()) { radioButton.Enabled = false; // Π·Π°Π±Π»ΠΎΠΊΠΈΡΡΠ΅ΠΌ ΠΊΠ°ΠΆΠ΄ΡΠΉ } currentRadioButton.Enabled = true; // ΡΠ°Π·Π±Π»ΠΎΠΊΠΈΡΡΠ΅ΠΌ Π²ΡΠ±ΡΠ°Π½Π½ΡΠΉ } } Thus, after selecting one radioButton rest will be blocked. In order for this method to be called on all radioButton placed on the form, it is necessary in the properties of these components, each to specify the method that will be called for the CheckedChanged event β call the radioButtons_CheckedChanged method.
Good day. To deactivate (lock) the radio button, after selecting, you need to write a trace. code: the function is created automatically after clicking the radio button several times with the mouse
private void radioButton1_CheckedChanged(object sender, EventArgs e) { Enabled = false; //Π΄Π΅Π°ΠΊΡΠΈΠ²ΠΈΡΡΠ΅ΠΌ radio button } To deactivate other radio buttons, you need to deactivate their trace. in the following way:
private void radioButton1_CheckedChanged(object sender, EventArgs e) { Enabled = false; radioButton2.Enabled = false; // Π½Π°Π·Π²Π°Π½ΠΈΠ΅ radio button . Π΄Π΅Π°ΠΊΡΠΈΠ²Π°ΡΠΈΡ = Π΄Π΅Π°ΠΊΡΠΈΠ²ΠΈΡΠΎΠ²Π°ΡΡ radioButton3.Enabled = false; } in order to cut down the entire form, you just need to write a trace to the function. line:
Form1.ActiveForm.Dispose ();
- no: // deactivate the whole form - Igor
- 3stop, what does
Disposedo with it? - Igor

