Solved

Windows Forms Control Validation and allowing form close.

Posted on 2009-04-14
4
383 Views
Last Modified: 2013-11-26
I am using code in my Validating event on a ComboBox to force the user to make a choice, which works, but once the user opens the form then they have to make a choice before they can close it.  I was to check if the form is closing, then skip validation...  I use the code below to validate...
If String.IsNullOrEmpty(ComboBox1.SelectedItem) Then

            e.Cancel = True

        End If

Open in new window

0
Comment
Question by:rgn2121
  • 3
4 Comments
 
LVL 15

Assisted Solution

by:JackOfPH
JackOfPH earned 50 total points
ID: 24135653
TRy this...

If ComboBox1.Selectedindex =-1 Then
            e.Cancel = True
End If


Or this

If ComboBox1.Selecteditem is nothing Then
            e.Cancel = True
End If
0
 
LVL 12

Author Comment

by:rgn2121
ID: 24135702
Okay..that works.  I am curious...when I use this validating event with the combo box and I click the red X to close out, the way I have it setup is that the ErrorProvider pops the little message.  But when I go to try and make a selection the drop down will open and then close right back unless I click the drop down and hold the mouse button down till I drag onto the selections.  Have you ever seen this?
 
0
 
LVL 12

Author Comment

by:rgn2121
ID: 24135738
You know...I am not sure that solution really works, because if they don't make a selection and they click another control on the form, then it will allow it because I allow ComboBox.SelectedItem=Nothing.
Is there a way to tell if the form is closing  without using API?
0
 
LVL 12

Accepted Solution

by:
rgn2121 earned 0 total points
ID: 24135806
Okay...I couldn't work around what you had given me so that other controls could be clicked and it would validate, but it wouldn't on the form_Closing..  I finally found that I can leave my code alone in the validating event and then in my form_Closing event I can set e.Cancel = False.  This will allow my form to close when the click the red x no matter what the validation says....
 
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

743 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

14 Experts available now in Live!

Get 1:1 Help Now