TextBox Control in VB.net – Properties, Method and Events

What is TextBox Control in VB.net?

A TextBox Control in VB.net is used to show, and take text from the user as input, or a single line of text on a VB.net Windows form at runtime.

Additionally, the VB.net Textbox Control allows us to add additional text and scroll bars.

On the other hand, we can modify the text that appears in the form’s textbox.

Let’s create a text box by dragging a Text Box control from the Toolbox and dropping it on the form.

TextBox Control in VB.net Tutorial
TextBox Control in VB.net Tutorial

Properties of the TextBox Control in VB.net

The following are some of the commonly used Properties of the TextBox control in VB.net.

#Properties of the TextBox Control in VB.netDescription
1.AutoCompleteModeIt is used to get or set a value that indicates how the automatic completion works for the textbox control.
2.FontIt is used to set the font style of the text displayed on a Windows form.
3.LinesIt is used to set the number of lines in a TextBox control.
4.CharacterCasingIt is used to get or set a value representing whether the TextBox control can modify the character’s case as they typed.
5.MultilineIt is used to enter more than one line in a TextBox control, by changing the Multiline property value from False to True.
6.AcceptsReturnIt is used to get or set a value that indicates whether pressing the enter button in a multiline textbox; it creates a new line of text in control.
7.PasswordCharIt is used to set the password character that can be a mask in a single line of a TextBox control.
8.PreferredHeightIt is used to set the preferred height of the textbox control in the window form.
9.ScrollBarsIt is used to display a scrollbar on a multiline textbox by setting a value for a Textbox control.
10.TextIt is used to get or set the text associated with the textbox control.
11.VisibleThe Visible property sets a value that indicates whether the textbox should be displayed on a Windows Form.
12.WordWrapThe WordWrap properties validate whether the multiline Textbox control automatically wraps words to the beginning of the next line when necessary.
13.AutoCompleteCustomSourceGets or sets a custom System.Collections.Specialized.StringCollection to use when the AutoCompleteSourceproperty is set to CustomSource.
14.AutoCompleteSourceGets or sets a value specifying the source of complete strings used for automatic completion.
15.FontHeightGets or sets the height of the font of the control.
16.ForeColorGets or sets the foreground color of the control.
17.TabIndexGets or sets the tab order of the control within its container.
18.TextAlignGets or sets how text is aligned in a TextBox control.
19.TextLengthGets the length of text in the control.
Properties and Description of the TextBox Control in VB.net

Methods of the TextBox Control in VB.net

The following are some of the commonly used Methods of TextBox Control in VB.net.

#Methods of the TextBox Control in VB.netDescription
1.AppendTextAppends text to the current text of a text box.
2.ClearClears all text from the text box control.
3.CopyCopies the current selection in the text box to the Clipboard.
4.CutMoves the current selection in the text box to the Clipboard.
5.PasteReplaces the current selection in the text box with the contents of the Clipboard.
6.Paste(String)Sets the selected text to the specified text without clearing the undo buffer.
7.ResetTextResets the Text property to its default value.
8.ToStringReturns a string that represents the TextBoxBase control.
9.UndoUndoes the last edit operation in the text box.
Methods and Description of the TextBox Control in VB.net

Events of the TextBox Control in VB.net

The following are some of the commonly used Events of the Text control in VB.net.

#Events of the TextBox Control in VB.netDescription
1.ClickWhen a textbox is clicked, a click event is called in the textbox control.
2.CausesValidationChangedIt occurs in the TextBox Control when the value of CauseValidation property is changed.
3.AcceptTabsChangedIt is found in the TextBox control when the property value of the AcceptTab is changed.
4.BackColorChangedIt is found in the TextBox Control when the property value of the BackColor is changed.
5.BorderStyleChangedIt is found in the TextBox Control when the value of the BorderStyle is changed.
6.ControlAddedIt is found when the new control is added to the Control.ControlCollection.
7.CursorChangedIt is found in TextBox, when the textbox control is removed from the Control.ControlCollection.
8.FontChangedIt occurs when the property of the Font is changed.
9.GetFocusIt is found in TextBox control to get the focus.
10.MouseClickA MouseClick event occurs when the mouse clicks the control.
11.MultilineChangedIt is found in a textbox control when the value of multiline changes.
Events and Description of the TextBox Control in VB.net

Let’s create a program that displays the login details.

Public Class Form1

    Private Sub Button1_Click(sender As Object, e As EventArgs)

    End Sub

    Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
        ' Display the user details, when the Button1 is clicked  
        MsgBox(" Your Name: " & sname.Text + vbCrLf + "Your Address: " & address.Text + vbCrLf + "Your Course: " & course.Text)
    End Sub

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

    End Sub
End Class

Program Output:

TextBox Control in VB.net Output
TextBox Control in VB.net Output

Now enter all the details of the student registration form, it shows the following image, as shown below.

TextBox Control in VB.net Entering Details
TextBox Control in VB.net Entering Details

Now, click on the Register button. It shows all the details filled in by the user in the form.

TextBox Control in VB.net Display Message Box
TextBox Control in VB.net Display Message Box

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.


Leave a Comment