Solved

validating radiobutton in winform doesnt work

Posted on 2007-12-05
6
1,355 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
6 Comments
 
LVL 55

Assisted Solution

by:Jaime Olivares
Jaime Olivares earned 50 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 100 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 13

Assisted Solution

by:SameerJagdale
SameerJagdale earned 50 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 300 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

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.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

786 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