VB Text box control


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.


The Properties of the TextBox Control

The following are some of the commonly used properties of the TextBox control:

S.NPropertyDescription
1AcceptsReturnGets 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.
2AutoCompleteCustomSourceGets or sets a custom System.Collections.Specialized.StringCollection to use when the AutoCompleteSourceproperty is set to CustomSource.
3AutoCompleteModeGets or sets an option that controls how automatic completion works for the TextBox.
4AutoCompleteSourceGets or sets a value specifying the source of complete strings used for automatic completion.
5CharacterCasingGets or sets whether the TextBox control modifies the case of characters as they are typed.
6FontGets or sets the font of the text displayed by the control.
7FontHeightGets or sets the height of the font of the control.
8ForeColorGets or sets the foreground color of the control.
9LinesGets or sets the lines of text in a text box control.
10MultilineGets or sets a value indicating whether this is a multiline TextBox control.
11PasswordCharGets or sets the character used to mask characters of a password in a single-line TextBox control.
12ReadOnlyGets or sets a value indicating whether text in the text box is read-only.
13ScrollBarsGets or sets which scroll bars should appear in a multiline TextBox control. This property has values:
  • None
  • Horizontal
  • Vertical
  • Both
14TabIndexGets or sets the tab order of the control within its container.
15TextGets or sets the current text in the TextBox.
16TextAlignGets or sets how text is aligned in a TextBox control. This property has values:
  • Left
  • Right
  • Center
17TextLengthGets the length of text in the control.
18WordWrapIndicates whether a multiline text box control automatically wraps words to the beginning of the next line when necessary.

The Methods of the TextBox Control

The following are some of the commonly used methods of the TextBox control:

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

Events of the TextBox Control

The following are some of the commonly used events of the Text control:

S.NEventDescription
1ClickOccurs when the control is clicked.
2DoubleClickOccurs when the control is double-clicked.
3TextAlignChangedOccurs when the TextAlign property value changes.

Example

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:

  1. Drag and drop three Label controls and three TextBox controls on the form.
  2. Change the texts on the labels to: Name, Organization and Comments, respectively.
  3. Change the names of the text boxes to txtName, txtOrg and txtComment, respectively.
  4. Drag and drop a button control on the form. Set its name to btnMessage and its text property to 'Send Message'.
  5. 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 = "fastlearning.in"
   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

Share this article on