C# Tutorial MSM_Murach chapter-24-slides

27
Murach’s C# 2010, C24 © 2010, Mike Murach & Associates, Inc. Slide 1 C hapter24 How to enhance the userinterface

Transcript of C# Tutorial MSM_Murach chapter-24-slides

Page 1: C# Tutorial MSM_Murach chapter-24-slides

Murach’s C# 2010, C24 © 2010, Mike Murach & Associates, Inc. Slide 1

Chapter 24

How to enhance the user interface

Page 2: C# Tutorial MSM_Murach chapter-24-slides

Murach’s C# 2010, C24 © 2010, Mike Murach & Associates, Inc. Slide 2

Objectives Applied 1. Use the Tab control to organize an application. 2. Use a multiple-document interface for an application. 3. Add menus, toolbars, and help information to an application.

Knowledge 1. Distinguish between a single-document and a multiple-

document interface.

Page 3: C# Tutorial MSM_Murach chapter-24-slides

Murach’s C# 2010, C24 © 2010, Mike Murach & Associates, Inc. Slide 3

Single-document interface (SDI)

Page 4: C# Tutorial MSM_Murach chapter-24-slides

Murach’s C# 2010, C24 © 2010, Mike Murach & Associates, Inc. Slide 4

Multiple-document interface (MDI)

Page 5: C# Tutorial MSM_Murach chapter-24-slides

Murach’s C# 2010, C24 © 2010, Mike Murach & Associates, Inc. Slide 5

A startup form for the Financial Calculations application

Page 6: C# Tutorial MSM_Murach chapter-24-slides

Murach’s C# 2010, C24 © 2010, Mike Murach & Associates, Inc. Slide 6

Code that displays a new instance of a form private void btnFutureValue_Click(object sender, System.EventArgs e) { Form newForm = new frmFutureValue(); newForm.Show(); }

Code that exits the application and closes all forms

private void btnExit_Click(object sender, System.EventArgs e) { Application.Exit(); }

Page 7: C# Tutorial MSM_Murach chapter-24-slides

Murach’s C# 2010, C24 © 2010, Mike Murach & Associates, Inc. Slide 7

Code that closes the current instance of a form private void btnClose_Click(object sender, System.EventArgs e) { this.Close(); }

Page 8: C# Tutorial MSM_Murach chapter-24-slides

Murach’s C# 2010, C24 © 2010, Mike Murach & Associates, Inc. Slide 8

A form that uses a Tab control with two tabs

Page 9: C# Tutorial MSM_Murach chapter-24-slides

Murach’s C# 2010, C24 © 2010, Mike Murach & Associates, Inc. Slide 9

Code that uses the SelectedIndex property of the Tab control

private void btnCalculate_Click(object sender, System.EventArgs e) { if (tabCalculations.SelectedIndex == 0) DisplayFutureValue(); else if (tabCalculations.SelectedIndex == 1) DisplayDepreciation(); }

Page 10: C# Tutorial MSM_Murach chapter-24-slides

Murach’s C# 2010, C24 © 2010, Mike Murach & Associates, Inc. Slide 10

Code that uses the SelectedIndexChanged event of the Tab control

private void tabCalculations_SelectedIndexChanged( object sender, System.EventArgs e) { if (tabCalculations.SelectedIndex == 0) txtMonthlyInvestment.Focus(); else if (tabCalculations.SelectedIndex == 1) txtInitialCost.Focus(); }

Page 11: C# Tutorial MSM_Murach chapter-24-slides

Murach’s C# 2010, C24 © 2010, Mike Murach & Associates, Inc. Slide 11

The start of the menu for the Future Value app

Page 12: C# Tutorial MSM_Murach chapter-24-slides

Murach’s C# 2010, C24 © 2010, Mike Murach & Associates, Inc. Slide 12

The complete Action menu

Page 13: C# Tutorial MSM_Murach chapter-24-slides

Murach’s C# 2010, C24 © 2010, Mike Murach & Associates, Inc. Slide 13

Common menu item properties Text Name Checked Enabled Visible ShortcutKeys ShowShortcutKeys

Page 14: C# Tutorial MSM_Murach chapter-24-slides

Murach’s C# 2010, C24 © 2010, Mike Murach & Associates, Inc. Slide 14

Code for a menu item that clears four controls private void mnuClear_Click(object sender, System.EventArgs e) { txtMonthlyInvestment.Text = ""; txtInterestRate.Text = ""; txtYears.Text = ""; txtFutureValue.Text = ""; }

Page 15: C# Tutorial MSM_Murach chapter-24-slides

Murach’s C# 2010, C24 © 2010, Mike Murach & Associates, Inc. Slide 15

Code for a menu item that calls another event handler

private void mnuCalculate_Click(object sender, System.EventArgs e) { btnCalculate_Click(sender, e); }

Another way to call another event handler private void mnuCalculate_Click(object sender, System.EventArgs e) { btnCalculate.PerformClick(); }

Page 16: C# Tutorial MSM_Murach chapter-24-slides

Murach’s C# 2010, C24 © 2010, Mike Murach & Associates, Inc. Slide 16

A project with one parent and two child forms

Page 17: C# Tutorial MSM_Murach chapter-24-slides

Murach’s C# 2010, C24 © 2010, Mike Murach & Associates, Inc. Slide 17

Properties and methods for a parent form Property/Method Description IsMdiContainer At design-time, set this property to true. ActiveMdiChild At runtime, you can use this property to

retrieve a reference to the active child form. LayoutMdi(mdiLayout) At runtime, you can use this method to

arrange all child forms by specifying a member of the MdiLayout enumeration: Cascade, TileVertical, TileHorizontal, and so on.

Typical property setting for a child form Property Description MdiParent At runtime, you can set this property to specify the

parent form for a child form.

Page 18: C# Tutorial MSM_Murach chapter-24-slides

Murach’s C# 2010, C24 © 2010, Mike Murach & Associates, Inc. Slide 18

An MDI application with three child forms arranged vertically

Page 19: C# Tutorial MSM_Murach chapter-24-slides

Murach’s C# 2010, C24 © 2010, Mike Murach & Associates, Inc. Slide 19

Code that creates and displays a new instance of a child form

private void mnuNewFutureValue_Click(object sender, System.EventArgs e) { Form newForm = new frmFutureValue(); newForm.MdiParent = this; newForm.Show(); }

Code that refers to the active child form private void mnuClose_Click(object sender, System.EventArgs e) { Form activeForm = this.ActiveMdiChild; if (activeForm != null) activeForm.Close(); }

Page 20: C# Tutorial MSM_Murach chapter-24-slides

Murach’s C# 2010, C24 © 2010, Mike Murach & Associates, Inc. Slide 20

Code that arranges the child forms vertically private void mnuTileVertical_Click(object sender, System.EventArgs e) { this.LayoutMdi(MdiLayout.TileVertical); }

Code that exits the application and closes all child forms

private void mnuExit_Click(object sender, System.EventArgs e) { Application.Exit(); }

Page 21: C# Tutorial MSM_Murach chapter-24-slides

Murach’s C# 2010, C24 © 2010, Mike Murach & Associates, Inc. Slide 21

A ToolStrip with the standard items

A ToolStrip with two custom buttons

Page 22: C# Tutorial MSM_Murach chapter-24-slides

Murach’s C# 2010, C24 © 2010, Mike Murach & Associates, Inc. Slide 22

The Items Collection Editor for a ToolStrip

Page 23: C# Tutorial MSM_Murach chapter-24-slides

Murach’s C# 2010, C24 © 2010, Mike Murach & Associates, Inc. Slide 23

Code for the Click event of a button on a ToolStrip private void btnFutureValue_Click(object sender, EventArgs e) { mnuNewFutureValue.PerformClick(); }

Page 24: C# Tutorial MSM_Murach chapter-24-slides

Murach’s C# 2010, C24 © 2010, Mike Murach & Associates, Inc. Slide 24

A View menu that shows or hides the toolbar

Code that shows or hides a toolbar depending on a menu selection

private void mnuToolbar_Click(object sender, System.EventArgs e) { if (mnuToolbar.Checked == true) { mnuToolbar.Checked = false; tlbMain.Visible = false; } else if (mnuToolbar.Checked == false) { mnuToolbar.Checked = true; tlbMain.Visible = true; } }

Page 25: C# Tutorial MSM_Murach chapter-24-slides

Murach’s C# 2010, C24 © 2010, Mike Murach & Associates, Inc. Slide 25

A tool tip Context-sensitive help

Page 26: C# Tutorial MSM_Murach chapter-24-slides

Murach’s C# 2010, C24 © 2010, Mike Murach & Associates, Inc. Slide 26

How to work with a tool tip A tool tip is a brief description of a control that’s displayed

automatically when you place the mouse pointer over that control. To create tool tips for a form, add a ToolTip control to the form.

A control named toolTip1 will appear in the Component Designer tray at the bottom of the window.

The ToolTip control makes a property named “ToolTip on toolTip1” available for each control on the form and for the form itself. You can enter the text for the tool tip in this property.

Page 27: C# Tutorial MSM_Murach chapter-24-slides

Murach’s C# 2010, C24 © 2010, Mike Murach & Associates, Inc. Slide 27

How to work with context-sensitive help To provide context-sensitive help for a form or control, add a

HelpProvider control to the form. A control named helpProvider1 will appear in the Component Designer tray at the bottom of the window. This control makes several additional properties available for the form and each control it contains.

To display a text string when the user presses the F1 key for the control that has the focus, enter the text for the “HelpString on helpProvider1” property of the control.

You can also enter help text for the HelpString property of the form. Then, that text is displayed at the location of the mouse pointer if a help string isn’t specified for the control that has the focus.

When you enter text for the “HelpString on helpProvider1” property, the “ShowHelp on helpProvider1” property automatically changes from false to true.