Tamil cinema News,Videos,Songs,Photos and Tailers :: Tamil Cinema Bazaar

Pages

Infolinks In Text Ads

Friday 4 November 2011

TextBox control key events in C#

Detect key events

Here we look at how you can read key down events in the TextBox control in Windows Forms using the C# language. The Windows Forms system provides several key-based events, and this tutorial uses the KeyDown event handler which is called before the key value actually is painted on the TextBox. 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, and 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 WindowsFormsApplication20
{
    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?");
     }
 }
    }
}
 
Keys enumeration. The Windows Forms platform provides a Keys enumeration that you can use to test individual key values. One of the simplest ways to test keys is to type "Keys" and press period, and then use the IntelliSense feature in Visual Studio to scroll through all the possible key values. Because the KeyCode value is of type Keys enum, you can use it as a switch evaluation expression for faster code as well.
Showing messages and dialogs. The above class text demonstrates the MessageBox.Show method, which is the easiest way to display a dialog box in the Windows Forms system.
 

No comments:

Post a Comment

Note: only a member of this blog may post a comment.