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 2

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.

    Before deactivation after deactivation

    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
    • 3
      stop, what does Dispose do with it? - Igor