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
Solved

Windows Forms Control Validation and allowing form close.

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

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 …
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

856 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