Results 1 to 12 of 12

Thread: [RESOLVED] CausesValidation=false not working?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2005
    Posts
    101

    Resolved [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.

  2. #2
    PowerPoster sparrow1's Avatar
    Join Date
    May 2005
    Location
    Globetrotter
    Posts
    2,820

    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
    Wkr,
    sparrow1

    If I helped you, don't forget to Rate my post. Thank you

    I'm using Visual Studio.Net 2003 and
    2005
    How to learn VB.Net Create setup with VB 2005 Drawing for beginners VB.Net Tutorials GDI+ Tutorials
    Video's for beginners

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jul 2005
    Posts
    101

    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.

  4. #4
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5
    PowerPoster sparrow1's Avatar
    Join Date
    May 2005
    Location
    Globetrotter
    Posts
    2,820

    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
    Wkr,
    sparrow1

    If I helped you, don't forget to Rate my post. Thank you

    I'm using Visual Studio.Net 2003 and
    2005
    How to learn VB.Net Create setup with VB 2005 Drawing for beginners VB.Net Tutorials GDI+ Tutorials
    Video's for beginners

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Jul 2005
    Posts
    101

    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
    Last edited by Essential; Mar 7th, 2006 at 07:06 PM.

  7. #7
    PowerPoster sparrow1's Avatar
    Join Date
    May 2005
    Location
    Globetrotter
    Posts
    2,820

    Re: CausesValidation=false not working?

    I hope it will help you!

    sparrow1
    Wkr,
    sparrow1

    If I helped you, don't forget to Rate my post. Thank you

    I'm using Visual Studio.Net 2003 and
    2005
    How to learn VB.Net Create setup with VB 2005 Drawing for beginners VB.Net Tutorials GDI+ Tutorials
    Video's for beginners

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Jul 2005
    Posts
    101

    Re: CausesValidation=false not working?

    tg: ok check this out!

    VB Code:
    1. Public Class Form1
    2.  
    3.  
    4.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    5.         Button1.CausesValidation = False
    6.     End Sub
    7.  
    8.  
    9.     Private Sub TextBox1_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating
    10.         If TextBox1.Text = "" Then
    11.             MsgBox("You must enter something")
    12.             e.Cancel = True
    13.         End If
    14.     End Sub
    15.  
    16.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    17.         MsgBox("Button1Click - No Me.Close event")
    18.         'MsgBox "Button1Click - Yes Me.Close event"
    19.         'Me.Close
    20.     End Sub
    21. 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.
    Last edited by Essential; Mar 7th, 2006 at 07:25 PM.

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. Private Sub TextBox1_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating
    2.         If Me.TextBox1.Text = String.Empty Then
    3.             MessageBox.Show("Please enter a value.")
    4.             e.Cancel = True
    5.         End If
    6.     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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Jul 2005
    Posts
    101

    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:
    1. Private Sub TextBox1_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating
    2.         If Me.TextBox1.Text = String.Empty Then
    3.             MessageBox.Show("Please enter a value.")
    4.             e.Cancel = True
    5.         End If
    6.     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:
    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.

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Jul 2005
    Posts
    101

    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!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width