[RESOLVED] CausesValidation=false not working?
Hey all,
I originally posted this on the msdn forums because I think it may be a bug and I wanted the msdn guys to see it, however, I'm not getting any help there so I'm hoping someone here can give me a hand.
I'm trying to get a cancel button on a form to not raise validation events. Looking it up on msdn it seems simple enough I just set the CausesValidation property on the button to False and it should not fire the validation event.
However, it's not working. I've looked at the different ways to change AutoValidate on the form but none of them give me the desired results and all the msdn material I can find leads me to believe that the form should keep it's default of EnablePreventFocusChange and that only the control (cancel button in this case) should I have to change anything.
I'm not sure if this is a bug in vb 2005 or if I am doing it wrong. Any and all help is really appreciated.
Re: CausesValidation=false not working?
Hi,
After I've read your thread, I think you just want that your cancel Button becoms Disable.
I found that when I set:
CausesValidation = false
ore
Enable = false
ore in code
cancelbutton1.Enable = false
Every time my Button is Disabled!
I think try to designe your button again and see what happens.
Hope it helps,
saprrow1
Re: CausesValidation=false not working?
Quote:
Originally Posted by sparrow1
Hi,
After I've read your thread, I think you just want that your cancel Button becoms Disable.
I found that when I set:
CausesValidation = false
ore
Enable = false
ore in code
cancelbutton1.Enable = false
Every time my Button is Disabled!
I think try to designe your button again and see what happens.
Hope it helps,
saprrow1
Hey sparrow1,
Thanks for the reply but I definitly want my button enabled. The causesvalidation property should just affect whether or not the control will activate the validation events on other controls. For example, I have a Calendar control that I want to run a Validating event on if they change the selection, but I also want them to be able to click on Cancel and not run the validating event.
Ok, hopefully that clears it up some.
Re: CausesValidation=false not working?
Do you have a link to that MSDN page? I'll take a look at it and see what needs to be done. What you've described sounds reasonable, but there's probably something that's being over looked (and I'll bet it's something simple and stupid... usualy is.)
-tg
Re: CausesValidation=false not working?
Quote:
Originally Posted by Essential
Hey sparrow1,
Thanks for the reply but I definitly want my button enabled. The causesvalidation property should just affect whether or not the control will activate the validation events on other controls. For example, I have a Calendar control that I want to run a Validating event on if they change the selection, but I also want them to be able to click on Cancel and not run the validating event.
Ok, hopefully that clears it up some.
I'll be honest with you, no it doesn't clear up, but the more I learn the more I know!
Anyway I found a link with a lot of explanation and example about CausesValidations.
See this link:
http://msdn2.microsoft.com/en-us/lib...alidation.aspx
I hope it helps you this time,
sparrow1
Re: CausesValidation=false not working?
Quote:
Originally Posted by techgnome
Do you have a link to that MSDN page? I'll take a look at it and see what needs to be done. What you've described sounds reasonable, but there's probably something that's being over looked (and I'll bet it's something simple and stupid... usualy is.)
-tg
tg: the link below that sparrow1 provided is the first one that I had found and based most of my assumptions off. I'm trying to find the other one that I found, I'll post it when I find it.
Quote:
Originally Posted by sparrow1
Thanks sparrow1! That is also the same link I was going to post :)
edit: another link: http://msdn.microsoft.com/library/de...onproperty.asp
link to example: http://msdn.microsoft.com/library/de...tyexamplex.asp <- this link is vb6 example it looks like, but is demonstrating the functionality
Re: CausesValidation=false not working?
I hope it will help you!
sparrow1
Re: CausesValidation=false not working?
tg: ok check this out!
VB Code:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Button1.CausesValidation = False
End Sub
Private Sub TextBox1_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating
If TextBox1.Text = "" Then
MsgBox("You must enter something")
e.Cancel = True
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
MsgBox("Button1Click - No Me.Close event")
'MsgBox "Button1Click - Yes Me.Close event"
'Me.Close
End Sub
End Class
this is just a simple 1 form, 1 button, 2 textbox form. Please test this program and you'll see some weird (might be working right, who am i to say) stuff.
Notice that if you enable the code in button1 click, namely Me.Close, then the validation event for textbox1 will fire. But if you leave it disabled then you can get the MsgBox("Button1Click - No Me.Close event") just fine, however try and go straight to the close form "x" and then the textbox1 validating event fires. So, it's like it's still sitting at the TextBox1 validating event, but it allows non event things to happen?
EDIT: I'm beginning to think that I might be trying to use the CausesValidation event for something it's not meant to do. I mean, it makes sense to me that it would work how I want it to; that way if the user enters information that I cannot accept I can allow them the ability to cancel out of the form. That being said, it seems like the msdn pages are refering to using CausesValidation to pop up some Help for the user.
Re: CausesValidation=false not working?
I just tested it and I have no such issue. I created a form and added a TextBox and two Buttons. I set the CausesValidation property of the second Button to False. I then added the following event handler:
VB Code:
Private Sub TextBox1_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating
If Me.TextBox1.Text = String.Empty Then
MessageBox.Show("Please enter a value.")
e.Cancel = True
End If
End Sub
I then run the project and the TextBox is initially focused with no text entered. If I press Button1 I get the message and the TextBox retains focus. If I press Button2 there is no message and the Button gets the focus, exactly as you'd expect. Note that this just means that the form will not be Validated when that Button is pressed. As soon as that Button loses focus the form will be validated. This means that you must close the form from within the Click event handler of that Button.
Re: CausesValidation=false not working?
Quote:
Originally Posted by jmcilhinney
I just tested it and I have no such issue. I created a form and added a TextBox and two Buttons. I set the CausesValidation property of the second Button to False. I then added the following event handler:
VB Code:
Private Sub TextBox1_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating
If Me.TextBox1.Text = String.Empty Then
MessageBox.Show("Please enter a value.")
e.Cancel = True
End If
End Sub
I then run the project and the TextBox is initially focused with no text entered. If I press Button1 I get the message and the TextBox retains focus. If I press Button2 there is no message and the Button gets the focus, exactly as you'd expect. Note that this just means that the form will not be Validated when that Button is pressed. As soon as that Button loses focus the form will be validated. This means that you must close the form from within the Click event handler of that Button.
Hi jmc, thanks for the help.
I created the same project that you did and indeed, I am seeing the same results. What I need clarification on is this:
Quote:
This means that you must close the form from within the Click event handler of that Button
I take this to mean that I should put Me.Close in the Click event of Button2. However, as soon as I add Me.Close to the click event of button2 I start getting the "Please enter a value" msgbox, as I would not expect. This is where my problem lies. Maybe you are talking about something else witht the Click event handler?
I'm sure you noticed that in your project if you click on Button2 (and obviously you don't get the msgbox) and then click the "x" to close the form you now get the msgbox and have to enter something in the textbox in order to close the form. That is what I'm trying to avoid. I want to be able to close the form without running the validation event.
Thanks!
edit: I just wanted to add that it's entirely possible I am trying to use the causesvalidation property in a way it's not meant to be used. I can't imagine though that I'm the first person to want to skip the validation event when a cancel button is clicked.
Re: CausesValidation=false not working?
Hmmm... now I see your dilemma. I should have taken my testing a step further. I'm guessing that the CausesValidation property was created with dialogues in mind, because I just tested the same scenario with a form that was displayed by calling ShowDialog and it worked as you want. As a work-around for forms that have been displayed by calling Show, or indeed the startup form, you could set the AutoValidate property of the form to Disable before calling Close.
Re: CausesValidation=false not working?
Quote:
Originally Posted by jmcilhinney
Hmmm... now I see your dilemma. I should have taken my testing a step further. I'm guessing that the CausesValidation property was created with dialogues in mind, because I just tested the same scenario with a form that was displayed by calling ShowDialog and it worked as you want. As a work-around for forms that have been displayed by calling Show, or indeed the startup form, you could set the AutoValidate property of the form to Disable before calling Close.
jmc: Thanks for the help looking into this. I think you are right about causesvalidation being meant for dialogues as all the evidence points to that, but the msdn material would indicate otherwise (which is why I pursued this in the first place).
Setting the forms AutoValidate property to disabled does indeed give me the desired result I was looking for so I'm going to use that. Thanks again for your help!