How to Disable radio button in listview

13,325

I assume you're using Windows Forms. Put all the RadioButtons in a container, like a Panel or in a GroupBox. Then, all your RadioButtons will be grouped together. Everything is explained on this MSDN page.

Once you completed this task, iterate through the controls of this group, and disable each one, with a pseudo-code like this:

foreach (RadioButton rButton in groupBox) {
                rButton.Enabled = this.Enabled;
}

Or, simply type this code:

groupBox.Enabled = false;

to disable the groupbox and all the controls in it.

Share:
13,325
Ammar Ali
Author by

Ammar Ali

Very noob to programing, I'm here to ask questions, still not qualified for answering (maybe after one year).

Updated on June 04, 2022

Comments

  • Ammar Ali
    Ammar Ali almost 2 years

    I'm a very noob and this is my first post guys, so your help is needed trying to disable all of radio buttons controls in listview when I check a checkbox and it is working for one row only.

    Here is my code:

    protected void chkb_DG_NO_CheckedChanged(object sender, EventArgs e)
            {
    
    
                        foreach (ListViewItem item in ListView1.Items)
                        {
                            RadioButton RD1 = (RadioButton)ListView1.Items[0].FindControl("R1");
                            RadioButton RD2 = (RadioButton)ListView1.Items[0].FindControl("R2");
                            RadioButton RD3 = (RadioButton)ListView1.Items[0].FindControl("R3");
                            RadioButton RD4 = (RadioButton)ListView1.Items[0].FindControl("R4");
                            RadioButton LD1 = (RadioButton)ListView1.Items[0].FindControl("L1");
                            RadioButton LD2 = (RadioButton)ListView1.Items[0].FindControl("L2");
                            RadioButton LD3 = (RadioButton)ListView1.Items[0].FindControl("L3");
                            RadioButton LD4 = (RadioButton)ListView1.Items[0].FindControl("L4");
                            RD1.Enabled = false;
                            RD2.Enabled = false;
                            RD3.Enabled = false;
                            RD4.Enabled = false;
                            LD1.Enabled = false;
                            LD2.Enabled = false;
                            LD3.Enabled = false;
                            LD4.Enabled = false;
                        }
    
            }
    

    What should I change?

  • Antagony
    Antagony over 11 years
    Why not just disable the GroupBox?
  • Ammar Ali
    Ammar Ali over 11 years
    I'm using asp.net I wanted to repeat a region that has 2 group of radio boxes, I did not use the repeater control, instead I used the list view control, and it worked perfectly, now I have a rows each one with 8 radio buttons (2 groups) and a check box, I want to write command that disable all of the row control including the radio buttons, and as I said... with the above code I can only disable one row, when I check the second row check box the first row's radio buttons gets disabled. I know the cause of this, it is because I'm using [0] in the code which apply to the first row only.
  • Alberto Solano
    Alberto Solano over 11 years
    @user1503894 I don't understand why you didn't add these info on your question. Maybe I would have answered better.
  • Alberto Solano
    Alberto Solano over 11 years
    Anyway, I don't understand the reason of a for-each loop in your code, without using the generic "item". I don't understand very well the description of your interface, but why don't you try to select each row and disable each group of this row?