VB.Net Form Level Validation

Posted on 2004-11-22
Last Modified: 2008-02-26

I want to validate all the controls (validating event of the controls) in a button click. Like as we have in ASP.NET (Validation Summary).

For Example, in an Employee Master, EmpID, EmpName, Designation, DateofJoin must not be empty. This can be done using Validating Event for the individual controls. But the User may fill only EmpName and Click the SAVE button. At this stage, all the controls have to validated by manual coding like EmpName<>"", Designation<>"".

Instead of this manual coding, I want to have an existing method or a generalized function to run the Validating Event of the controls in a form.

This facility is available in ASP.Net by using Validation Summary Control. How can be the same obtained in VB.Net?

Hope all of you understand my requirement.

Thanks in advance.

Question by:devan_alagar
    LVL 4

    Accepted Solution


    I suggest that you write a custom control that inherits from textbox (call it ValidatingTextBox). In this control you add the Validate() method (it could return a boolean or whatever). Inside this function, you make the validating tests.

    Then in your form that contains the ValidatingTextBox controls, call it for each of these :

    (something like this :)
    for each ctrl as Control in Me.Controls
        If typeof ctrl is ValidatingTextBox then
            Ctype(ctrl, ValidatingTextBox).Validate()
        End If

    Hope it helps
    LVL 4

    Expert Comment

    You can even add a label in the control to automatically display your error message.


    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

    This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
    Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    729 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

    20 Experts available now in Live!

    Get 1:1 Help Now