HomeSearch

VB.NET TextBox Example

Use the TextBox control. Access the Text property and the TextChanged event handler.

TextBox. Users often need to enter text into programs. The TextBox control in Windows Forms is the simplest way to implement this capability. With its Text property, and its TextChanged event, we have a powerful and easy-to-use input control.

To start, please open a new VB.NET Windows Forms project in Visual Studio. Open the Toolbox window from the View menu. Then, drag a TextBox control onto the window of your project.

Next: The TextBox will appear on your form. Open the Properties panel and click on the lightning bolt icon.

Events: The lightning bolt will show you events that you can add. Double-click on the TextChanged event.

A TextChanged event will be automatically inserted into your VB.NET code. Here you can add statements that do whatever you want. This code is triggered whenever a character is typed (or deleted) from the TextBox.

Also: If another part of the code changes the text of a TextBox, TextChanged will also be triggered.

Method that handles TextChanged event: VB.NET Public Class Form1 Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged ' Get TextBox reference from sender object. ' ... The TextBox1 could be used directly instead. Dim t As TextBox = sender ' Assign the window's title to the Text of the TextBox. ' ... This is a string value. Me.Text = t.Text End Sub End Class

We cast the sender Object to a TextBox reference. You could use the TextBox1 directly in your code, but this is often less flexible. We access the Text property on the TextBox. We mirror the Text to the Window title.

Tip: This example does nothing useful, but it shows some important principles of using TextBox.

Text: We almost always use the Text property on TextBox. And TextChanged tends to lead to clearer program logic.

Property

Summary. A text box is used in almost all Windows Forms programs. It provides no formatting support (RichTextBox should be used when formatting is needed). But its simplicity is a benefit. It is easy and clear to use.
Home
Dot Net Perls
© 2007-2020 Sam Allen. Every person is special and unique. Send bug reports to info@dotnetperls.com.