Page 117 - DCAP408_WEB_PROGRAMMING
P. 117
Unit 7: Character Sets, Fonts and the Keyboard
7.6 Implementing a Simple Keyboard Interface Notes
The .NET Framework Windows Form client is a huge platform for providing a rich, interactive
and receptive user interface. The rich interface of a WinForm client, correctly constructed, can
propose productivity advantages to the information worker far and above other standard
architectures. There are many reasons for augmented productivity but one reason occurs from
how simple it is to influence the keyboard in a WinForm client.
The Microsoft .NET Framework occurs packaged with all the building blocks to execute a
keyboard interface in a WinForm application.
Remember that constancy is critical when implementing any interface, particularly a keyboard
interface. Just as simple as it is to augment productivity with the keyboard, it is even simpler to
reduce productivity by implementing a badly thought out and conflicting keyboard interface.
Example: If the F1 key pulls up a search box in one form of the application, and removes
records in another form it can be harmful to the application all together. This is why the approaches
discussed will concentrate on keeping the keyboard interface reliable while at the same time
make the job of implementing the interface painless and competent for the .NET developer.
At the fundamental level capturing and taking action off of a keyboard command is easy and
uncomplicated. By managing one of a few keyboard events (KeyDown, KeyUp, or KeyPress) on
almost any WinForm .NET Control, keystrokes can be captured and suitable action taken. The
severe problem with this is that the keyboard event would have to be managed individually for
every control on the form to confirm that keyboard commands were always captured. It means
that if a form had 5 buttons, 5 events would have to be managed and coded. This is neither
practical nor competent for the .NET Developer and so causes the requirement to handle the
keyboard events at the form level, in spite of what control has focus. The following discusses
how to implement an event handler on the form that captures keystrokes.
Open Microsoft Visual Studio and generate a new Windows Form Application. Position 2 textbox
controls and 2 button controls on the form. Open up the properties panel for the default form
and rename the form to “MainForm”. There is then a property on the form to set to true that is
by default, false. The ‘KeyPreview’ property on the form causes keyboard events fired on controls
on the form to be recorded with the form first. By managing the events at the form level it
removes the requirement to handle them independently at the control level. Once the
‘KeyPreview’ property is set to true the subsequent step is to create the event handler on
MainForm to manage the KeyDown event. Double-clicking the KeyDown event in the Properties
window of MainForm will produce the event handler in code. Use the code block below to
inhabit the KeyDown method.
private void MainForm_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Escape)
MessageBox.Show(“Escape Key”, “Keyboard Command”);
else if (e.KeyCode == Keys.F1)
LOVELY PROFESSIONAL UNIVERSITY 111