Defining Windows Forms ❘
145 Windows can also have default actions. The two most common default actions occur when you press
Enter (default acceptance) and Escape (default cancel). Providing controls for these two features helps
users move quickly through a form and can speed processing of wizards. Here’s the code you use to
provide default actions for the example application.
self.AcceptButton = self.btnOK
self.CancelButton = self.btnCancel
You must configure the controls for a form before you configure any default actions. The control must exist before you make the assignment to either self
.AcceptButton
or self.CancelButton
. Otherwise, you receive an ambiguous error message when you run the application that will prove difficult to debug. Another useful enhancement that doesn’t require a lot of work is the
ToolTip
component. Using
tooltips makes it easier for the user to figure out how an application works. These bits of mini-help
are quite useful and they provide all that many users need to become proficient quickly. Tooltips
also make it easy for someone who hasn’t used the application for a while to get back up to speed
quickly. Tooltips also provide input to screen readers (commonly used by those with special visual
needs) so that the screen reader can tell the user the control’s purpose. Here’s the code used to add
ToolTip
to the window.
# Add a tooltip control.
self.Tips = System.Windows.Forms.ToolTip()
You don’t add the
ToolTip
component to the window itself using
self.Controls.Add()
because
ToolTip
lacks a visual interface. The
ToolTip
component sits in the background and waits for a visual
control to require its services. The following code shows how to add a
ToolTip
to individual controls.
self.Tips.SetToolTip(self.btnOK, ‘Displays an interesting message.’)
The
SetToolTip()
method of the
ToolTip
adds a tooltip to the control specified by the first argu-
ment. The message appears as the second argument. Figure 8-3 shows a typical example of a tooltip
created using this technique.
Visual Studio provides a wealth of addi-
tional enhancements that usually won’t
require a lot of implementation time, but
can make a big difference to the user.
The best way to determine how to add
these enhancements is to add them to a
C# or Visual Basic.NET application and
then view the Visual Designer file. For
example, you might want to add informa-
tion to the
AccessibleDescription
,
AccessibleName
, and
AccessibleRole
FIguRE 8-3: Adding tooltips is easy and
helps most users considerably.
548592c08.indd 145
2/24/10 12:48:09 PM
www.finebook.ir