






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







♦ Chapter Overview
♦ Chapter Objectives
♦ Instructor Notes
♦ Quick Quizzes
♦ Discussion Questions
♦ Projects to Assign
♦ Key Terms
Chapter Overview
In this chapter, students extend their knowledge of programming by creating applications that capture and respond to user input. A different way of programming based on interactively responding to events such as mouse clicks is introduced. Windows-based applications using the .NET Framework Class Library are built. Classes that enable highly interactive, attractive, graphical user interface (GUI) applications are introduced. The drag-and-drop construction approach available with Visual Studio is illustrated.
Chapter Objectives
In this chapter, students will:
Instructor Notes
With a console-based application, each line in the Main ( ) method is executed sequentially. Then the program halts. Method calls might branch to different locations in your program; however, control always returns to Main ( ). Programs retrieve data by calling the operating system to get data, and they call on the operating system to output data through method calls such as WriteLine( ) or Write( ).
Once launched, the Windows application sits in a process loop waiting for an event— notification from the operating system that an action, such as the user clicking the mouse or pressing a key, has occurred. Instead of calling on the operating system with a request, as console applications do, Windows applications receive messages from the operating system that an event has occurred. Methods called event handlers indicate what should be done when an event occurs.
The heading added is: public class Form1 : Form
The new class inherits all the functionality of the base class. Form is a predefined .NET class that includes a number of methods and properties used to create a basic Windows screen.
Windows forms and controls offer a wide variety of changeable properties, including Text, Color, Font, Size, and Location. Much of the power of Visual Studio lies in having these properties readily available and easy to change or add through IntelliSense. Text is a property that can be used to set or get the caption of the title bar for the window.
Execution for Windows-based programs begins in the Main( ) method. The first statement instantiates or creates an object of the Form1 class (Form1 winForm = new Form1( );). The last statement in the body of Main( ) calls the Run( ) method (Application.Run(winForm);). Application class is also defined as part of the System.Windows.Forms namespace. The object that is instantiated from the Form1 class is sent as the argument to its Run( ) method. This Run( ) method call is what displays the form and places the application in a process loop so that it receives messages from the operating system when events of interest to the application are fired. The Exit( ) method stops an application and closes all of its windows.
Quick Quiz
Appearance matters! The goal should be to develop applications that are usable, that permit users to spot items in the windows quickly, and that enable users to interact with your program. A large field of research, called human-computer interaction (HCI), concentrates on the design and implementation of interactive computing systems for human use. A few HCI considerations include consistency, alignment, avoiding clutter, color, and the target audience.
Consistency Consistent placement of items is important. Consistent sizing of items is important. Consistent appearance of items that are performing the same functionality is also important. Unless you are trying to call attention to an item, keep the item the same as other items. Use contrast to bring attention.
Alignment Use alignment for grouping items. When you place controls on a form, place similar items together. They can be lined up to call attention to the fact that they belong together.
Avoid Clutter Do not crowd a form with too many controls. Fonts should be large enough to be legible.
Color Background colors and foreground text colors should contrast each other. Avoid using bright colors (such as reds), especially for backgrounds—this can result in user eye fatigue. You can use color as an attention getter, but do not abuse it. Color can play a significant role in your design.
Target Audience Think about the design of an interface in terms of who will be using it and on what platform it will be displayed.
Quick Quiz
Visual Studio automatically generates code when you create a Windows application. Begin by selecting File>New>Project and selecting Visual C# Project as the Project Type with Windows Application selected from the list of Templates. Browse to the location where you want to store your work. Use the View menu to display different windows, such as the Toolbox , Solution Explorer , Class View , Properties , and Dynamic Help windows.
Comparing the Code of Example 9-1 to Example 9- Visual Studio uses a method named InitializeComponent ( ) to build the form at run time. The call to this method is the only statement in the constructor. All code setting the properties that are set during design are placed in InitializeComponent ( ) method.
Each of the properties are suffixed with the keyword this, referring to the current instance of a class. In the call to the Run ( ) method ( Application.Run(new Form1( )); ), no identifier is associated with the Form1 object. The new keyword creates an anonymous (unnamed) instance of Form1. The Form1 object identifier is not used anywhere else. Thus, there is no reason to name it.
The Dispose ( ) method is added by Visual Studio to perform memory management. The automatic garbage collector releases memory from the application after the program is halted. It cleans up or releases unused resources back to the operating system.
Windows Form Events The topic of Chapter 10 is handling events. One way to add a method to handle events in Visual Studio is to find the event in the list of events (from the Properties window with the lightning bolt (Events) ) and double-click on the event name. Code is automatically added to the program. In the InitializeComponent ( ) method, the event is registered as being of interest using a statement similar to: this.FormClosing += new System.Windows.Forms.FormClosingEventHandler (this.Form1_FormClosing); The above statement registers the program to be notified when a user is closing the application. In addition, an event-handler method heading and an empty body are added to the code as shown here: private void Form1_FormClosing(object sender, FormClosingEventArgs e) { }
Quick Quiz
System.Windows.Forms namespace includes controls with names such as Button, Label, TextBox, ComboBox, MainMenu, ListBox, CheckBox, RadioButton, and MonthCalendar. Both of these controls are classes with properties and methods. Each fires events—some of which you should write methods for—indicating what to do when its event is fired. A lot of code is added automatically for you when you place the control on the form.
All of the control classes that can be added to a form are derived from the System.Windows.Forms.Control class, which means they inherit the characteristics of the Control class. Call attention to Figures 9-8 and 9-9. They show the controls that can be added.
Place, Move, Resize, and Delete Control Objects Either double-click the control or drag and drop the control to the correct location on the form to place it. Resize controls using the standard Windows conventions of positioning the mouse pointer over the resizing handles. Clicking the Delete or Backspace key, once the control is selected, removes the control visually from the Form Designer view but also removes all the constructor code and associated property changes relating to the selected object.
The Windows Forms Designer in Visual Studio has a SnapLine feature to help you accomplish precise layout of controls on your form. Horizontal and vertical line segments are dynamically created by the SnapLine feature to assist in the layout of controls.
Options under the Format menu, including Align, Make Same Size, Horizontal Spacing, and Vertical Spacing, should be explored. After the controls are selected, use, for example, Align to align lefts, rights, or centers of controls. The Make Same Size selection is useful for sizing objects for consistency. Note that when you select more than one control to format, the last control selected is the one used as the template for the action. The Horizontal and Vertical Spacing options enable you to increase or decrease the amount of blank space displayed between objects.
Methods and Properties of the Control Class Call attention to Table 9-2 for a list of Control properties inherited by control objects such as buttons and labels. Table 9-3 includes a list of the control methods. Some of the methods provide the same functionality as one or more of the properties. For example, the Show ( ) method functions the same way as setting the Visible property to true; Hide( ) does the same thing as setting the Visible property to false.
Derived Classes of the System.Windows.Form.Control Class Figure 9-11 shows a visual representation of many of the controls you add to a form. They are labeled.
This application is created to demonstrate the design and implementation of a graphical user interface for a Windows application. An application for a temp agency is designed that accepts as input the contractor’s name, number of dependents, and the number of hours worked. It displays the gross pay, deductions, and the net pay for the worker.
Two classes are constructed to separate the business logic from the presentation details. The Employee class defines a template for a typical payroll employee object. It includes the behaviors for determining withholding deductions and calculating the net take-home pay. The second class instantiates objects of the .NET predefined Control classes (labels, text boxes, and button) and adds those objects to the Form object.
Structured English (pseudocode) and class diagrams are included with the example. A prototype for the desired output is shown. Tables are shown documenting the properties that were set during design. The complete program listing is shown in the book and is available as a Visual Studio project to demo for the class.
CODING STANDARDS Encourage students to develop a consistent style for naming controls. By prefixing the control name with an abbreviation representing the control type (i.e., btn for button), IntelliSense works better.
Discussion Questions
Some interesting topics of discussion in this chapter include:
Projects to Assign
All of the Multiple Choice Exercises, Problems 1- Odd-numbered Short Answer Exercises, Problems 21- Programming Exercises, Problems 3, 5, 9, and 10
Key Terms
¾ controls : objects that can display and respond to user interaction ¾ derived class : new class that that inherits the characteristics of the base class ¾ event : a notification from the operating system that an action, such as the user clicking the mouse or pressing a key, has occurred
¾ event handlers : methods that indicate what should be done when an event such as a mouse click on a button or the press of a key occurs ¾ graphical user interface (GUI) : the menus, buttons, pictures, and text front end portion of the program ¾ Human-Computer Interaction (HCI) : design and implementation of interactive computing systems for human use ¾ interface : the front end of a program—the visual image you see when you run a program ¾ preprocessor directive : directive indicating what should be done prior to processing the program statements ¾ Windows applications : applications that are usually graphical and that use the event model and receive messages from the operating system that an event has occurred