One control many validations?

General Fear

Member
Joined
Dec 22, 2012
Messages
12
Programming Experience
10+
I am using VB.Net 2010.

I have 2 questions.

Suppose that I wanted to create one custom control like a textbox. In the textbox, it will have all the typical code you find in a desktop application. For example, I need a letter only textbox, numbers only textbox, this textbox can't be empty textbox etc etc . . .

1.) Can I do the above example where I put all the validation into a single custom control.

2.) Okay so if the above is possible, how do I associate the code in the custom control to the textbox on the form. If I drop a custom textbox control called txtZip, how do I get txtZip to use the validation code that checks for numbers only.
 
You would add one or more properties to your control to define how it was to behave and then override the appropriate methods, test those properties and validate accordingly. Here's a simple example:
VB.NET:
Public Class CustomTextBox
    Inherits TextBox

    Public Enum ContentType
        Any
        Numeric
        Nonnumeric
    End Enum

    Public Property Content As ContentType

    Public Property ContentRequired() As Boolean

End Class
You can then use If statements to determine the appropriate validation to perform at the appropriate times based on the values of those two properties.
 
Back
Top