Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

use of validating event

Status
Not open for further replies.

newora

Programmer
Aug 19, 2003
133
GB
Hi There,

Could someone please help me with regards to the design of my routine and the use of the validating event.

I have two comboboxes on a form, along with some other text boxes and an OK button and a cancel button.

I have setup the validating event for both comboxes, to verify the contents of them before moving onto the other inputs.

The issue is that the validating event fires when either one of the comboxes loses focus, which is fine, except for when I click the cancel button or the cross on the control box. When I click the cancel button or the cross in the top right of the form to close the form off, I do not want to call the validation events really - which I do not think is posisble.

Thus I thought that I would put some logic in the comboboxes validation events to say if the control receiving the focus is the cancel button or the control box cross, I would not do any validation.

I must admit I do not know how I would do this in VB .NET - I know that there is a controls collection, so maybe I can get the control name that is receiving the focus.

Do you think that this is the way to go, or should I just put all validation into my OK button?

Many thanks for your opinions / help.
 
Set the Cancel's button "CausesValidation" property to FALSE. By doing this, you will be able to press it even if the data are invalidated.
 
Doh !! I completelky forgot all about the causesvalidation property of each control - thanks a lot for that.

However there is still a problem when I try to close the form off using the cross in the control box, in that it still causes the validating event to be called and if invalid data is in the the combobox, the form will not close.

Thanks again and any further ides / pointers would be much appreciated.
 
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
e.Cancel = False
End Sub
 
Thanks again TipGiver - funnily enough I had just put this in the form_closing event myself and also set the causesvalidation property of the form to false - but it is still calling the validate event of the combobox even when I click on the cross or press the ESC key, but not when I click on the cancel key.

The form being displayed is an MDI child and so I have set causesvalidation to false for the MDI parent, but the validate event is still called when I click on the cross in the MDI child form control box.

It is obviously me doing something wring - maybe I need to do some more VB .NET redaing before continuing with the project (it been a year or so since I coded in VB .NET)

Thanjks again for your help though.
 
I hope in the validating and not in the validated event handler.
 
Hi Guys,

Thanks for the replies - my code is differently in the validating event handler and not the validated event - I have double checked this to be sure.

I will look at it again tomorrow morning now I think - thanks again for your ideas - I am obviuosly doing something stupid but I can not see what!!

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top