0% found this document useful (0 votes)
338 views

Keypress Event in C#: Sitemap About Home C# Ajax Interview Questions About

This document discusses handling keyboard input in C# Windows Forms applications. It explains that keyboard events like KeyDown, KeyPress, and KeyUp are raised in response to keyboard input. It provides code examples for handling the Enter key press specifically and detecting arrow keys. It also distinguishes between the different keyboard events, such as KeyDown occurring when a key is pressed and KeyUp occurring after a key is released.

Uploaded by

Ammar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
338 views

Keypress Event in C#: Sitemap About Home C# Ajax Interview Questions About

This document discusses handling keyboard input in C# Windows Forms applications. It explains that keyboard events like KeyDown, KeyPress, and KeyUp are raised in response to keyboard input. It provides code examples for handling the Enter key press specifically and detecting arrow keys. It also distinguishes between the different keyboard events, such as KeyDown occurring when a key is pressed and KeyUp occurring after a key is released.

Uploaded by

Ammar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Net-informations.

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#

Windows Forms processes keyboard input by raising


keyboard events in response to Windows messages. Most
Windows Forms applications process keyboard input
exclusively by handling the keyboard events.

How do I detect keys pressed in C#

You can detect most physical key presses by handling the


KeyDown or KeyUp events. Key events occur in the following order:

KeyDown
KeyPress
KeyUp

How to detect when the Enter Key Pressed in C#

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

How to get TextBox1_KeyDown event in your C# source file ?


Select your TextBox control on your Form and go to Properties window.
Select Event icon on the properties window and scroll down and find the
KeyDown event from the list and double click the Keydown Event. The you
will get the KeyDown event in your source code editor.

private void textBox1_KeyDown(.....)


{

Difference between the KeyDown Event, KeyPress Event and KeyUp


Event

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

How to Detecting arrow keys in C#

In order to capture keystrokes in a Forms control, you


must derive a new class that is based on the class of
the control that you want, and you override the
ProcessCmdKey().

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)


{
//handle your keys here
}

More about.... Detecting arrow keys from C#

KeyUp Event :

The following C# source code shows how to capture Enter KeyDown event
from a TextBox Control.

Next :  How to create Dynamic Controls in C# ?

Download Source Code


Print Source Code

using System;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void textBox1_KeyUp(object sender, KeyEventArgs e)


{
if (e.KeyCode == Keys.Enter)
{
MessageBox.Show("Enter key pressed");
}
}
}
}

 C# Visual Studio IDE


 How to Create a C# Windows Forms Application
 C# Label Control
 C# Button Control
 C# TextBox Control
 C# ComboBox
 C# ListBox Control
 C# Checked ListBox Control
 C# RadioButton Control
 C# CheckBox Control
 C# PictureBox Control
 C# ProgressBar Control
 C# ScrollBars Control
 C# DateTimePicker Control
 C# Treeview Control
 C# ListView Control
 C# Menu Control
 C# MDI Form
 C# Color Dialog Box
 C# Font Dialog Box
 C# OpenFile Dialog Box
 C# Print Dialog Box
 How to create Dynamic Controls in C# ?
 Keep Form on Top of All Other Windows
 C# Timer Control

Share Facebook Twitter Email Google+

 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

 C# Crystal Reports Tutorial


 CSharp Communication Tutorial
 C# Ado.Net Tutorial and Source Code , c# database programming tutorial
 C# ADO.NET data Providers Tutorial
 C# Dataset Tutorial
 C# DataAdapater Tutorial
 Csharp DataView Tutorial
 Csharp Remoting Tutorial
 C# XML Tutorial
 C# DataGridView Tutorial

  More Source Code :   

Mail to :  [email protected]    

net-informations.com (C) 2015    Founded by raps mk


All Rights Reserved. All other trademarks are property of their respective owners.

You might also like