part03dbg

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 .NET libraries) are
called controls.
2
Ways to Add Controls via the Toolbox
There are several ways to add a control to a form:
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
that control to the upper left-hand corner of the 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
clicking again on the form.
As you add controls to your form via the Designer,
code to create the objects and add the controls and
their events to various collections is automatically
added to the formname.Designer.cs file.
3
A Form with One Button
• Using one of the 4 described techniques, we will
add a Button object to a new form.
• We will program a event handler method 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 method “stepby-step”.
• When the button is clicked, the event handler
method 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 event handler is the
most likely event handler for a
particular object.
• The Click handler is the default
event 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
• 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, VS2005 asks you if you want
to use this new name in 3 locations? Yes. The
new name will replace Form1 in 3 places:
– 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 method.
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.
 CloseHandler
16
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.
 DesignTimeLabelText
20
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.
 RunTimeLabelText
21
A Plan for Designing the Solution
1. Decide which controls will be needed and
which events will be needed. Use Planning
Guides on next slides.
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
Create a project to display the daily specials for
“your” diner. Make up a name for your diner and
display it in a label at the top of the form. Add a
label to display the appropriate special depending
on the button that is pressed. The buttons should
be:
– Soup of the Day
– Chef’s Special
– Fish of the Day
Also include an Exit button.
23
Plan First, Then Design
• Plan the Objects
Object
Property
(Forms and Controls)
Setting
(design-time)
• Plan the Events
Action
(pseudocode)
Object
Event
(Forms and Controls)
24
Download