TextBox ExampleUse the TextBox control. Access the Text property and the TextChanged event handler.
This page was last reviewed on Oct 6, 2022.
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.
Shows a textbox
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.
Shows a textbox
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.
This page was last updated on Oct 6, 2022 (edit).
© 2007-2024 Sam Allen.