Page 118 - DCAP408_WEB_PROGRAMMING
P. 118
Windows Programming
Notes MessageBox.Show(“F1 Key”, “Keyboard Command”);
else if (e.KeyCode == Keys.Enter)
MessageBox.Show(“Enter Key”, “Keyboard Command”);
}
The above code exhibits a message box with a message when the Escape, F1 or Enter key is
pressed. Run the application and observe that regardless of what control has focus on the form,
the KeyDown event on the form manages the keyboard command.
Figure 7.2
The KeyEventArgs utilized in the KeyDown event can also be used identify when modifiers are
used (a grouping of keys, such as ‘Ctrl+Alt’) and even control if the keyboard command is
passed down to the primary control.
The problem by means of this approach on each individual form is efficiency for the .NET
Developer. By means of this approach in larger applications with many forms is not only
incompetent but prone to bring in constancy issues. An application with number of forms will
need the .NET Developer to individually setup and configure the keyboard events at a quantifiable
impact to time and cost. This is where implementing an approach to expand .NET Controls and
customizing events occurs in. By merging customized .NET Controls and inheritance the needed
work on the Developer can be decreased while increasing the constancy in the way the keyboard
interface is implemented.
There is another approach to capturing all keystrokes at the main form level. It includes
implementing an IMessageFilter interface on the form level. While this is more effectual in
some cases to capture all keystrokes it does have the potential of degrading performance as
message filters are being added to the message pump for the application.
Self Assessment
Fill in the blank:
8. The ‘.........................’ property on the form causes keyboard events fired on controls on the
form to be recorded with the form first.
112 LOVELY PROFESSIONAL UNIVERSITY