144
❘
CHAPTER 8
Creating WindoWs Forms appliCations
The next step is to create the window class,
frmMain
, which inherits from
System.Windows.Forms
.Form
. It’s important to remember that this class is simply a blueprint, as all classes are, and that
your application can create as many instances of it as required. This is the reason that every element
in the code is prefaced with
self
, which refers to the current instance.
You can perform the configuration tasks found in
InitializeComponent()
in any order. Most developers
like to create the controls first, followed by the client area,
as shown in Listing 8-1. However, you must add the con-
trols to the window after you configure them, or the con-
trols will appear partially configured onscreen (and make
for a difficult debugging chore). For this reason, the code
calls on
self.Controls.Add()
last to add the controls to
the window. Figure 8-2 shows the output from the example
based on the configuration criteria in Listing 8-1.
It’s easy to get lost while configuring the controls, espe-
cially if you have multiple control levels to consider (such
as when using container controls). Most controls require
five configuration steps and you want to perform them in
this order to ensure you accomplish them all:
1.
Instantiate the control.
2.
Set the control’s output text, if any.
3.
Set the control’s position on the window (starting from the upper-left corner).
4.
(Optional) Set the control’s size when the default size won’t work.
5.
(Optional) Add control enhancements, such as the tooltips discussed in the section “Making
the Application Easier to Use,” which follows.
Making the Application Easier to Use
One interesting point about using IronPython to create a Windows Forms application is that it tends
to reduce the number of unnecessary bells and whistles. However, don’t make your application so
Spartan that the user spends a lot of head-scratching time trying to figure out how to use it. You
have to create a balance between the amount of code you must write and the needs of the user.
An easy addition is the use of speed keys. Simply type an ampersand (&) in front of the letter you
want to use for a speed key. When the user presses Alt+, the application selects
the associated control. Providing speed keys is incredibly easy, yet has a big impact on user produc-
tivity and also makes it possible for users who can’t work with a mouse to use your application. The
sample application described in Listing 8-1 already has this feature.
When users rely on the keyboard instead of the mouse, they also want to select the controls in order —
from left to right and from top to bottom (unless your language has a different natural order for reading
text). In some cases, this means changing the
TabIndex
property. The form defaults to the tab order
provided by the order in which the controls appear in the code. Careful placement of the controls often
negates the need to change the
TabIndex
property.
FIguRE 8-2:
The form created by this
example is simple but works for demonstra-
tion purposes.
548592c08.indd 144
2/24/10 12:48:09 PM
www.finebook.ir
Do'stlaringiz bilan baham: |