Link to home
Start Free TrialLog in
Avatar of Edward Joell
Edward JoellFlag for United States of America

asked on

Windows Form radio button does not check and uncheck

This is probably pretty simple, but I have been searching for several hours on line for an answer to this and haven't found one.  I have a windows form.  On it is a radio button that the user checks when using the default property and unchecks when they have to enter the value of that property manually.  Checking the radio button disables the texbox and the post back populates the textbox with the default value.  So far pretty simple.  To start with I set the design-time "checked" property = to "true."  In the form's constructor I read the value of the checked property of the radio button and base on that value either filled the textbox with the default value and disabled it, or cleared the value in the textbox and left it enabled.  I also had an auto-load method that would read the values from an array and stored them in the form.  If the value in the array for this text box equaled the default then the radio button was set to checked.However in testing, I was never able to get the radio button to uncheck. I implemented an onclick event handler that would set the checked property equal to the the opposite of the curent value of that property.  This wourked fine while unchecking, but di not work at all for checking.  I tried setting this property to false in the design time properties and only setting to it to true in the constructor.  Unable to uncheck.  I do not understand the reasons for this behavior.  I checked the internet but did not find anyone else experiencing this.  Anyone have any ideas?
private void rbDefaultPort_Click(object sender, EventArgs e)
   {
       this.rbDefaultPort.Checked = !(this.rbDefaultPort.Checked);
   }
 
if (servDataRetrieveProp[5] == SimlUtil.SimlPort.ToString())
 {
         this.rbDefaultPort.Checked = true;
         this.txtPort.Enabled = false;
         this.txtPort.Text = SimlUtil.SimlPort.ToString();
  }
 else
 {
        this.rbDefaultPort.Checked = false;
        this.txtPort.Enabled = true;
        this.txtPort.Text = servDataRetrieveProp[5];
}
 
        private void rbDefaultPort_CheckedChanged(object sender, EventArgs e)
        {
            if (rbDefaultPort.Checked == true)
            {
                txtPort.Text = SimlUtil.SimlPort.ToString();
                txtPort.Enabled = false;
            }
            else
            {
                txtPort.Enabled = true;
            }
        }

Open in new window

Avatar of William Domenz
William Domenz
Flag of United States of America image

OK - did not read all the way...  sounds like you are looking for a checkbox.  radio buttons are only unchecked if another radio buton is checked or you have to do  it programmtically.
 
Place 2 or three radio buttons on a form and launch the form. click one then another and see the behavior.
use the CheckBox component for 2 to 3 states. Radio buttons will uncheck if it is part of a group of more than one RadioButton.
Hi, use the .equals method for objects:

You have: if (servDataRetrieveProp[5] == SimlUtil.SimlPort.ToString())

this is not a boolean.  You need to use if (servDataRetrieveProp[5].equals(SimlUtil.SimlPort.ToString()))

Are you certain that 5 contains the exact string that should be compared to SimlPort method output?
Also, this is not necessary:  if (rbDefaultPort.Checked == true)

You can just say: if (rbDefaultPort.Checked)
Avatar of Edward Joell

ASKER

Billy DVD that's alright, it is a common error I've noticed on this site.  I am familiar with the behavior of multiple radio buttons having used them since they were control arrays in VB6 and identically named elements on web pages,and also remembering the car radio buttons which they emulate.   The customer specified a radio button to indicate whether the default port was being accepted or not. Sure it is easy and straight forward to utilize a checkbox at this location but that is not what is desired.

But why does the click event code "this.rbDefaultPort.Checked = !(this.rbDefaultPort.Checked);" work perfectly to uncheck the button but is a dismal failure at checking it?

I seem to remember about 4- 5 years ago in ASP.net 1.1 having to set autopostback  to be true for either a checkbox or a radiobutton and indicating in that onclick event handler that
"yada.Checked = Not(yada.Checked)"  
If this control requires multiple radio buttons to work properly, perhaps that is why I saw an example of a custom control on the web for a radio button that behaves like a checkbox.  So if everyone of the accord that it is not possible to make the built-in radio button behave like a checkbox, I will have to download that code and use it. Siiiiiiighhhhhhhh.

MightySW the part " if (servDataRetrieveProp[5] == SimlUtil.SimlPort.ToString()) ..."  works just fine.  That code was included to show why I want the checked property to change.  I am also aware that it is not necessary to show "== true"; it is also not incorrect to show it.  My preference.




I agree with the first comment.

Just add another radio button and group it with your first one.
Hide the second radio button.

Whenever you want to 'uncheck' the visible radiobutton, just select the other one programmatically.

Dabas
ASKER CERTIFIED SOLUTION
Avatar of DanRollins
DanRollins
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Is it possible that the Page_Load event is clearing the checkbox on postback?
ptakja:

The question states this is a Windows Forms application.

Dabas
Dabas,
 
True that. I saw the term "post back" in the write up and assumed web. My bad.
Radio buttons are mutually exclusive. You can't just have one and have it function like a checkbox. So your solution is to either add a 2nd radio button and a Group box to hold them, or replace the radio button with a checkbox.
radiobutton.autocheck = false; did the trick.  All the other parts of the code worked fine once I turned off that property.  I wonder why that property is not listed in the property page.  Based on my ASP 1.1 experience I figured there had to be something of the kind, but when i did not see it in property page I did not look elsewhere.  Should have checked members page in MSDN  next time I will know.  So Thank you Dan Rollins for TWO lessons.  Once again you save my bacon I should probably single you out each time I need help.