Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How do I have a check box grey out radio buttons. The code below works but when you uncheck the check box nothing happens

C#
private void CheckBoxChecked(object sender, RoutedEventArgs e)
         {

              if (CheckBox.IsChecked == true)
              {
                   acceptRadioButton.IsEnabled = false;
                   declineRadioButton.IsEnabled = false;
              }
              else
              {
                   acceptRadioButton.IsEnabled = true;
                   declineRadioButton.IsEnabled = true;
              }


         }
Posted
Comments
sagar wasule 15-May-12 1:11am    
Check out if you have set any default property for the check box (IsChecked Property)
deeptibansal 15-May-12 1:23am    
Can you please post the aspx code so that it can be checked that which event have you set for doing this.... This code should be written in CheckedChange event of checkbox.
Siddiqui's 15-May-12 1:26am    
Use CheckedChanged event instead of CheckBoxChecked.
Vipin_Arora 15-May-12 1:56am    
are you using Winforms or ASP.NET page?
preet88 15-May-12 2:33am    
instead of CheckBoxChecked event you can try CheckBox_CheckedChanged Event

Adding to the solution provided by "Code 89", I would use this code:
C#
protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
  RadioButton1.Enabled = !CheckBox1.Checked;
  RadioButton2.Enabled = !CheckBox1.Checked;
}
 
Share this answer
 
Comments
VJ Reddy 15-May-12 2:27am    
Good user of CheckBox1.Checked. 5!
Code 89 15-May-12 2:34am    
Good!
Maciej Los 15-May-12 10:20am    
+5!
I think you may be using WPF as the parameter e in the method signature is of type RoutedEventArgs. If so, then the CheckBox control of WPF has got Checked and Unchecked events as explained here.
WPF CheckBox Events[^]

You are handling only the Checked event of the CheckBox, hence desired action takes place when the CheckBox is checked and nothing happens when the CheckBox is unchecked as the Unchecked event is not handled.

The Checked and Unchecked can be used as follows
C#
void chkbox_Unchecked(object sender, RoutedEventArgs e)
{
   acceptRadioButton.IsEnabled = true;
   declineRadioButton.IsEnabled = true;
}

void chkbox_Checked(object sender, RoutedEventArgs e)
{
   acceptRadioButton.IsEnabled = false;
   declineRadioButton.IsEnabled = false;
}
 
Share this answer
 
Comments
Sandeep Mewara 15-May-12 10:34am    
Good answer. 5!
VJ Reddy 15-May-12 10:41am    
Thank you, Sandeep :)
Try this...

protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
   {
       if (CheckBox1.Checked== true)
       {
           RadioButton1.Enabled = false;
           RadioButton2.Enabled = false;
       }
       else
       {
           RadioButton1.Enabled = true;
           RadioButton2.Enabled = true;
       }
   }



XML
<div>
   <asp:CheckBox ID="CheckBox1" runat="server" OnCheckedChanged="CheckBox1_CheckedChanged" AutoPostBack="True" /><br />

  <asp:RadioButton ID="RadioButton1" runat="server" />
  <asp:RadioButton ID="RadioButton2" runat="server" />
      </div>
 
Share this answer
 
Comments
JF2015 15-May-12 2:35am    
Good answer! 4+
Check For Unchecked event of checkbox in the event list of check box
 
Share this answer
 
Comments
Andy411 15-May-12 1:37am    
That's the solution
Hi,

If you are using ASP.NET, then you have to set the
AutoPostBack
property of checkbox to
True
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900