Home
VB.NET.WinForms
TextBox Example
Updated Oct 6, 2022
Dot Net Perls
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 pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
This page was last updated on Oct 6, 2022 (edit).
Home
Changes
© 2007-2025 Sam Allen