introduction to windows piprogramm ing · standard controls c# programming: from problem analysis...
TRANSCRIPT
1
Copyright © 2007Robinson College of Business, Georgia State UniversityDavid S. McDonald Director of Emerging TechnologiesTel: 404-413-7368; e-mail: [email protected]
8 Introduction to Windows P i8 Programming
David McDonald, Ph.D.Director of Emerging Technologies
C# Programming: From Problem Analysis to Program Design 2nd Edition
Chapter Objectives
Learn about graphical user interfaces
Become aware of some elements of good design
Use C# and Visual Studio to create Wi d b d li ti
C# Programming: From Problem Analysis to Program Design
Windows-based applications
2
Copyright © 2007Robinson College of Business, Georgia State UniversityDavid S. McDonald Director of Emerging TechnologiesTel: 404-413-7368; e-mail: [email protected]
Chapter Objectives (continued)
Create Windows forms and be able to h fchange form properties
Add control objects such as buttons, labels, and text boxes to a form
W k th h i l
C# Programming: From Problem Analysis to Program Design
Work through a programming example that illustrates the chapter’s concepts
Graphical User Interfaces Windows applications also look different from console applicationsInterface: front end of a program√Visual image you see when you run a
program Graphical user interface (GUI) includes:√
C# Programming: From Problem Analysis to Program Design
√Menus√Text in many different colors and sizes√Other controls (pictures, buttons, etc.)
3
Copyright © 2007Robinson College of Business, Georgia State UniversityDavid S. McDonald Director of Emerging TechnologiesTel: 404-413-7368; e-mail: [email protected]
Windows ApplicationsReference and import System.Windows.Forms namespace Class heading definition√Includes not only the class name, but a
colon followed by another class name • Derived class (first class)• Base class (second class)
C# Programming: From Problem Analysis to Program Design
Base class (second class)• public class Form1 : Form
Derived classes inherit from base class
Windows Applications (continued)Text√ A property for setting/getting title bar caption√ Can be used in constructor√ Can be used in constructor
Windows forms/controls offer many properties including Text, Color, Font, and LocationExecution begins in Main( ) method√Main( ) is located in Program.cs file for the application√ Call to Run( ) method places application in process
C# Programming: From Problem Analysis to Program Design
√ ( ) p pp ploop
4
Copyright © 2007Robinson College of Business, Georgia State UniversityDavid S. McDonald Director of Emerging TechnologiesTel: 404-413-7368; e-mail: [email protected]
// Windows0.cs Author: McDonaldusing System.Windows.Forms; // Line 1namespace Windows0{
public class Form1 : Form // Line 2{
public Form1( ) // Line 3
New namespace referenced
C t t
Base class
public Form1( ) // Line 3{
Text = "Simple Windows Application"; // Line 4}static void Main( )
{Form1 winForm = new Form1( ); // Line 5
Constructor
Sets title bar caption
C# Programming: From Problem Analysis to Program Design
( );Application.Run(winForm); // Line 6
}}
}Starts
process loop
Windows Application (continued)
Output generated
from Windows0 application
C# Programming: From Problem Analysis to Program Design
Figure 8-1 Windows-based form
5
Copyright © 2007Robinson College of Business, Georgia State UniversityDavid S. McDonald Director of Emerging TechnologiesTel: 404-413-7368; e-mail: [email protected]
Elements of Good Design Appearance matters √Human-computer interaction (HCI) p ( )
researchDesign considerations√Consistency √Alignment
C# Programming: From Problem Analysis to Program Design
√Avoid Clutter √Color √Target Audience
Use Visual Studio to Create Windows-based Applications
Windows Application
templateBrowse
to location to store
Select File New
Project
C# Programming: From Problem Analysis to Program Design
your workName
Figure 8-2 Visual Studio New Windows application
6
Copyright © 2007Robinson College of Business, Georgia State UniversityDavid S. McDonald Director of Emerging TechnologiesTel: 404-413-7368; e-mail: [email protected]
Windows-based Applications
PropertiesWindow
Switch between
Design and Code view using View
menu
C# Programming: From Problem Analysis to Program Design
Design ViewToolbox
Figure 8-3 Initial design screen
Windows-based Applications
PropertiesAuto-hide
pushpin
Dynamic Help
Solution Explorer
C# Programming: From Problem Analysis to Program Design
Figure 8-4 Dockable windows
7
Copyright © 2007Robinson College of Business, Georgia State UniversityDavid S. McDonald Director of Emerging TechnologiesTel: 404-413-7368; e-mail: [email protected]
Windows Forms
Extensive collection of Control classes Top level window for an application isTop-level window for an application is called a Form Each control has large collection of properties and methods √Select property from an alphabetized list √ p p y p
(Properties window)√Change property by clicking in the box
and selecting or typing the new entry
Windows Form Properties
Alphabetical
Events
Properties Property value
Categorized
C# Programming: From Problem Analysis to Program Design
Figure 8-5 Properties window
8
Copyright © 2007Robinson College of Business, Georgia State UniversityDavid S. McDonald Director of Emerging TechnologiesTel: 404-413-7368; e-mail: [email protected]
Windows Form Properties (continued)
C# Programming: From Problem Analysis to Program Design
Windows Form EventsAdd code to respond to events, like button clicks
From the Properties window, select the lightening bolt (Events)
√ Double-click on the event name to generate code
• Registers the event as being of interest
• Adds a heading for event-handler method
C# Programming: From Problem Analysis to Program Design
9
Copyright © 2007Robinson College of Business, Georgia State UniversityDavid S. McDonald Director of Emerging TechnologiesTel: 404-413-7368; e-mail: [email protected]
Windows Form Properties
Events button
selected
C# Programming: From Problem Analysis to Program Design
Figure 8-6 Form1 events
Windows Form – Closing Event
Code automatically added to register eventthis.Closing += new
S t C tM d l C lE tH dlSystem.ComponentModel.CancelEventHandler(this.Form1_Closing);
Code automatically added for method headingprivate void Form1_Closing(object sender,
System.ComponentModel.CancelEventArgs e){}
C# Programming: From Problem Analysis to Program Design
}You add statement to event-handler method bodyMessageBox.Show("Hope you are having fun!");
10
Copyright © 2007Robinson College of Business, Georgia State UniversityDavid S. McDonald Director of Emerging TechnologiesTel: 404-413-7368; e-mail: [email protected]
Simple Windows Application New with Visual Studio 2005, the IDE separates the source code into three separate filessource code into three separate files√ Form1.cs: Normally this is the only one you edit√ Form1.Designer.cs: Holds the auto-generated
code√ Program.cs: Contains the Main( ) method, where
execution always begins
C# Programming: From Problem Analysis to Program Design
Form1.cs and Form1.Designer.cs both include partial class definitions for the Form1 class
Windows Form Events (continued)
Expand Form1.cs node to reveal the Form1.Designer.cs
file
C# Programming: From Problem Analysis to Program Design
Figure 8-7 Solution Explorer window
11
Copyright © 2007Robinson College of Business, Georgia State UniversityDavid S. McDonald Director of Emerging TechnologiesTel: 404-413-7368; e-mail: [email protected]
Controls Controls are all classes
√Button, Label, TextBox, ComboBox, MainMenu, ListBox, CheckBox, RadioButton, and MonthCalendar
Each comes with its own predefined properties and methods
Each fires events
C# Programming: From Problem Analysis to Program Design
Each fires events
Each is derived from the System.Windows.Forms.Control class
Controls (continued)
DotsDots indicate
other classes
are derived from the
class
C# Programming: From Problem Analysis to Program Design
Figure 8-9 Control class hierarchy
12
Copyright © 2007Robinson College of Business, Georgia State UniversityDavid S. McDonald Director of Emerging TechnologiesTel: 404-413-7368; e-mail: [email protected]
Standard Controls
C# Programming: From Problem Analysis to Program Design
Figure 8-10 Windows Forms controls
Controls (continued)Two procedures to place controls
√ From Toolbox, double-click on control or drag and√ From Toolbox, double click on control or drag and drop
Move, resize, and delete controls
Format controls
√ Align controls
C# Programming: From Problem Analysis to Program Design
√Make same size
√ Horizontal and vertical spacing
13
Copyright © 2007Robinson College of Business, Georgia State UniversityDavid S. McDonald Director of Emerging TechnologiesTel: 404-413-7368; e-mail: [email protected]
Properties of the Control Class
C# Programming: From Problem Analysis to Program Design
Methods of the Control Class
C# Programming: From Problem Analysis to Program Design
14
Copyright © 2007Robinson College of Business, Georgia State UniversityDavid S. McDonald Director of Emerging TechnologiesTel: 404-413-7368; e-mail: [email protected]
Controls
C# Programming: From Problem Analysis to Program Design
Figure 8-11 GUI controls
Label Objects
Provides descriptive text or labels for other controlsInstantiate objectInstantiate object
Label labelName = new Label( );Add control to Form
this.Controls.Add(labelName);Set property values (some from Control class)√ Text; TextAlign; Font; Location
C# Programming: From Problem Analysis to Program Design
√ Text; TextAlign; Font; Location
15
Copyright © 2007Robinson College of Business, Georgia State UniversityDavid S. McDonald Director of Emerging TechnologiesTel: 404-413-7368; e-mail: [email protected]
Creating a TaxApp
C# Programming: From Problem Analysis to Program Design
Properties set for the Form container
Creating a TaxApp Form
Add Label
objects to Form
object, then
format
C# Programming: From Problem Analysis to Program Design
Figure 8-12 Formatting Label objects
16
Copyright © 2007Robinson College of Business, Georgia State UniversityDavid S. McDonald Director of Emerging TechnologiesTel: 404-413-7368; e-mail: [email protected]
Adding Labels to TaxApp Form
Add Label objects, then set their properties using thetheir properties using the
Properties window (View Properties window)
C# Programming: From Problem Analysis to Program Design
TextBox ObjectsUsed to enter data or display text during run time √ Used for both input and output
I t ti t bj tInstantiate objectTextBox textBoxName = new TextBox( );
Add control to Form this.Controls.Add(TextBoxName);
Interesting properties√MultiLine, ScollBars, MaxLength, PasswordChar,
C# Programming: From Problem Analysis to Program Design
CharacterCasing
17
Copyright © 2007Robinson College of Business, Georgia State UniversityDavid S. McDonald Director of Emerging TechnologiesTel: 404-413-7368; e-mail: [email protected]
TextBox Objects (continued)
C# Programming: From Problem Analysis to Program Design 33
Adding TextBox Objects to TaxApp Form
Add TextBox objects, then set their property
values
C# Programming: From Problem Analysis to Program Design
18
Copyright © 2007Robinson College of Business, Georgia State UniversityDavid S. McDonald Director of Emerging TechnologiesTel: 404-413-7368; e-mail: [email protected]
ButtonEnables user to click button to perform task
√ If button has event-handler method and is registered as an event to which your program is planning toas an event to which your program is planning to respond, event-handler method is called automatically when button clicked
Button object’s properties, methods, and events
√ Inherits from Control (Table 8-2 & 8-3, slides 25 & 26)
C# Programming: From Problem Analysis to Program Design
• Text, Enabled, Focused, TabIndex
Adding Button Objects to TaxApp Form
Add Button objects, then set their property
values
C# Programming: From Problem Analysis to Program Design