NET class: To use this new property and check the validity of the Text Box contents, use the System. With this code, a regular expression designed to find five-digit zip codes would match the first five digits in a string such as "23434fred" despite the extra text added on at the end. Length Then Return True Else Return False End If Catch 'some form of error in parsing the pattern Return False End Try End Function Windows Forms controls provide a variety of methods and events dealing with validation already, so it seems appropriate to fit my new Regular Expression check into that existing model by checking the Text Box contents in the On Validating method. On Validating(e) End Sub Put all of this code together and you have a complete control that you can try out.
Since a version of this control hasn't been provided for use in Windows Forms, it makes a great example of a custom control that you can build—a Windows Form Text Box that validates its contents against a regular expression.
To create my validating Text Box, I will have to work through a few different steps.
For this control, there are two properties: Valid and Regular Expression. Category attribute is what you will use to specify the appropriate category, but note that it just takes a string as an argument, so make sure you spell the category name correctly: Figure 1.
I am going to put Regular Expression in the Behavior category, but there is no real restriction on where you put a specific property; you can even create your own category. You can use any category name you wish and it will appear as a section of the property window. Description attribute accepts a string as an argument that is then displayed whenever that property is selected in the Properties window: Valid, unlike Regular Expression, is not very useful at design time (it is read-only and calculated based on the contents of the Text Box), so I do not want it to appear in the Properties window in any category.
Regular Expression to validate video file formats Here my need is to validate a file for only video formats, so that the user is able to upload the file in the valid video formats. Net application that contains a file upload control and with a regular expression validation control.
To validate only the video file, I provide the regular expression for video file formats to the validation control and some properties of the validation control to validate the file upload control.
With that in mind, I will create a new Windows Control Library project to hold the new control, and then create a regular Windows Application project in the same solution to make it easy to test the control.
By default, if you create a new Windows Control Library project, a new blank User Control will be added as a starting point.
In this case I don't want to use a User Control, since I am inheriting from an existing control, so I am going to delete that file and just add a new class file.