Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Windows Form radio button does not check and uncheck

Posted on 2009-07-15
14
Medium Priority
?
1,407 Views
Last Modified: 2013-12-17
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

0
Comment
Question by:Edward Joell
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +4
14 Comments
 
LVL 11

Expert Comment

by:William
ID: 24863071
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.
0
 
LVL 8

Expert Comment

by:RyanAndres
ID: 24863140
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.
0
 
LVL 20

Expert Comment

by:MightySW
ID: 24863152
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?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 20

Expert Comment

by:MightySW
ID: 24863159
Also, this is not necessary:  if (rbDefaultPort.Checked == true)

You can just say: if (rbDefaultPort.Checked)
0
 

Author Comment

by:Edward Joell
ID: 24864146
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.




0
 
LVL 27

Expert Comment

by:Dabas
ID: 24920903
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
0
 
LVL 49

Accepted Solution

by:
DanRollins earned 500 total points
ID: 24921382
Some things to try:
* Turn off the AutoCheck property (this.rbDefaultPort.AutoCheck= false)
* Set the radiobutton into a Group that contains only the one item.
* Normalize the form so that it uses two radiobuttons:
    (o) Standard Handling
    (   ) Use Custom Port.   Port Number: [ ____________]
    If you do this, leave AutoCheck on

0
 
LVL 14

Expert Comment

by:ptakja
ID: 24925007
Is it possible that the Page_Load event is clearing the checkbox on postback?
0
 
LVL 27

Expert Comment

by:Dabas
ID: 24925114
ptakja:

The question states this is a Windows Forms application.

Dabas
0
 
LVL 14

Expert Comment

by:ptakja
ID: 24925145
Dabas,
 
True that. I saw the term "post back" in the write up and assumed web. My bad.
0
 
LVL 14

Expert Comment

by:ptakja
ID: 24925172
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.
0
 

Author Comment

by:Edward Joell
ID: 24925350
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.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question