Solved

Windows Forms Control Validation and allowing form close.

Posted on 2009-04-14
4
394 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

920 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

12 Experts available now in Live!

Get 1:1 Help Now