Forms and Controls

advertisement
Forms and Controls
Part 3 dbg --- Naming conventions,
Button, Event handlers, Label
The Form as a Platform
• A Windows application form can act as a
container for additional objects.
• Objects that can be contained by forms (and are
coded by existing classes in the Foundation
Class Library) are called controls.
2
Ways to Add Controls via the Toolbox
It is possible to add controls to a form by manually
coding the form class. But, the IDE simplifies this
process by automatically adding the necessary code
as you add a control to the form by:
1. Dragging a control from the Toolbox and dropping it on
the Designer View of the form.
2. Double-clicking a control icon in the Toolbox to add a
copy of that control to a form.
3. Clicking on a control icon in the Toolbox and “drawing”
the control on the form.
4. Clicking on a control icon in the Toolbox and then
3
clicking again on the form.
A Form with One Button
• Using one of the 4 described techniques, we will
add a Button to a new form.
• We will program a handler function that will
respond to the Click event, generated when the
mouse is clicked while its pointer is positioned
over the button.
• This event is termed a Button.Click event.
4
Programming the Click Handler Function
• We will create a new application project and
program its single event handler function “stepby-step”.
• When the button is clicked, the handler function
will be executed and the form will close.
5
Programming the Click Handler Function
• Double-clicking a control on the
form will automatically create a
default event handler skeleton
and the necessary “event wiring”.
• A default handler is the most
likely handler for a particular
object.
• The Click handler is the default
handler for a button control.
6
Naming and Renaming
Strategies and Conventions
Object Naming for Clarity
• At times, we have accepted default object
names, generated by the IDE, for these first
examples.
• To avoid ambiguity and enforce clarity, it is a
good idea to use a standard naming convention
for all the objects that you will create for your
Windows applications.
• We will follow some accepted conventions as we
complete the next simple application project.
8
Naming Strategies
• Give all objects meaningful and memorable
names.
• Use a simple three-character prefix to identify
controls.
• Capitalize the first letter of multiple-word names;
this is called camel-casing.
Ex. frmOrderConfirmation
btnCalcShippingCost
• A list of appropriate standard object prefixes has
been provided as a handout.
• Assign meaningful names to projects and
solutions (namespaces) as well.
9
Form Class File
• A default class that defines the startup form is
created with each new Windows application
project.
• This class is stored in a separate file with the
default name Form1.cs.
• Your first task in developing a new Windows
application should be to rename this file.
10
Renaming the Form Class File
• Highlight the Form1.cs file in the
Solution Explorer Window.
• Right-click the file and select
Rename.
• Use the prefix frm for the new file
name. Assign a meaningful
name to the file, remembering to
retain the original .cs file
extension.
11
Renaming the Form Class
• As you rename the form object with a unique
and meaningful name, V2005 asks you if you
want to use this new name in 3 locations? You
should answer Yes. The new name will replace
Form1 at these locations:
– Application.Run(new Form1()); // startup object in Main()
– Form1.cs //in Solution Explorer
– Form1.Designer.cs //in Solution Explorer
• While the Form Class file is selected in the
Solution Explorer, switch to the Designer View.
12
Renaming the Form Class
• Select the form in the Designer and
assign a meaningful name to its
Name property using the Property
Window.
• It is usually best to use the same
name for the class that you
assigned to the class file, including
the frm prefix.
• No .cs extension here.
13
Add and Rename a Button
• Use one of the techniques described earlier to
add a new Button object to the form.
• Select the button in the designer and assign a
new Name using the Property Window; use the
btn prefix.
14
Add a Default Event Handler
• The Click event is the default
event for a Button object.
• We can add the default event
for the Button by simply
double-clicking the button in
the Designer.
• The code view of the class will
display, complete with the new
default event handler function.
15
Running the Close() Method
• The Form class has a Close() method.
• Since we want to close the current object (the
form) when we click the Close button, we will run
the Close() method on our form by typing
this.Close(); in the click handler for the Close
button.
• The keyword this is used as a shorthand or alias
for the current object.
16
 CloseHandler
Controls for Text Output
The Label
Text with Labels
• A label is a simple control that exists only to
display text. Label controls are used to display
headings, informational messages, and output
information.
• In C#.NET 2005, a label is sized automatically
based on the amount of text that it contains (it
will resize automatically if you change the
FontSize property). As a result, you can’t size a
label by dragging its handles unless you change
its AutoSize property to False (allowing multiline word wrap).
18
When to Assign a Custom Name
• Labels that are merely used as headings or
informational messages, and whose contents do
not change at run time, can use the assigned
default names (Label1, Label2, etc).
• Labels whose appearance or contents will
change at run time should be assigned
meaningful names that begin with the prefix, lbl.
19
Assigning Text at Design Time
• Text may be assigned to labels by using the Text
Property at design time.
20
 DesignTimeLabelText
Assign Text During Run Time
• Although the user can not change the text of a
label directly at run time, handlers for events
raised by other controls or the form itself can be
programmed to change the contents of a label.
• Here, click handlers for two buttons and a
collection of menu items are programmed to
change the Text property of a label at run time.
21
 RunTimeLabelText
A Plan for Designing the Solution
1. Decide which controls will be needed and
which events will be needed. Use Planning
Guides on next slide.
2. Draw a sketch on paper with names for
controls that will have events associated with
them, and those whose property values will
change. Use the Designer to place controls
on form.
3. Set (design-time) property values.
4. Write event handlers, which may change
some property values at run-time.
22
Let’s Do One Together
• Chapter 1, exercise 1.5, page 49
• Plan the Objects
• Plan the Events
Object
Property
(Forms and
Controls)
Action
(pseudocode)
Setting
(design-time)
Object
Event
(Forms and
Controls)
23
Download