Solved

Windows Forms Control Validation and allowing form close.

Posted on 2009-04-14
4
426 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
[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
  • 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

Business Impact of IT Communications

What are the business impacts of how well businesses communicate during an IT incident? Targeting, speed, and transparency all matter. Find out more in this infographic.

Question has a verified solution.

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

Suggested Solutions

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

739 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