Home

Search

C# TextBox Examples

Use the TextBox control to allow text to be entered. Create a TextBox with the Visual Studio designer.

dot net perls

TextBox. This 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. Many events and properties are available on this control.RichTextBoxMaskedTextBox

TextChanged. You can use the TextChanged event to modify another part of your program when the user types text into a TextBox. The TextChanged event is only triggered when the text is changed to something different, not changed to the same value.
Here: This program assigns the base window's title text to the text typed in by the user to the TextBox.
And: This makes the base window's title reflect the user's input. The Windows taskbar 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.
C# program that uses TextBox and TextChanged event 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. This tutorial uses the KeyDown event handler which is called before the key value actually is painted.
Cancel: You can cancel the key event in the KeyDown 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.
C# program that uses KeyDown on 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 do something that could be useful in a real program: write the file to disk. This is essentially a primitive word processor. But don't use it to write anything important yet.
And: 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.
WriteAllText: This method 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.
File
C# program that writes text from TextBox 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); } } } Output 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 at any time. You can access the Text property at any time. And you can use the TextChanged event to monitor this property for changes.Property

Events. In Visual Studio the lightning bolt symbol describes event handlers. Windows Forms programs are primarily event-based, which means 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. The Windows Forms platform provides a Keys enumeration that you can use to test individual key values. You can type "Keys" and press period, and then use IntelliSense to scroll through all the possible key values.
Tip: Because the KeyCode value is of type Keys enum, you can use it as a switch evaluation expression for faster code as well.
SwitchEnum

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. This site contains a MessageBox.Show tutorial.MessageBox

Multiline. You can use the Multiline property on the TextBox control to create a longer text input area. The TextBox control has performance problems with large amounts of text. But for shorter multiline input boxes, this is useful.

Summary. We looked at the TextBox control. This control provides a powerful way for the user to input text with the keyboard or other input device. It provides the Text property, which enables easy access to the input entered into the program.

Home

© 2007-2020 sam allen. send bug reports to info@dotnetperls.com.