Forms and controls
1 / 24

Forms and Controls - PowerPoint PPT Presentation

  • Uploaded on

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.

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
Download Presentation

PowerPoint Slideshow about 'Forms and Controls' - ahmed-barker

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
Forms and controls

Forms and Controls

Part 3 dbg --- Naming conventions, Button, Event handlers, Label

The form as a platform
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.

Ways to add controls via the toolbox
Ways to Add Controls via the Toolbox

There are several ways to add a control to a form:

  • Dragging a control from the Toolbox and dropping it on the Designer View of the form.

  • Double-clicking a control iconin the Toolbox to add a that control to the upper left-hand corner of the form.

  • Clicking on a control icon in the Toolboxand“drawing” the control on the form.

  • Clicking on acontrol iconin 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.

A form with one button
A Form with One Button

  • Using one of the 4 described techniques, we will add a Buttonobject 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.

Programming the click handler function
Programming the Click Handler Function

  • We will create a new application project and program its single event handler method “step-by-step”.

  • When the button is clicked, the event handler method will be executed and the form will close.

Forms and controls

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.

Naming and renaming

Naming and Renaming

Strategies and Conventions

Object naming for clarity
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.

Naming strategies
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.


  • Assign meaningful names to projects and solutions (namespaces) as well.

Form class file
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.

Renaming the form class file
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.

Renaming the form class
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.

Renaming the form class1
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.

Add and rename a button
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.

Add a default event handler
Add a Default Event Handler

  • The Click eventis 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.

Running the close method
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

Text with labels
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 multi-line word wrap).

When to assign a custom name
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.

Assigning text at design time
Assigning Text at Design Time

  • Text may be assigned to labels by using the Text Property at design time.

 DesignTimeLabelText

Assign text during run time
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

A plan for designing the solution
A Plan for Designing the Solution

  • Decide which controls will beneeded and which events will be needed. Use Planning Guides on next slides.

  • Draw asketch 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.

  • Set(design-time) property values.

  • Write event handlers, which may change some property values at run-time.

Let s do one together
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.

Plan first then design
Plan First, Then Design

  • Plan the Objects

  • Plan the Events