2 Ways to Limit TextBox Character Length in Visual Basic.NET (VB.Net)


This article discusses how to limit the length of characters that can be typed into a TextBox on a Form with Visual Basic.Net (VB.Net). Programming Language Visual Basic / Visual Basic.Net is a programming language that is easy to learn by programmers. Although, over time it is increasingly rare for people to use Visual Basic.Net to create Desktop-based applications, but not by creating web-based applications with the help of ASP.

This tutorial applies to all versions of Visual Studio that have Visual Basic.Net which includes Visual Studio 2008, Visual Studio 2010, Visual Studio 2012, and so on, both the express version (a separate programming language between VB.Net and C#) or full, except Visual Basic 6.

This method is usually used to validate certain codes such as KTP or NIK numbers where the character length is 16 digits, member validation is based on the registration date which is 8-20 digits long. This affects when saving or editing data into the database with a character length that has been determined by each field by the database system itself.

If you do not limit the length of characters in the TextBox, you cannot save or edit data into the database because the results of typing text input into the TextBox exceed the length of the characters in each field in one table.

The author provides a tutorial with two ways to limit the length of characters typed into a TextBox on a Form using VB.Net, namely through Properties and Coding. The author made this tutorial using Visual Basic 2010 Express, you can use the Visual Studio IDE installed on your computer or laptop.


1. Activate Visual Studio with any version installed on your computer or laptop.

2. Create a new project using Visual Basic.Net or choose an existing project.

a. Via Properties

3. Click TextBox.

4. Go to Properties >>> Choose MaxLength and enter the limit value for the character length.

5. Press Enter.

b. Via Coding (Command Code)

3. Double click Form.

4. Put the command code into the Form_Load scope.

TextBox1.MaxLength = [nilai limit panjang karakter]

5. Press Enter.

Result Display

6. Click Start Debugging or the play icon.

7. The following is the result of typing text in the TextBox.



You can limit the length of characters that can be typed by the application user into the TextBox. This is done in order to save the file size on the desktop application during the process of making the application. This can be done to prevent data input errors caused by excess length of characters that have been agreed to save or edit data into the database.


Leave a comment

Your email address will not be published.