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.
Getting started. 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.
Example program. 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.
Also If another part of the code changes the text of a TextBox, TextChanged will also be triggered.
Detail We cast the sender Object to a 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
Some notes. 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.
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.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.