ToolStripButton .Net 2.0 CausesValidation Property and Validating...

Posted on 2008-11-13
Last Modified: 2012-06-21
I have a Windows Forms application using VS 2005.  When running the App I want to have CausesValidation Property and the Validating Event to be available for me to use when I move from a TextBox to a ToolStripButton.  The Problem is that the ToolStripButton does not support CausesValidation Property or the Validating Event (ToolStripTextBox does suport Validation via the parent class ToolStripControlHost also ToolStrip does via the parent class Control) But ToolStripButton does not.

How can I have my TextBox validate when the user move from the TextBox to the the ToolStripButton?
Question by:u2envy1
    LVL 29

    Expert Comment

    Honestly, I probably don't understand the problem.

    If you move from a textbox to a button - what do you wan't to validate? If you want to validate the textbox all you need is to add validating event handler. For that you select the textbox in designer, go to Properties tab, click "lightening" button and select Validating event. Double click it. Put any code there, e.g. MesageBox.Show("Hello") just to try. Now, when you leave the textbox the Validating event happens and handler is executed.

    Or pls clarify the problem.

    Author Comment

    Let me clarify....
    I have a validate event on a textbox. If you tab away from the textbox the validation fires. If you mouse click on a button that is placed on the ToolStrip the validation is not fired. The ToolStripButton does not have a causes validation property. Any work around to fire the validation on mouse click for the ToolStripButton ?
    LVL 29

    Accepted Solution

    Honestly it was a surprise for me. I tried before with a textbox on the same toolstrip - and validation worked. But you are right - if we have a normal textbox and click a button on a toolstrip validation event is not fired.

    Now I can see the only way: to call the validation event handler from the button click event handler. See the snippet.
            // validating event handler for a textbox
            private void textBox1_Validating(object sender, CancelEventArgs e)
            // toolstrip button click event handler
            private void toolStripButton1_Click(object sender, EventArgs e)
                // call texbox validation manually
                textBox1_Validating(textBox1, new CancelEventArgs());

    Open in new window


    Author Comment

    I have code in the toolStripButton1_Click event that must not execute if a Validation occurs.
    How can this be done ?

    Expert Comment

    Instead of explizit calling the validation event of the control, it is better to move the focus somewhere. The focus isn't moved from your control to validate, while clicking the toolstrip. That's the reason why the event is not firing. Just move it to a label, and the validate will fire.

    Private Sub TSB_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TSB_Speichern.Click

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Join & Write a Comment

    Article by: Najam
    Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
    This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    745 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

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now