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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Closing Form with CauseValidation = true

Status
Not open for further replies.

Bloobird

Programmer
Oct 23, 2001
49
GB
Hi - I've just started using vb.net from vb6 - I have a form with a text box which I am not letting the user exit from unless a valid value is entered (By using the Validating event and having the CausesValidation property on all all other controls = true). However, I want the user to be able to exit the form by clicking the X in the top right, even if an invalid value is in the textbox. I tried setting CausesValidation to false against the form (As per VB6), but this does not appear to work. Any ideas?

Thanks

Bloobird
 
Hi

in FORM_CLosing event
Private Sub frmContainer_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
e.cancel=false


Regards
Nouman

Nouman Zaheer
Software Engineer
MSR
 
Nouman,

Thanks for that. It certainly works (The form now unloads), however the error message from the validation is still appearing - any idea how to prevent this ?

Thanks

Bloobird
 
Hi bloobird

Sorry i don't think that you will able to stop the message which is present in the VALIDATED
event of the TEXBOX because VALIDATED event of controls fires first then the FORM
CLOSING EVENT and its by default..now what i would do to implement the validation is
to have the following logic (Altough i know this is not what you require but that what i have :) )
a) Add a textbox to a form
b) add a varibale to form as
Dim Changed as boolean
c) set Changed=false at Form_Load
d) Set Changed=true at TextChanged event of textbox
e) In the Form_Closing event the you can check like this
If Changed then
'Do validation
if not validaion() then
Dim response As MsgBoxResult = MsgBox("validation field, Whould you like to Close Form?", MsgBoxStyle.YesNo, "IT/Way Bill")
If response = MsgBoxResult.No Then
e.Cancel = True
End If
End If
End if


Nouman Zaheer
Software Engineer
MSR
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top