C# TextBox

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.

TextBox in Windows Forms example

Intro

Net

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.

Tip:In GUI programming, you will handle custom events provided by the TextBox to know when to execute your logic rules.

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

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.

TextBoxProperty

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.

Tip:If you see an event handler you want to add, double click on the row in the Properties pane.

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.

Note:The method signatures must be compatible. This means they must have the same number and types of arguments.

TextChanged

Lightning bolt

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.

Program that uses TextBox and TextChanged event: C#

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;
	}
    }
}
TextBox

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.

Also:The TextChanged row in the Properties pane can be used to create or assign the TextChanged event handler.

KeyDown

Key: used to access value

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.

Windows Forms class that uses KeyDown on TextBox: C#

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?");
	    }
	}
    }
}
TextBoxEnum type

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

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 Examples

Multiline

Programming tip

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.

Tip:The next parts of this tutorial show how to save the contents of a multiline TextBox to a file on the disk.

TextBox

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

Note

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.

Windows Forms program that writes text from TextBox: C#

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);
	}
    }
}

Result

This is some text
written for the textbox tutorial
in the site dotnetperls.com
Note

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.

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 Handling

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.


C#: Windows