Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

validating radiobutton in winform doesnt work

Posted on 2007-12-05
6
Medium Priority
?
1,424 Views
Last Modified: 2008-02-01
I followed this example to validate my 2 textboxes:
http://devcenter.infragistics.com/Articles/ArticleTemplate.Aspx?ArticleID=1263

I have 2 radiobuttons, I have them in a panel. User must select a radiobutton. Otherwise, they should get a msg like that example. Not sure why it doesnt work. I clicked on the "events" and added the event. My code is like this. I have an errorProvider. I even set the debugger, clicked OK and the validating event didnt trigger...


private void rbIBM_Validating(object sender, CancelEventArgs e)
        {
            if (!rbSQL.Checked & !rbIBM.Checked)
            {
                errorProvider1.SetError((Control)sender, "Please choose a System");
 
            }
            else
            {
                errorProvider1.Clear();
            }
 
        }

Open in new window

0
Comment
Question by:Camillia
[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
6 Comments
 
LVL 55

Assisted Solution

by:Jaime Olivares
Jaime Olivares earned 200 total points
ID: 20416862
at least it should be:
if (!rbSQL.Checked && !rbIBM.Checked)

notice the extra ampersand, it is a logical operator, you was using the bit-wise operator (&)
0
 
LVL 7

Author Comment

by:Camillia
ID: 20416916
that didnt do it . Not sure what to google for even...
0
 
LVL 26

Assisted Solution

by:Anurag Thakur
Anurag Thakur earned 400 total points
ID: 20417359
the following code snippet works for me
        private void panel1_Validating(object sender, CancelEventArgs e)
        {
            if (rad1.Checked == true || rad2.Checked == true)
            {
                MessageBox.Show ("Yes");
            }
            else
            {
                MessageBox.Show ("No");
            }
        }

Open in new window

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 13

Assisted Solution

by:SameerJagdale
SameerJagdale earned 200 total points
ID: 20417885
if (!rbSQL.Checked && !rbIBM.Checked)
            {
                errorProvider1.SetError((Control)sender, "Please choose a System");
            }
            else
            {
                errorProvider1.Clear();
            }

move this code to where you are validating, i mean on a button click or where you want to validate.
0
 
LVL 19

Accepted Solution

by:
arif_eqbal earned 1200 total points
ID: 20417984
Hi Farzadw,
A few points here,
Your event rbIBM_Validating is not proper for this kind of validation.
this event will be triggered "When the radioButton rbIBM is about to Lose Focus And before its value Checked/Uncheked is changed"
So suppose rbIBM was checked and you checked some other RadioButton so that it is Unchecked, but when the validating event fires it fires just before the rbIBM is actually uncheked, so if you chek in this event the value of Checked property would still be True. Hence this problem. Also, as you said very rightly, this event will not trigger if you click on say an OK button, it will only trigger when the rbIBM loses focus.
the solution given by ragi0017 would work, since he is using the validating event of the Panel not the radioButton, however, there's a catch there as well, if there are many controls (many panels etc.) the focus might be on some other control when you click on OK and not on that panel, however, you would of course get it at the time when focus shifted from this Panel to any other control.
My solution to this is put this IF block in your OK Button Click event
See the attached Code Snippet,
Also, I have used "&&" for the AND operator, though a single "&" is perfectly valid, the difference is
When you say Single & both conditions (rbSQL.Checked as well as rbIBM.Checked ) would be evaluated. however if you use Double && it will smartly not evaluate the second condition if the First is false.
Thanks

 
private void btnOK_Click(object sender, EventArgs e)
      if (!rbSQL.Checked && !rbIBM.Checked)
            {
                errorProvider1.SetError((Control)sender, "Please choose a System");
 
            }
            else
            {
                errorProvider1.Clear();
            }
        }

Open in new window

0
 
LVL 7

Author Comment

by:Camillia
ID: 20419475
let me read, try and will post back tonight.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
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…
Suggested Courses

715 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