TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to C-SHARP

C# KeyCode Property and KeyDown

KeyCode handles key presses. You have a TextBox control on your Windows Forms application and need to detect Enter. The TextBox allows your users to type letters into it, and you need to detect when a certain key is pressed.

Here: Our solution involves the KeyDown event in our C# Windows Form and the KeyCode property.

Example. In the Visual Studio designer, click on your TextBox control in the form display. You will see the Properties Pane. Click on the lightning bolt icon. This icon stands for events: we use it to create events.

Then: In the event tab, scroll to KeyDown, and double click in the space to the right. A new block of code will appear.

Example that uses KeyDown event handler: C# using System; using System.ComponentModel; using System.Windows.Forms; using System.Linq; namespace WindowsProgramNamespace { public partial class TextWindow : Form { public TextWindow() { InitializeComponent(); } private void textBox1_KeyDown(object sender, KeyEventArgs e) { } } }
Example 2. Here is the code that detects when Enter is pressed. The trick is to examine the "KeyEventsArg e" and check KeyCode. Keys is an enumeration, and it stores special values for many different keys pressed. KeyCode must be compared to Keys.
KeyDown implementation: C# private void textBox1_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) { // Enter (return) was pressed. // ... Call a custom method when user presses this key. AcceptMethod(); } }
Keys. Here we see some values you can test against the KeyCode property. You can do this the same way as we tested Keys.Enter in the previous example. This is useful for developing data-driven user interfaces.

Keys.Shift: Indicates the shift key was pressed. You could use this to prevent a user from typing uppercase letters.

Keys.Tab: Could use this to prevent user from tabbing out of a TextBox that isn't valid.

Keys.OemPipe, Keys.Oem: Some of these are apparently original equipment manufacturer specific. They may vary on the keyboard being used.

Keys.NumPad: Useful for data entry applications. You could setup the program to detect whether the numeric pad is being used.

Summary. Here we saw how to use KeyCode in the KeyDown event to check against the Keys enumeration to detect the Enter key. You can check many other keys also by testing the e.KeyCode in the KeyDown event.

Note: If you need to detect Shift, Tab, or an Arrow key, the e.KeyCode property can be ideal.

© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf