introduction to windows programming - electrical...

Post on 25-Aug-2018

217 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

C# Programming: From Problem Analysis to Program Design 1

Introduction to

Windows

Programming

C# Programming: From Problem Analysis to Program Design

4th Edition

9

2

Chapter Objectives

3

Contrasting Windows and Console

Applications

Main()

C# Programming: From Problem Analysis to Program Design 4

Graphical User Interfaces

5

Windows Applications

• public class Form1 : Form

6

Windows Applications (continued)

• Text

Main()

– Main() Program.cs

Run()

using System.Windows.Forms; // Line 1

namespace Windows0

{

public class Form1 : Form // Line 2

{

public Form1( ) // Line 3

{

Text = "Simple Windows Application"; // Line 4

}

static void Main( )

{

InitializeComponent(); this.Text = "Simple Windows Application";

}

}

}

1

System.Windows.Forms

Form

Form1 winForm = new Form1( ); Application.Run(winForm);

2

3

4,5

using System.Windows.Forms; namespace Lesson09Prep { public partial class Form1 : Form { public Form1() { InitializeComponent(); this.Text = "Simple Windows Application"; } } }

1

System.Windows.Forms

Form

Form1 winForm = new Form1( ); Application.Run(winForm);

2

3

4,5

Windows Application - Example

9

Windows Application (continued)

Generated

output

10

Elements of Good Design

11

Use Visual Studio to Create Windows-Based Applications

Windows Forms App

(.Net Framework )

Name

Figure 9-2 Visual Studio New Windows application

Select

File New

Project

12

Windows-Based Applications

Properties

Window

Toolbox

Design View

13

Windows-Based Apps

Toolbox

C# Programming: From Problem Analysis to Program Design 14

Windows Forms

15

Windows Form Properties

Properties Property value

Windows Form Properties

C# Programming: From Problem Analysis to Program Design 16

Figure 9-6 Form events

Windows Form Properties

(continued)

17

Inspecting the Code Generated

by Visual Studio

18

Expand Form1.cs

node to reveal the

Form1.Designer.cs

file

Inspecting the Code - Form1.cs

InitializeComponent( )

19

namespace MyFirstWindowsApp { partial class Form1 { /// <summary> /// ... #region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.SuspendLayout(); // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(629, 455); this.Name = "Form1"; this.Text = "Form1"; this.ResumeLayout(false); } #endregion } }

20

Windows Form Events

Form1.cs

21

Windows Form Properties (continued)

Events button selected

22

Windows Form – Closing Event

public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { MessageBox.Show("Bye - Hope you had fun!", "Leaving this app"); } } }

23

Controls (continued)

Dots

indicate

other

classes

are

derived

from the

class

Figure 9-9 Control class hierarchy

24

Properties of the Control Class

Table 9-2 Systems.Windows.Forms.Control class properties

Properties of the Control Class

(continued)

25 Table 9-2 Systems.Windows.Forms.Control class properties

Methods of the Control Class

26

Table 9-3 includes a

short list of some of the

many methods.

Explore MSDN for

more documentation

Pizza App

27

Pizza App – Form1 Desing

28

btnPlaceYourOrder btnCancelAndClear

lblSides

grpCheese

comSides Items (collection) None Breadsticks Cheesesticks Garlic knots Fries

grpCheese radNormal radDouble radNone

grpShape radRounded radSquared

grpToppings chkPepperoni chkMushroom chkOnions chkAnchovies

txtCustomerName txtPhone

Pizza App – Form2

29

btnQuit

txtOderSummary

btnBack

Pizza App – Form1.cs 1 of 3

30

namespace WindowsFormsApp { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void comSides_SelectedIndexChanged(object sender, EventArgs e) { string selectedItem = comSides.Items[comSides.SelectedIndex].ToString(); if (selectedItem == "None") { lblSides.Text = "Summary of sides: "; } else { lblSides.Text += selectedItem + ", "; } } private void btnPlaceOrder_Click(object sender, EventArgs e) { //this string holds all choices made string choices = ""; //(EXCLUSIVE) obtaining the pizza's shape if (radRounded.Checked) choices += "Rounded, "; else if (radSquared.Checked) choices += "Squared, ";

Pizza App – Form1.cs 2 of 3

31

//(EXCLUSIVE) looking into cheese selection if (radCheeseNormal.Checked) choices += "Normal cheese, "; else if (radCheeseDouble.Checked) choices += "Double cheese, "; else if (radCheeseNone.Checked) choices += "No cheese, "; //(INCLUSIVE) collecting all toppings checked by the customer if (chkPepperoni.Checked) choices += "Pepperoni, "; if (chkMushroom.Checked) choices += "Mushroom, "; if (chkOnions.Checked) choices += "Onions, "; if (chkAnchovies.Checked) choices += "Anchovies, "; //(MULTIPLE) extracting Beverage elections from the CheckedListBox var itemList = chkListBeverage.CheckedItems; foreach (var item in itemList) { choices += item + ", "; } //obtaining customer's name and phone choices += "\n" + txtCustomerName.Text + " " + txtPhone.Text; MessageBox.Show(choices, "Quick Summary"); //call other form to display summary Form2 f2 = new Form2(choices, this); f2.ShowDialog(); //try f2.Show(); }

Pizza App – Form1.cs 3 of 3

32

private void btnCancel_Click(object sender, EventArgs e) { //clear all side selections lblSides.Text = "Summary of sides: "; //clear all beverage selections for (int i = 0; i < chkListBeverage.Items.Count; i++) { chkListBeverage.SetItemCheckState(i, CheckState.Unchecked); } //individually, clear each topping option chkPepperoni.CheckState = CheckState.Unchecked; chkOnions.CheckState = CheckState.Unchecked; chkMushroom.CheckState = CheckState.Unchecked; chkAnchovies.CheckState = CheckState.Unchecked; } } }

Pizza App – Form2.cs 1 of 1

33

namespace WindowsFormsApp { public partial class Form2 : Form { //this variable will be used to remember who called us Form1 form1Reference; public Form2() { InitializeComponent(); } //we added this constructor to accept the order's text and //a reference to the form that called us public Form2(string orderSummaryValue, Form1 callerRefValue) { InitializeComponent(); txtOrderSummary.Text = orderSummaryValue; form1Reference = callerRefValue; } private void btnBack_Click(object sender, EventArgs e) { //go back to our caller, close current form form1Reference.Show(); this.Close(); } private void btnQuit_Click(object sender, EventArgs e) { this.Close(); //closes current form Application.Exit(); //terminates application } } }

C# Programming: From Problem Analysis to Program Design 34

Creating a TaxApp

Properties set for the Form

container

Table 9-4 TaxApp Form1 properties

C# Programming: From Problem Analysis to Program Design 35

Creating a TaxApp Form

Figure 9-12 Formatting Label objects

Add Label

objects to

Form

object…

Use

options on

FORMAT

menu

C# Programming: From Problem Analysis to Program Design 36

Adding Labels to TaxApp Form

Table 9-5 TaxApp label5 object properties

Add Label objects, then set

their properties using the

Properties window

(View Properties window)

C# Programming: From Problem Analysis to Program Design 37

TextBox Objects

• Used to enter data or display text during run time

– Used for both input and output

• Instantiate object

TextBox textBoxName = new TextBox( );

• Add control to Form this.Controls.Add(TextBoxName);

• Interesting properties

– MultiLine, ScollBars, MaxLength, PasswordChar, CharacterCasing

C# Programming: From Problem Analysis to Program Design 38

TextBox Objects (continued)

Table 9-6 TextBox properties

TextBox Objects (continued)

C# Programming: From Problem Analysis to Program Design 39

Table 9-6 TextBox properties (continued)

C# Programming: From Problem Analysis to Program Design 40

Adding TextBox Objects to

TaxApp Form Add TextBox objects,

then set their property

values

Table 9-7 TaxApp TextBox objects property changes

C# Programming: From Problem Analysis to Program Design 41

Button

• Enables 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 to respond,

event-handler method is called automatically when

button clicked

• Button object’s properties, methods, and events

– Inherits from Control (Table 9-2 & 9-3, slides 31-33)

• Text, Enabled, Focused, TabIndex

C# Programming: From Problem Analysis to Program Design 42

Adding Button Objects to

TaxApp Form

Add

Button

objects,

then set

their

property

values

Table 9-7 TaxApp button1 properties

C# Programming: From Problem Analysis to Program Design 43

Adding Button

Objects to

TaxApp Form

(continued)

Figure 9-14 Events

C# Programming: From Problem Analysis to Program Design 44

Adding Button Objects to

TaxApp Form (continued)

•When you double-click on event, an event-

handler method is created: private void btnCompute_Click(object

sender, System.EventArgs e)

{

}

•AND registers click event: this.btnCompute.Click +=

new System.EventHandler

(this.btnCompute_Click);

C# Programming: From Problem Analysis to Program Design 45

Adding Button Objects to

TaxApp Form (continued) private void btnCompute_Click(object sender, System.EventArgs e)

{

string inValue;

double purchaseAmt, percent, ans;

inValue = txtPurchase.Text;

while (double.TryParse(txtPurchase.Text,out purchaseAmt)==false)

{

MessageBox.Show("Value entered must be numeric");

txtPurchase.Text = "0.0";

txtPurchase.Focus();

} Review TaxApp Example

C# Programming: From Problem Analysis to Program Design 46

Adding Button Objects to

TaxApp Form (continued) btnCompute_Click( ) ( … continued)

inValue = label5.Text; //inValue previously declared as string

inValue = inValue.Remove(inValue.Length-1, 1);

percent = double.Parse(inValue) / 100;

ans = (purchaseAmt * percent) + purchaseAmt;

txtTotalDue.Text = String.Format("{0:C}", ans).ToString();

}

Parse( ) used

here as opposed

to TryParse( )

…since value is

being retrieve

from TextBox

C# Programming: From Problem Analysis to Program Design 47

TaxApp Form

Figure 9-15 Tax calculator output

AcceptButton

property on the

form

was set to

btnCompute

C# Programming: From Problem Analysis to Program Design 48

TempAgency

Application

Example

Figure 9-16 Problem specification for TempAgency

C# Programming: From Problem Analysis to Program Design 49

TempAgency Application

Example (continued)

Table 9-9 Instance field members for the Employee class

TempAgency Application

Example (continued)

C# Programming: From Problem Analysis to Program Design 50

Table 9-10 Constant field members for the Employee class

C# Programming: From Problem Analysis to Program Design 51

Figure 9-17 Prototype for TempAgency example

TempAgency Application Example

(continued)

C# Programming: From Problem Analysis to Program Design 52

TempAgency

Application

Example

(continued)

Figure 9-18 Class diagrams for TempAgency

example

C# Programming: From Problem Analysis to Program Design 53

Algorithm for TempAgency

Figure 9-19 Pseudocode for the Employee class for the

TempAgency example

C# Programming: From Problem Analysis to Program Design 54

Test Data for TempAgency

Table 9-11 Desk check test plan of TempAgency example

C# Programming: From Problem Analysis to Program Design 55

Properties for TempAgency

Table 9-12 Properties set for the TempAgency example

C# Programming: From Problem Analysis to Program Design 56

TempAgency Properties (continued)

Table 9-12 Properties set for the TempAgency example

C# Programming: From Problem Analysis to Program Design 57

Properties for TempAgency

Table 9-12 Properties set for the TempAgency example

C# Programming: From Problem Analysis to Program Design 58

Properties for TempAgency

Table 9-12 Properties set for the TempAgency example

C# Programming: From Problem Analysis to Program Design 59

Properties for TempAgency

Table 9-12 Properties set for the TempAgency example

C# Programming: From Problem Analysis to Program Design 60

Properties for TempAgency

Table 9-12 Properties set for the TempAgency example

C# Programming: From Problem Analysis to Program Design 61

Properties for TempAgency

Table 9-12 Properties set for the TempAgency example

C# Programming: From Problem Analysis to Program Design 62

Properties for TempAgency

Table 9-12 Properties set for the TempAgency example

Review TempAgency Example

C# Programming: From Problem Analysis to Program Design 63

TempAgency Example

Figure 9-20 First user interface for the payroll application

C# Programming: From Problem Analysis to Program Design 64

TempAgency Example (continued)

Figure 9-21 Output produced when the Calculate button is clicked

Coding Standards • Guidelines for Naming Controls

– Consistency

– Use appropriate prefix for controls

C# Programming: From Problem Analysis to Program Design 65

Table 9-13 Example

prefix identifiers for

controls

Resources

C#: Windows Controls –

http://csharpcomputing.com/Tutorials/Lesson9.htm

Visual C# Tutorials - Windows Forms –

http://visualcsharptutorials.com/windows-forms

Beginners Guide To User Interface Design in C# –

http://www.thetechlabs.com/interfaces/user-interface-design/

Free C# Tutorials –

http://www.homeandlearn.co.uk/csharp/csharp.html

C# Programming: From Problem Analysis to Program Design 66

C# Programming: From Problem Analysis to Program Design 67

Chapter Summary

• Windows versus console applications

• Graphical user interfaces

• Elements of good design

• Visual Studio with Windows-based applications

– Drag-and-drop construction

C# Programming: From Problem Analysis to Program Design 68

Chapter Summary (continued)

• Properties

– Getters

– Setters

• Controls as objects

– Buttons

– Labels

– TextBox

top related