Solved

validating radiobutton in winform doesnt work

Posted on 2007-12-05
6
1,330 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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
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.
This video discusses moving either the default database or any database to a new volume.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

706 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

15 Experts available now in Live!

Get 1:1 Help Now