Text box controls allow entering text on a form at runtime. By default, it takes a single line of text, however, you can make it accept multiple texts and even add scroll bars to it.
Let's create a text box by dragging a Text Box control from the Toolbox and dropping it on the form.
The following are some of the commonly used properties of the TextBox control −
|Sr.No.||Property & Description|
Gets or sets a value indicating whether pressing ENTER in a multiline TextBox control creates a new line of text in the control or activates the default button for the form.
Gets or sets a custom System.Collections.Specialized.StringCollection to use when the AutoCompleteSourceproperty is set to CustomSource.
Gets or sets an option that controls how automatic completion works for the TextBox.
Gets or sets a value specifying the source of complete strings used for automatic completion.
Gets or sets whether the TextBox control modifies the case of characters as they are typed.
Gets or sets the font of the text displayed by the control.
Gets or sets the height of the font of the control.
Gets or sets the foreground color of the control.
Gets or sets the lines of text in a text box control.
Gets or sets a value indicating whether this is a multiline TextBox control.
Gets or sets the character used to mask characters of a password in a single-line TextBox control.
Gets or sets a value indicating whether text in the text box is read-only.
Gets or sets which scroll bars should appear in a multiline TextBox control. This property has values −
Gets or sets the tab order of the control within its container.
Gets or sets the current text in the TextBox.
Gets or sets how text is aligned in a TextBox control. This property has values −
Gets the length of text in the control.
Indicates whether a multiline text box control automatically wraps words to the beginning of the next line when necessary.
The following are some of the commonly used methods of the TextBox control −
|Sr.No.||Method Name & Description|
Appends text to the current text of a text box.
Clears all text from the text box control.
Copies the current selection in the text box to the Clipboard.
Moves the current selection in the text box to the Clipboard.
Replaces the current selection in the text box with the contents of the Clipboard.
Sets the selected text to the specified text without clearing the undo buffer.
Resets the Text property to its default value.
Returns a string that represents the TextBoxBase control.
Undoes the last edit operation in the text box.
The following are some of the commonly used events of the Text control −
|Sr.No.||Event & Description|
Occurs when the control is clicked.
Occurs when the control is double-clicked.
Occurs when the TextAlign property value changes.
In this example, we create three text boxes and use the Click event of a button to display the entered text using a message box. Take the following steps −
Drag and drop three Label controls and three TextBox controls on the form.
Change the texts on the labels to: Name, Organization and Comments, respectively.
Change the names of the text boxes to txtName, txtOrg and txtComment, respectively.
Drag and drop a button control on the form. Set its name to btnMessage and its text property to 'Send Message'.
Click the button to add the Click event in the code window and add the following code.
Public Class Form1 Private Sub Form1_Load(sender As Object, e As EventArgs) _ Handles MyBase.Load ' Set the caption bar text of the form. Me.Text = "tutorialspont.com" End Sub Private Sub btnMessage_Click(sender As Object, e As EventArgs) _ Handles btnMessage.Click MessageBox.Show("Thank you " + txtName.Text + " from " + txtOrg.Text) End Sub End Class
When the above code is executed and run using Start button available at the Microsoft Visual Studio tool bar, it will show the following window −
Clicking the Send Message button would show the following message box −