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.
Many events and properties are available on this control. We often use the TextChanged
event handler alongside the Text property.
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.
TextChanged
event handler to a TextBox
by double-clicking on the TextBox
in the Windows Forms designer.TextChanged
row in the Properties pane can be used to create or assign the TextChanged
event handler.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.
KeyDown
event handler which is called before the key value is painted.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?");
}
}
}
}
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.
TextBox
Text property and write it to a location on the hard disk.WriteAllText()
will write the string
data returned by the Text property to the physical disk.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
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.
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.
TextBox
control are listed in the Properties pane in Visual Studio.Keys
enumerationWe 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.
KeyCode
value is an enum
, you can use it as a switch
evaluation expression for faster code.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.
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.
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.