Treating Keystrokes systematically in C#.Net


Walking around the C#.Net 2015 may be little bit fun. Today I will introduce keystroke and show how it can help to move focus between controls.
Sendkeys Object in .Net frame work help you to fire some key stroke, which will enable functions such as move focus forward and backward with the send() method.

 Lets’ start with building a Key Press event for the following Pcode Textbox.

Making of the Key Press Event

Go to the Property Box – Choose Event – locate Key Press and enter Key Press event name as “EnterKey_Treatment” and hit enter, will bring the method structure code.

Now select all controls with Ctrl + Click and select Key Press Event as  EnterKey_Treatment. You can also do it individually. This would save our time indeed. 

Applying Keystroke

For proper working of the keystroke for moving from one box to another, you have keep the Tab Index of all boxes in as sequential order, i.e, 0,1,2 and so on.Let’s fill this cup with some hot tea.

But the MoveToMox and common static class not yet defined, we need the fuel from the common class.( A static class act like a library, it can be called anywhere in the project). 

Static class for Keystroke (MoveToBox)

A static class can contain only static members, and it can access without creating the object, it act like C# library for our project.
For utilizing the KeyPressEventArgs(accessing the keys the user press), have to reference System.Windows.Forms. The new class would look like,

Create a single method for key Press Event and call the commons. That is all you need to know.

Let’s test the program.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.