• Home
  • Testing
  • SAP
  • Web
  • Must Learn!
  • Big Data
  • Live Projects
  • AI
  • Blog

What is TextBox Control?

The TextBox Control allows you to enter text on your form during runtime. The default setting is that it will accept only one line of text, but you can modify it to accept multiple lines. You can even include scroll bars into your TextBox Control.

In this tutorial, you will learn

TextBox Properties

The following are the most common properties of the Visual Basic TextBox control:

  • TextAlign- for setting text alignment
  • ScrollBars- for adding scrollbars, both vertical and horizontal
  • Multiline- to set the TextBox Control to allow multiple lines
  • MaxLength- for specifying the maximum character number the TextBox Control will accept
  • Index- for specifying the index of control array
  • Enabled- for enabling the textbox control
  • Readonly- if set to true, you will be able to use the TextBox Control, if set to false, you won't be able to use the TextBox Control.
  • SelectionStart- for setting or getting the starting point for the TextBox Control.
  • SelectionLength- for setting or getting the number of characters that have been selected in the TextBox Control.
  • SelectedText- returns the TextBox Control that is currently selected.

Textbox Events

The purpose of events is to make the TextBox Control respond to user actions such as a click, a double click or change in text alignment. Here are the common events for the TextBox Control:

  • AutoSizeChanged- Triggered by a change in the AutoSize property.
  • ReadOnlyChanged- Triggered by a change of the ReadOnly property value.
  • Click- Triggered by a click on the TextBox Control.

How to Create a TextBox

Step 1) To create a TextBox, drag the TextBox control from the toolbox into the WindowForm:

Step 2)

  1. Click the TextBox Control that you have added to the form.
  2. Move to the Properties section located on the bottom left of the screen. Change the name of the text box from TextBox1 to HelloTextBox:

Step 3) Add the following code to add text to the control:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'Add text to the control
         HelloTextBox.Text = "Hello. Welcome to Guru99!"
  End Sub

Step 4) You can now run the code by clicking the Start button located at the top bar:

Step 5) You should get the following form:

Here is a screenshot of the complete code for the above:

Explanation of code:

  1. Creating a public class named Form1
  2. Creating a sub procedure named Form1_Load. It will be called when the form is loaded.
  3. A comment. The VB.net compiler will skip this.
  4. End the subprocedure
  5. End the class.

Password character

Sometimes, you want a text box to be used for typing a password. This means that whatever is typed into that text box to remain confidential. This is possible with VB.net. It can be done using the PasswordChar property which allows us to use any character that you want. Let us demonstrate this using an example:

Begin by creating a new project. Drag two TextBox Controls, two Labels, and one Button into the form. Change the texts on the two labels and the button to the following:

Click the text box next to Username label, move to the Properties section located at the bottom left of the window. Give it the name UsernameField.

Do the same for the TextBox Control next to Password label, giving it the name PasswordField.

You should now make the PasswordField TextBox Control show * for each character typed in it. Add the following code:

Private Sub PasswordField_TextChanged(sender As Object, e As EventArgs) Handles PasswordField.TextChanged
       PasswordField.PasswordChar = "*"
End Sub

Now, run the code by clicking the Start button. A form will popup.

Type the username and the password and observe what happens. You should see the following:

The username is shown, but the password has been hidden. Here is the code for the above:

Explanation of code:

  1. Creating a class named Form1.
  2. Creating a sub-procedure named PasswordField_textchanged(). It will be called when the form is loaded.
  3. Using the PasswordChar property to show * as a user types the password.
  4. Ending the sub-procedure.
  5. Ending the class.

Newline in TextBox

By default, you can only create one line of text in a text box. There are two ways through which we can achieve this. Let us discuss them.

Drag and drop a TextBox Control to your form. Give the control the name GuruWelcome.

Click the text box control again and move the mouse cursor to the Properties section. Change the value of Multiline property to True.

Alternative, you can set the Multiline property to true in your code as follows:

GuruWelcome.Multiline = True

Add the following code:

 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        GuruWelcome.Multiline = True

        GuruWelcome.Text = "Line 1"

        GuruWelcome.Text = GuruWelcome.Text & ControlChars.NewLine & "Line 2"

    End Sub

Upon execution, the two lines of text will be separated.

Explanation of Code:

  1. Creating a class named Form1
  2. Creating a sub-routine named Form1_Load()
  3. Setting the Multiline property to True. The textbox will be able to take more than one lines.
  4. Adding the first line of text to the text box.
  5. Adding the second line of text to the text box. The Controlchars.NewLine property helps us to split the two lines.
  6. Ending the sub-routine.
  7. Ending the class.

Retrieving Integer Values

VB.net treats everything as a string. This means that you read an integer from the text box as a string, then you convert it into an integer. This is normally done using the Integer.Parse() method.

To demonstrate this, create a new text box control plus a button. Give the text box the name age. Give the button the name Button1. You should have the following interface:

Add the following code:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim x As Integer
        x = Integer.Parse(age.Text)
        MessageBox.Show(x)

Run the code, and enter your age into the text box. Click the Show Age button. You should see the following:

The value you enter is returned in a MessageBox.

Explanation of Code:

  1. Creating a class named Form1.
  2. Creating a sub-procedure named Button1_Click. It will be called when the button is clicked.
  3. Defining an integer variable named x.
  4. Converting the value read from the textbox named age into an integer.
  5. Displaying the value converted in the above step in a MessageBox.
  6. Ending the sub-procedure.
  7. Ending the class.

ReadOnly TextBox

You can make a textbox readonly. This means that the user will not be able to modify the value displayed within the textbox. To do this, you set the ReadOnly property to True. To demonstrate this, create a new textbox and give it the name guru99. Add the following code:

Private Sub guru99_TextChanged(sender As Object, e As EventArgs) Handles guru99.TextChanged

        guru99.Text = "welcome to Guru99"

        guru99.ReadOnly = True

    End Sub

The code should return the following:

Here is the code:

Explanation of Code:

  1. Creating a class named Form1.
  2. Creating a subroutine named guru99_TextChanged.
  3. Adding text to the textbox named guru99.
  4. Setting the ReadOnly property of the textbox to True. This make the text on the textbox unmodifiable.
  5. Ending the sub-procedure.
  6. Ending the class.

max length

The MaxLength property can help you set the maximum number of words or characters that the textbox will allow. To demonstrate this, create a TextBox control and give it the name fullName. Add the following code:

Private Sub fullName_TextChanged(sender As Object, e As EventArgs) Handles fullName.TextChanged

        fullName.MaxLength = 8

    End Sub

Run the code and try to type your full name. You will not be able to type more than 8 characters, with whitespace included:

The code:

Code Explanation:

  1. Creating a class named Form1.
  2. Creating a sub-procedure named fullName_TextChanged.
  3. Making the fullName textbox accept a maximum of only 8 characters.
  4. Ending the sub-procedure.
  5. Ending the class.

Summary:

  • The TextBox Control allows you to enter text into your form during runtime. It is good for getting input from users.
  • The default setting is that the TextBox Control will only accept one line of text. However, it is possible for you to change this.
  • You can hide what the user types into the TextBox, especially when you need to capture passwords.
  • You can also set the maximum number of characters that you need to be entered into the TextBox.
  • You can make your TextBox un-editable, meaning that the users won't be able to change the text displayed on it.

 

YOU MIGHT LIKE: