TextBox` 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.`420px`300px`Intro.` First, the TextBox will take care of reading the keys and displaying the text. But it will not do anything with the text it accepts. You will need to access the text from the TextBox based on custom logic rules. `In GUI programming, you will handle custom events provided by the TextBox to know when to execute your logic rules.`Event `event`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 `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.`420px`360px`Some useful event handlers` on the TextBox control are listed in the Properties pane in Visual Studio. If you cannot locate the Properties pane, visit the View, Properties Window menu item. `If you see an event handler you want to add, double click on the row in the Properties pane.`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.`The method signatures must be compatible. This means they must have the same number and types of arguments.`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.`This program` assigns the base window's title text to the text typed in by the user to the TextBox. This makes the base window's title reflect the user's input. The Windows taskbar will also show this text. `420px`300px`You can` add the TextChanged event handler to a TextBox by double-clicking on the TextBox in the Windows Forms designer. You can also add the event handler by typing C# code in the Form1() constructor to add the event handler manually. `The TextChanged row in the Properties pane can be used to create or assign the TextChanged event handler.`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.`You can cancel` the key event in the KeyDown event handler as well, although this is not demonstrated. The program will display an alert when the Enter key is pressed. An alternative alert message when the Escape key is pressed. `420px`300px`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. `Because the KeyCode value is of type Keys enum, you can use it as a switch evaluation expression for faster code as well.`Switch `switch`Enum `enum`Showing messages and dialogs.` The above class demonstrates the MessageBox.Show method. This is the easiest way to display a dialog box in the Windows Forms system. This site contains a MessageBox.Show tutorial. `MessageBox.Show `messagebox-show`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. `The next parts of this tutorial show how to save the contents of a multiline TextBox to a file on the disk.`420px`300px`Using TextBox with button.` The screenshot shows a TextBox that was modified in the designer to have its multiline property set to true. The form also has a Button control with the text "Save" on it. `Next: `We show how to use this Save button to write the text contained in TextBox to a file.`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.`After adding the multiline TextBox,` we can add a Button and add the button1_Click event handler. In that event handler, we can access the TextBox Text property and write it to a location on the hard disk. `The example calls` the File.WriteAllText method. This method will take the string data pointed to by the string reference returned by the Text property. It then actually writes that to the physical hard disk on the computer. `After you run this program, you can open the "demo.txt" file and the text you typed into the program will be there.`File `file`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. `RichTextBox `richtextbox`MaskedTextBox `maskedtextbox`TextBox.AppendText `textbox-appendtext

234 33; 333.ComponentModel; 33.Data; 33.Drawing; 33.Linq; 33.Text; 33.Windows.Forms3namespace WindowsFormsApplication1 { 23partial 3Form1 : Form 2{ 223Form1() 22{ 222InitializeComponent(); 22} 223 3textBox1_TextChanged(object sender, EventArgs e) 22{4 222// 2223This changes the main window text when you type 3o the TextBox. 222// 2224this.Text3textBox1.Text; 22} 2} }4 33; 333.ComponentModel; 33.Data; 33.Drawing; 33.Linq; 33.Text; 33.Windows.Forms3namespace WindowsFormsApplication1 { 23partial 3Form1 : Form 2{ 223Form1() 22{ 222InitializeComponent(); 22} 223 3textBox1_KeyDown(object sender, KeyEventArgs e) 22{4 222// 2223Detect the KeyEventArg's key enumerated constant. 222// 22243e.KeyCode3Keys.Enter) 222{ 2222MessageBox.Show("You pressed enter! Good job!"); 222} 222else 3e.KeyCode3Keys.Escape) 222{ 2222MessageBox.Show("You pressed escape! What's wrong?"); 222} 22} 2} }4 33; 33.IO; 33.Windows.Forms3namespace WindowsFormsApplication1 { 23partial 3Form1 : Form 2{ 223Form1() 22{ 222InitializeComponent(); 22} 223 3button1_Click(object sender, EventArgs e) 22{4 222// 2223This is the button labeled "Save"3the program. 222// 2224File.3AllText("C:\\demo.txt", textBox1.Text); 22} 2} } 4Result4 This is some text written 3the textbox tutorial in the site dotnetperls.com4

#mb8TextBox and TextChanged eventKeyDown on TextBoxwrites text from TextBox