Solved

validating radiobutton in winform doesnt work

Posted on 2007-12-05
6
1,402 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

617 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