Solved

validating radiobutton in winform doesnt work

Posted on 2007-12-05
6
1,376 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 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

740 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