C# cont’d

50
1 C# cont’d (C-sharp) (many of these slides are extracted and adapted from Deitel’s book and slides, “How to Program in C#”. They are provided for CSE3403 students only. Not to be published or publicly distributed without permission by the publisher).

Transcript of C# cont’d

Page 1: C# cont’d

1

C# cont’d(C-sharp)

(many of these slides are extracted and adapted from Deitel’s book and slides, “How to Program in C#”. They are provided for CSE3403 students only. Not to be

published or publicly distributed without permission by the publisher).

Page 2: C# cont’d

2

1. Windows Forms2. Event-Handling Model3. Basic Event Handling4. Control Properties and Layout5. Labels, TextBoxes and Buttons6. GroupBoxes and Panels7. CheckBoxes and RadioButtons

Page 3: C# cont’d

3

Introduction

• Graphical user interface– Allow interaction with program visually– Give program distinct look and feel– Built from window gadgets – Is an object, accessed via keyboard or mouse

Page 4: C# cont’d

4

Some GUI componentsFrame

TextBox

ScrollbarMenu Bar

Button

Page 5: C# cont’d

5

IntroductionControl Description Label An area in which icons or uneditable text can be displayed.

TextBox An area in which the user inputs data from the keyboard. The area also can display information.

Button An area that triggers an event when clicked. CheckBox A GUI control that is either selected or not selected. ComboBox A drop-down list of items from which the user can make a selection,

by clicking an item in the list or by typing into the box, if permitted. ListBox An area in which a list of items is displayed from which the user can

make a selection by clicking once on any element. Multiple elements can be selected.

Panel A container in which components can be placed. ScrollBar Allows the user to access a range of values that cannot normally fit in

its container.

Some basic GUI components.

Page 6: C# cont’d

6

Windows Forms

• WinForms– Create GUIs for programs– Element on the desktop– Represented by:

• Dialog• Window• MDI window

Page 7: C# cont’d

7

Windows Forms• Component

– Class that implements IComponent interface– Lacks visual parts

• Control– Component with graphical part

• Such as button or label– Are visible

• Event– Generated by movement from mouse or keyboard– Event handler performs action

• Specifics written by programmer

Page 8: C# cont’d

8

Windows Forms

Components and controls for Windows Forms.

Page 9: C# cont’d

9

Windows FormsForm Properties and Events

Description / Delegate and Event Arguments

Common Properties AcceptButton Which button will be clicked when Enter is pressed. AutoScroll Whether scrollbars appear when needed (if data fills more than one

screen). CancelButton Button that is clicked when the Escape key is pressed. FormBorderStyle Border of the form (e.g., none, single, 3D, sizable). Font Font of text displayed on the form, as well as the default font of

controls added to the form. Text Text in the form’s title bar. Common Methods Close Closes form and releases all resources. A closed form cannot be

reopened. Hide Hides form (does not release resources). Show Displays a hidden form. Common Events (Delegate EventHandler, event arguments EventArgs) Load Occurs before a form is shown. This event is the default when the

form is double-clicked in the Visual Studio .NET designer.

Common Form properties and events.

Page 10: C# cont’d

10

Event-Handling Model

• GUIs are event driven• Event handlers

– Methods that process events and perform tasks.• Associated delegate

– Objects that reference methods– Contain lists of method references

• Must have same signature

– Intermediaries for objects and methods– Signature for control’s event handler

Page 11: C# cont’d

11

Basic Event Handling

• Event handler– Must have same signature as corresponding delegate– Two object reference are passed in– ControlName_EventName– Must be registered with delegate object

• Add event handlers to the delegate’s invocation list

– New delegate object for each event handler• Event multicasting

– Have multiple handlers for one event– Order called for event handlers is indeterminate

Page 12: C# cont’d

12

Basic Event Handling

Selected event

Event description

List of events supported by control

Events icon

Current event handler (none)

Events section of the Properties window.

Page 13: C# cont’d

13

SimpleEventExample.cs

Program Output

Click in the Form area … get MessageBox pop-up

Page 14: C# cont’d

14

SimpleEventExample.cs1 // SimpleEventExample.cs2 // Using Visual Studio .NET to create event handlers.4 using System;5 using System.Drawing;6 using System.Collections;7 using System.ComponentModel;8 using System.Windows.Forms;9 using System.Data;11 // program that shows a simple event handler12 public class MyForm : System.Windows.Forms.Form13 {14 private System.ComponentModel.Container components = null;15 16 // Visual Studio .NET generated code18 [STAThread]19 static void Main() 20 {21 Application.Run( new MyForm() );22 }24 // Visual Studio .NET creates an empty handler, 25 // we write definition: show message box when form clicked

26 private void MyForm_Click( object sender, System.EventArgs e )

27 {

28 MessageBox.Show( "Form was pressed" );

29 }

30 31 } // end class MyForm

Page 15: C# cont’d

15

Basic Event Handling

Class name List of events

List of Form events.

Page 16: C# cont’d

16

Control Properties and Layout• Common properties

– Derive from class Control– Text property

• Specifies the text that appears on a control– Focus method

• Transfers the focus to a control• Becomes active control

– TabIndex property• Order in which controls are given focus• Automatically set by Visual Studio .NET

– Enable property• Indicate a control’s accessibility

Page 17: C# cont’d

17

Control Properties and Layout• Visibility control

– Hide control from user• Or use method Hide

• Anchor property– Anchoring control to specific location

• Constant distance from specified location– Unanchored control moves relative to the position– Docking allows control to spread itself along an entire side– Both options refer to the parent container

• Size structure– Allow for specifying size range

• MinimumSize and MaximumSize property

Page 18: C# cont’d

18

Control Properties and LayoutClass Control Properties and Methods

Description

Common Properties

BackColor Background color of the control. BackgroundImage Background image of the control. Enabled Whether the control is enabled (i.e., if the user can interact with it). A

disabled control will still be displayed, but “grayed-out”—portions of the control will become gray.

Focused Whether a control has focus. (The control that is currently being used in some way.)

Font Font used to display control’s Text. ForeColor Foreground color of the control. This is usually the color used to

display the control’s Text property. TabIndex Tab order of the control. When the Tab key is pressed, the focus is

moved to controls in increasing tab order. This order can be set by the programmer.

TabStop If true, user can use the Tab key to select the control. Text Text associated with the control. The location and appearance varies

with the type of control. TextAlign The alignment of the text on the control. One of three horizontal

positions (left, center or right) and one of three vertical positions (top, middle or bottom).

Visible Whether the control is visible.

Common Methods

Focus Transfers the focus to the control. Hide Hides the control (sets Visible to false). Show Shows the control (sets Visible to true).

Class Control properties and methods.

Page 19: C# cont’d

19

Control Properties and Layout

Before resize After resize

Constant distance to left and top sides

Anchoring demonstration.

Page 20: C# cont’d

20

Control Properties and Layout

Darkened bar indicates to which wall control is anchored

Click down-arrow in Anchor property to display anchoring window

Manipulating the Anchor property of a control.

Page 21: C# cont’d

21

Control Properties and Layout

Control expands along top portion of the form

Docking demonstration.

Page 22: C# cont’d

22

Control Properties and LayoutCommon Layout Properties

Description

Common Properties Anchor Side of parent container at which to anchor control—values can be

combined, such as Top, Left. Dock Side of parent container to dock control—values cannot be combined. DockPadding (for containers)

Sets the dock spacing for controls inside the container. Default is zero, so controls appear flush against the side of the container.

Location Location of the upper-left corner of the control, relative to it’s container.

Size Size of the control. Takes a Size structure, which has properties Height and Width.

MinimumSize, MaximumSize (for Windows Forms)

The minimum and maximum size of the form.

Class Control layout properties.

Page 23: C# cont’d

23

Labels, TextBoxes and Buttons• Labels

– Provide text instruction• Read only text

– Defined with class Label• Derived from class Control

• Textbox– Class TextBox– Area for text input

• Password textbox• Button

– Control to trigger a specific action• Checkboxes or radio buttons

– Derived from ButtonBase

Page 24: C# cont’d

24

Labels TextBoxes and Buttons

Label Properties

Description / Delegate and Event Arguments

Common Properties

Font The font used by the text on the Label. Text The text to appear on the Label. TextAlign The alignment of the Label’s text on the control. One of three horizontal positions

(left, center or right) and one of three vertical positions (top, middle or bottom).

Label properties.

Page 25: C# cont’d

25

Labels, TextBoxes and ButtonsTextBox Properties and Events

Description / Delegate and Event Arguments

Common Properties

AcceptsReturn If true, pressing Enter creates a new line if textbox spans multiple lines. If false, pressing Enter clicks the default button of the form.

Multiline If true, textbox can span multiple lines. Default is false. PasswordChar Single character to display instead of typed text, making the

TextBox a password box. If no character is specified, Textbox displays the typed text.

ReadOnly If true, TextBox has a gray background and its text cannot be edited. Default is false.

ScrollBars For multiline textboxes, indicates which scrollbars appear (none, horizontal, vertical or both).

Text The text to be displayed in the text box. Common Events (Delegate EventHandler, event arguments EventArgs) TextChanged Raised when text changes in TextBox (the user added or deleted

characters). Default event when this control is double clicked in the designer.

TextBox properties and events.

Page 26: C# cont’d

26

Labels TextBoxes and Buttons

Button properties and events

Description / Delegate and Event Arguments

Common Properties Text Text displayed on the Button face. Common Events (Delegate EventHandler, event arguments EventArgs) Click Raised when user clicks the control. Default event when this control is

double clicked in the designer.

Button properties and events.

Page 27: C# cont’d

27

Text area (a Textbox with properties Multiline and AcceptsReturn set to true.)

Page 28: C# cont’d

28

An example GUI

• Textbox in which can type password

• Password displayed upon clicking button

Page 29: C# cont’d

29

The code …• using System;• using System.Drawing;• using System.Collections;• using System.ComponentModel;• using System.Windows.Forms;• using System.Data;

• namespace WindowsApplication3_2• {• /// <summary>• /// Summary description for Form1.• /// </summary>• public class Form1 : System.Windows.Forms.Form• {

• private System.Windows.Forms.TextBox textBox1;• private System.Windows.Forms.Label label1;• private System.Windows.Forms.Button button1;• /// <summary>• /// Required designer variable.• /// </summary>• private System.ComponentModel.Container components = null;

• public Form1()• {• //• // Required for Windows Form Designer support• //• InitializeComponent();

• //• // TODO: Add any constructor code after InitializeComponent call• //• }

Declared components

Page 30: C# cont’d

30

…• private void InitializeComponent()• {• this.textBox1 = new

System.Windows.Forms.TextBox();• this.label1 = new System.Windows.Forms.Label();• this.button1 = new

System.Windows.Forms.Button();• this.SuspendLayout();• // • // textBox1• // • this.textBox1.Location = new

System.Drawing.Point(32, 48);• this.textBox1.Name = "textBox1";• this.textBox1.PasswordChar = '*';• this.textBox1.Size = new

System.Drawing.Size(176, 20);• this.textBox1.TabIndex = 0;• this.textBox1.Text = "";• this.textBox1.TextChanged += new

System.EventHandler(this.textBox1_TextChanged);• // • // label1• // • this.label1.BackColor =

System.Drawing.SystemColors.Info;• this.label1.Location = new

System.Drawing.Point(32, 96);• this.label1.Name = "label1";• this.label1.Size = new System.Drawing.Size(176,

23);• this.label1.TabIndex = 1;

Construct components

Set property so that chars appear as ‘*’ when typed in

textBox1.

Event handler for textBox, but never used. (added automatically by vs .net)

Page 31: C# cont’d

31

…• // • // button1• // • this.button1.Location = new System.Drawing.Point(48,

144);• this.button1.Name = "button1";• this.button1.Size = new System.Drawing.Size(152, 23);• this.button1.TabIndex = 2;• this.button1.Text = "Show password";• this.button1.Click += new

System.EventHandler(this.button1_Click);• // • // Form1• // • this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);• this.ClientSize = new System.Drawing.Size(292, 273);• this.Controls.AddRange(new System.Windows.Forms.Control[]

{•

this.button1,•

this.label1,•

this.textBox1});• this.Name = "Form1";• this.Text = "Form1";• this.ResumeLayout(false);

• }• #endregion

Event handler for button. (generated by vs.net, and implemented by programmer).

Page 32: C# cont’d

32

…• /// <summary>• /// Clean up any resources being used.• /// </summary>• protected override void Dispose( bool disposing )• {• if( disposing )• {• if (components != null) • {• components.Dispose();• }• }• base.Dispose( disposing );• }

• private void button1_Click(object sender, System.EventArgs e)• {• label1.Text = textBox1.Text;• }

• private void textBox1_TextChanged(object sender, System.EventArgs e)

• {•• }

• #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>• /// <summary>

This line added by programmer.

Page 33: C# cont’d

33

• /// The main entry point for the application.

• /// </summary>

• [STAThread]

• static void Main()

• {

• Application.Run(new Form1());

• }

• }

• }

Page 34: C# cont’d

34

GroupBoxes and Panels

• Arrange components on a GUI– GroupBoxes can display a caption

• Text property determines its caption– Panels can have scrollbar

• View additional controls inside the Panel

Page 35: C# cont’d

35

GroupBoxes and Panels

GroupBox Properties Description Common Properties

Controls The controls that the GroupBox contains. Text Text displayed on the top portion of the GroupBox (its caption).

GroupBox properties.

Page 36: C# cont’d

36

GroupBoxes and Panels

Panel Properties Description Common Properties

AutoScroll Whether scrollbars appear when the Panel is too small to hold its controls. Default is false.

BorderStyle Border of the Panel (default None; other options are Fixed3D and FixedSingle).

Controls The controls that the Panel contains.

Panel properties.

Page 37: C# cont’d

37

Example After click of button 1Before

any click.

Panel containing 2 buttons

Page 38: C# cont’d

38

The code• using System;• using System.Drawing;• using System.Collections;• using System.ComponentModel;• using System.Windows.Forms;• using System.Data;• namespace WindowsApplication3_3• {• /// <summary>• /// Summary description for Form1.• /// </summary>• public class Form1 : System.Windows.Forms.Form• {• private System.Windows.Forms.GroupBox groupBox1;• private System.Windows.Forms.Button button1;• private System.Windows.Forms.Button button2;• private System.Windows.Forms.Panel panel1;• private System.Windows.Forms.Button button3;• private System.Windows.Forms.Button button4;• private System.Windows.Forms.Label label1;• /// <summary>• private System.ComponentModel.Container components = null;

• public Form1()• {• //• // Required for Windows Form Designer support• //• InitializeComponent();

• //• // TODO: Add any constructor code after InitializeComponent call• //• }

Declared components

Page 39: C# cont’d

39

…• //….. • private void InitializeComponent()• {• this.groupBox1 = new System.Windows.Forms.GroupBox();• this.button1 = new System.Windows.Forms.Button();• this.button2 = new System.Windows.Forms.Button();• this.panel1 = new System.Windows.Forms.Panel();• this.button3 = new System.Windows.Forms.Button();• this.button4 = new System.Windows.Forms.Button();• this.label1 = new System.Windows.Forms.Label();• this.groupBox1.SuspendLayout();• this.panel1.SuspendLayout();• this.SuspendLayout();• // • // groupBox1• // • this.groupBox1.BackColor = System.Drawing.SystemColors.Desktop;• this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {•

this.button2,•

this.button1});• this.groupBox1.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Bold,

System.Drawing.GraphicsUnit.Point, ((System.Byte)(161)));• this.groupBox1.Location = new System.Drawing.Point(40, 32);• this.groupBox1.Name = "groupBox1";• this.groupBox1.Size = new System.Drawing.Size(240, 96);• this.groupBox1.TabIndex = 0;• this.groupBox1.TabStop = false;• this.groupBox1.Text = "GroupBox with 2 buttons";• // • // button1• // • this.button1.BackColor = System.Drawing.Color.Red;• this.button1.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Bold,

System.Drawing.GraphicsUnit.Point, ((System.Byte)(161)));• this.button1.Location = new System.Drawing.Point(16, 40);• this.button1.Name = "button1";• this.button1.Size = new System.Drawing.Size(80, 32);• this.button1.TabIndex = 0;• this.button1.Text = "button1";

• this.button1.Click += new System.EventHandler(this.button1_Click);

Configure components.

Construct components

Hookup to events

Page 40: C# cont’d

40

…• // • // button2• // • this.button2.BackColor = System.Drawing.Color.Red;• this.button2.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F,

System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(161)));• this.button2.Location = new System.Drawing.Point(144, 32);• this.button2.Name = "button2";• this.button2.Size = new System.Drawing.Size(80, 32);• this.button2.TabIndex = 1;• this.button2.Text = "exit";

• this.button2.Click += new System.EventHandler(this.button2_Click);• //

• // panel1• // • this.panel1.BackColor = System.Drawing.SystemColors.Desktop;• this.panel1.Controls.AddRange(new System.Windows.Forms.Control[] {•

this.button4,•

this.button3});• this.panel1.Location = new System.Drawing.Point(40, 184);• this.panel1.Name = "panel1";• this.panel1.Size = new System.Drawing.Size(224, 96);• this.panel1.TabIndex = 1;

• this.panel1.Paint += new System.Windows.Forms.PaintEventHandler(this.panel1_Paint);

• // • // button3• // • this.button3.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(0)),

((System.Byte)(0)), ((System.Byte)(192)));• this.button3.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F,

System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(161)));• this.button3.ForeColor = System.Drawing.SystemColors.ControlLightLight;• this.button3.Location = new System.Drawing.Point(16, 48);• this.button3.Name = "button3";• this.button3.Size = new System.Drawing.Size(80, 32);• this.button3.TabIndex = 0;• this.button3.Text = "button3";

• this.button3.Click += new System.EventHandler(this.button3_Click);•

Page 41: C# cont’d

41

….• // • // button4• // • this.button4.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(0)),

((System.Byte)(0)), ((System.Byte)(192)));• this.button4.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F,

System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(161)));• this.button4.ForeColor = System.Drawing.SystemColors.ControlLightLight;• this.button4.Location = new System.Drawing.Point(136, 48);• this.button4.Name = "button4";• this.button4.Size = new System.Drawing.Size(80, 32);• this.button4.TabIndex = 1;• this.button4.Text = "button4";

• this.button4.Click += new System.EventHandler(this.button4_Click);• //

• // label1• // • this.label1.BackColor = System.Drawing.Color.Yellow;• this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F,

(System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic), System.Drawing.GraphicsUnit.Point, ((System.Byte)(161)));• this.label1.Location = new System.Drawing.Point(64, 144);• this.label1.Name = "label1";• this.label1.Size = new System.Drawing.Size(168, 23);• this.label1.TabIndex = 2;• this.label1.Text = "no button pressed";• this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;• // • // Form1• // • this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);• this.ClientSize = new System.Drawing.Size(296, 317);• this.Controls.AddRange(new System.Windows.Forms.Control[] {•

this.label1,•

this.panel1,•

this.groupBox1});• this.Name = "Form1";• this.Text = "Form1";• this.groupBox1.ResumeLayout(false);• this.panel1.ResumeLayout(false);• this.ResumeLayout(false);

• }• #endregion

Page 42: C# cont’d

42

….• /// <summary>• /// The main entry point for the application.• /// </summary>• [STAThread]• static void Main() • {• Application.Run(new Form1());• }

• private void panel1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)

• {•• }

• private void button1_Click(object sender, System.EventArgs e)• {• label1.Text = "Button 1 pressed";• }

• private void button2_Click(object sender, System.EventArgs e)• {• this.Close();• }

• private void button3_Click(object sender, System.EventArgs e)• {• label1.Text = "Button 3 pressed";• }

• private void button4_Click(object sender, System.EventArgs e)• {• label1.Text = "Button 4 pressed";• }• }• }

Event handling code for the four

buttons.

Event handling for rendering the panel (automatically generated).

Page 43: C# cont’d

43

Checkboxes and RadioButtons

• State buttons– On/off or true/false state– Derived from class ButtonBase

• CheckBox– No restriction on usage

• RadioButton– Grouped together – Only one can be true

Page 44: C# cont’d

44

CheckBoxes and RadioButtons

CheckBox events and properties

Description / Delegate and Event Arguments

Common Properties

Checked Whether or not the CheckBox has been checked. CheckState Whether the Checkbox is checked (contains a black checkmark) or

unchecked (blank). An enumeration with values Checked, Unchecked or Indeterminate.

Text Text displayed to the right of the CheckBox (called the label). Common Events (Delegate EventHandler, event arguments EventArgs) CheckedChanged Raised every time the Checkbox is either checked or unchecked.

Default event when this control is double clicked in the designer. CheckStateChanged Raised when the CheckState property changes.

CheckBox properties and events.

Page 45: C# cont’d

45

CheckBoxes and RadioButtons

RadioButton properties and events

Description / Delegate and Event Arguments

Common Properties

Checked Whether the RadioButton is checked. Text Text displayed to the right of the RadioButton (called the label). Common Events (Delegate EventHandler, event arguments EventArgs) Click Raised when user clicks the control. CheckedChanged Raised every time the RadioButton is checked or unchecked.

Default event when this control is double clicked in the designer.

RadioButton properties and events.

Page 46: C# cont’d

46

Example

Page 47: C# cont’d

47

The code • using System;• using System.Drawing;• using System.Collections;• using System.ComponentModel;• using System.Windows.Forms;• using System.Data;

• namespace WindowsApplication3_4• {• /// <summary>• /// Summary description for Form1.• /// </summary>• public class Form1 : System.Windows.Forms.Form• {• private System.Windows.Forms.CheckBox checkBox1;• private System.Windows.Forms.CheckBox checkBox2;• private System.Windows.Forms.RadioButton radioButton1;• private System.Windows.Forms.RadioButton radioButton2;• private System.Windows.Forms.RadioButton radioButton3;• private System.Windows.Forms.CheckBox checkBox3;• private System.Windows.Forms.CheckBox checkBox4;• private System.Windows.Forms.CheckBox checkBox5;• private System.Windows.Forms.CheckBox checkBox6;• private System.Windows.Forms.CheckBox checkBox7;• private System.Windows.Forms.Button button3;• private System.Windows.Forms.Button button1;• /// <summary>• /// Required designer variable.• /// </summary>• private System.ComponentModel.Container components = null;

• public Form1()• {• //• // Required for Windows Form Designer support• //

I i i li C ()

Page 48: C# cont’d

48

…• public Form1()• {• //• // Required for Windows Form Designer support• //• InitializeComponent();

• //• // TODO: Add any constructor code after InitializeComponent call• //• }• //…• #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.checkBox1 = new System.Windows.Forms.CheckBox();• this.checkBox2 = new System.Windows.Forms.CheckBox();• this.radioButton1 = new System.Windows.Forms.RadioButton();• this.radioButton2 = new System.Windows.Forms.RadioButton();• this.radioButton3 = new System.Windows.Forms.RadioButton();• this.checkBox3 = new System.Windows.Forms.CheckBox();• this.checkBox4 = new System.Windows.Forms.CheckBox();• this.checkBox5 = new System.Windows.Forms.CheckBox();• this.checkBox6 = new System.Windows.Forms.CheckBox();• this.checkBox7 = new System.Windows.Forms.CheckBox();• this.button3 = new System.Windows.Forms.Button();• this.button1 = new System.Windows.Forms.Button();• this.SuspendLayout();

Page 49: C# cont’d

49

…• //• // configure components …. )lots of code)• //• // button3• // • this.button3.Location = new System.Drawing.Point(32, 208);• this.button3.Name = "button3";• this.button3.TabIndex = 12;• this.button3.Text = "exit";• this.button3.Click += new System.EventHandler(this.button3_Click);• // • // button1• // • this.button1.Location = new System.Drawing.Point(24, 120);• this.button1.Name = "button1";• this.button1.Size = new System.Drawing.Size(88, 48);• this.button1.TabIndex = 13;• this.button1.Text = "Show Order";• this.button1.Click += new System.EventHandler(this.button1_Click);•• static void Main() • {• Application.Run(new Form1());• }• //…

• private void button3_Click(object sender, System.EventArgs e)• {• this.Close();• }

Page 50: C# cont’d

50

…• // • //………..• private void button1_Click(object sender, System.EventArgs e)• {• string theOrder = "";• if ( radioButton1.Checked) theOrder +="small " + '\n' + "--------"+ '\n';• if ( radioButton2.Checked) theOrder += "medium " + '\n' + "--------"+ '\n';• if ( radioButton3.Checked) theOrder += "large " + '\n' + "--------"+ '\n';• if ( checkBox1.Checked) theOrder += "sausage " + '\n';• if ( checkBox2.Checked) theOrder += "pepperoni " + '\n';• if ( checkBox3.Checked) theOrder += "greenPepper " + '\n';• if ( checkBox4.Checked) theOrder += "redOnion " + '\n'; • if ( checkBox5.Checked) theOrder += "anchovies " + '\n';• if ( checkBox6.Checked) theOrder += "cheese " + '\n';• if ( checkBox7.Checked) theOrder += "mashrooms " + '\n';

• MessageBox.Show(theOrder);• }• }• }

Display MessageBox with the assembled Order.