Keypress Event in C#: Sitemap About Home C# Ajax Interview Questions About
Keypress Event in C#: Sitemap About Home C# Ajax Interview Questions About
com
SiteMap | About
Home
C#
VB.NET
ASP.NET
AJAX
.Net Framework
Interview Questions
About
keyPress event in C#
Handle Keyboard Input at the Form Level in C#
KeyDown
KeyPress
KeyUp
The following C# code behind creates the KeyDown event handler. If the key
that is pressed is the Enter key, a MessegeBox will displayed .
if (e.KeyCode == Keys.Enter)
{
MessageBox.Show("Enter Key Pressed ");
KeyDown Event : This event raised as soon as the user presses a key on
the keyboard, it repeats while the user keeps the key depressed.
KeyPress Event : This event is raised for character keys while the key is
pressed and then released. This event is not raised by noncharacter keys,
unlike KeyDown and KeyUp, which are also raised for noncharacter keys
KeyUp Event : This event is raised after the user releases a key on the
keyboard.
KeyPress Event :
using System;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Enter)
{
MessageBox.Show("Enter key pressed");
}
if (e.KeyChar == 13)
{
MessageBox.Show("Enter key pressed");
}
}
}
}
KeyDown Event :
using System;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
MessageBox.Show("Enter key pressed");
}
}
}
}
KeyUp Event :
The following C# source code shows how to capture Enter KeyDown event
from a TextBox Control.
using System;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
An overview of Microsoft C#
C# Language Tutorial
C# Statements Tutorial
C# Graphical User Interface Tutorial
C# Collection Tutorial
C# String Tutorial
C# File Operations Tutorial
C# Excel Tutorial
Mail to : [email protected]