Solved

Windows Form radio button does not check and uncheck

Posted on 2009-07-15
14
1,352 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
  • 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
 
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 49

Accepted Solution

by:
DanRollins earned 125 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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

708 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now