C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Tip: In GUI programming, you will handle custom events provided by the TextBox to know when to execute your logic rules.
EventTip: 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.
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.
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;
}
}
}
Also: The TextChanged row in the Properties pane can be used to create or assign the TextChanged event handler.
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?");
}
}
}
}
Tip: Because the KeyCode value is of type Keys enum, you can use it as a switch evaluation expression for faster code as well.
SwitchEnumTip: The next parts of this tutorial show how to save the contents of a multiline TextBox to a file on the disk.
Next: We show how to use this Save button to write the text contained in TextBox to a file.
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.
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 dotnetCodex.com
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