Home
C#.WinForms
TextBox Examples
This page was last reviewed on Mar 3, 2023.
Dot Net Perls
TextBox. This control lets users type letters and enter data. It is part of the Windows Forms platform and is used with C# code. It is added with the Visual Studio designer.
Shows a textboxShows a textbox
Many events and properties are available on this control. We often use the TextChanged event handler alongside the Text property.
RichTextBox
MaskedTextBox
TextChanged. You can use the TextChanged event to modify another part of your program when the user types text. This program assigns the window's title text to the text entered.
And This makes the base window's title reflect the user's input. The Windows task bar will also show this text.
Tip You can add the TextChanged event handler to a TextBox by double-clicking on the TextBox in the Windows Forms designer.
Also The TextChanged row in the Properties pane can be used to create or assign the TextChanged event handler.
Shows a textbox
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void textBox1_TextChanged(object sender, EventArgs e) { // // This changes the main window text when you type into the TextBox. // this.Text = textBox1.Text; } } }
KeyDown. You can read key down events in the TextBox control in Windows Forms. The Windows Forms system provides several key-based events.
Info This tutorial uses the KeyDown event handler which is called before the key value is painted.
Detail You can cancel the key event in the Key Down event handler as well, although this is not demonstrated.
Here The program will display an alert when the Enter key is pressed. An alternative alert message when the Escape key is pressed.
Shows a textbox
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void textBox1_KeyDown(object sender, KeyEventArgs e) { // // Detect the KeyEventArg's key enumerated constant. // if (e.KeyCode == Keys.Enter) { MessageBox.Show("You pressed enter! Good job!"); } else if (e.KeyCode == Keys.Escape) { MessageBox.Show("You pressed escape! What's wrong?"); } } } }
Files. You can make the TextBox control write a file to disk. After adding the multiline TextBox, we can add a Button and add the button1_Click event handler.
Tip In that event handler, we can access the TextBox Text property and write it to a location on the hard disk.
Detail WriteAllText() will write the string data returned by the Text property to the physical disk.
Note After you run this program, you can open the "demo.txt" file and the text you typed into the program will be there.
using System; using System.IO; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { // // This is the button labeled "Save" in the program. // File.WriteAllText("C:\\demo.txt", textBox1.Text); } } }
This is some text written for the textbox tutorial in the site dotnetperls.com
Text. The Text property returns a string that represents the exact characters in the TextBox. You can use the TextChanged event to monitor this property for changes.
Events. In Visual Studio the lightning bolt symbol describes event handlers. You can use the lightning bolt icon in the Properties dialog to add the most important event handlers.
Tip Some useful event handlers on the TextBox control are listed in the Properties pane in Visual Studio.
Also If you want to hook an event handler to a method that already exists in your program, use the drop-down menu on the Properties pane.
Keys enumeration. We can test individual key values with the Keys property. You can type "Keys" and press period, and then scroll through all the possible key values.
Tip Because the KeyCode value is an enum, you can use it as a switch evaluation expression for faster code.
Dialogs. We can use the MessageBox.Show method alongside a TextBox. This is the easiest way to display a dialog box in the Windows Forms system.
MessageBox.Show
Multiline. You can use the Multiline property on the TextBox control to create a longer text input area. For shorter multiline input boxes, this is useful.
Summary. TextBox provides a way for the user to input text with the keyboard. And the Text property enables easy access to the input entered into the program.
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 Mar 3, 2023 (edit).
Home
Changes
© 2007-2024 Sam Allen.