TextBox
Users often need to enter text into programs. TextBox
is the simplest way to implement this capability. We often used the Text property and its TextChanged
event.
Open a new VB.NET Windows Forms project in Visual Studio. Open the Toolbox, and then drag a TextBox
control onto the window of your project.
A TextChanged
event will be automatically inserted into your VB.NET code. This code is triggered whenever a character is typed (or deleted) from the TextBox
.
TextBox
, TextChanged
will also be triggered.TextBox
reference. You could use the TextBox1
directly in your code, but this is often less flexible.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
The example shows some important principles of using TextBox
. We almost always use the Text property on TextBox
—and TextChanged
tends to lead to clearer program logic.
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.