Visual Basic .NET - Textbox Control

The TextBox control in VB.NET is used for accepting user input in text format. 

Properties:

  • Text: Gets or sets the text in the control.
  • ReadOnly: Gets or sets a value indicating whether the control is read-only.
  • Multiline: Gets or sets a value indicating whether this is a multiline textbox.
  • PasswordChar: Gets or sets the character used to mask characters entered in a single-line TextBox control.

Methods:

  • Clear(): Clears the contents of the TextBox.
  • SelectAll(): Selects all text in the control.
  • Copy(): Copies the current selection in the control to the clipboard.
  • Paste(): Pastes the contents of the clipboard into the control.

Events:

  • TextChanged: Occurs when the Text property value changes.
  • KeyPress: Occurs when a character is pressed while the control has focus.
  • LostFocus: Occurs when the control loses focus.

Example:

Suppose you have a form that asks the user to enter their name in a TextBox control, and you want to display a message box when the user clicks a button. Here's a simple example:

  • Drag and drop a TextBox control and a Button control onto the form.
  • Set the Name property of the TextBox control to "txtName" and set the Text property of the Button control to "Submit".
  • Double-click the Button control to create a Click event handler.
  • In the event handler, add the following code:
MessageBox.Show("Hello, " + txtName.Text + "!");

Run the application and type your name into the TextBox control.

Click the Submit button to see the message box displaying "Hello, [your name]!".

This is a simple example of using the TextBox control and the Button control to accept user input and perform an action based on that input.

Assuming you have a form with a single TextBox control named txtInput, you can use the following code to display a message box with the text entered by the user when they click a button:

Private Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
    Dim userInput As String = txtInput.Text
    MessageBox.Show("You entered: " & userInput)
End Sub

In this example, the btnSubmit_Click event handler is triggered when the user clicks the button with the name btnSubmit. The code then retrieves the text entered by the user in the txtInput control using the Text property and assigns it to a variable named userInput. Finally, a message box is displayed showing the text entered by the user concatenated with the message "You entered: ".