C# Tutorial MSM_Murach chapter-02-slides

of 31 /31
Murach’s C# 2010, C2 © 2010, Mike Murach & Associates, Inc. Slide 1 C hapter2 How to design a W indow s Form s application

Transcript of C# Tutorial MSM_Murach chapter-02-slides

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

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

Chapter 2

How to design a Windows Forms

application

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

Slide 2

Agenda

• Forms Basics– Forms– Controls– Properties

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

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

The Options dialog box for setting the project options

Tools…Options

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

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

The New Project dialog box

File…New…Project

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

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

The Invoice Total form

Three types of controls A label displays text on a form. A text box lets the user enter text on a form. A button initiates form processing when clicked.

Text Box

Buttons

Labels

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

Slide 6

How the Invoice Total form works The user enters the subtotal into a text box, and the discount percent, discount

amount, and total are displayed in label controls. The discount percent is a function of the amount of the subtotal.

After entering a subtotal, the user can click the Calculate button to calculate the

discount percent, discount amount, and the total. Alternatively, the user can press the Enter key to perform the calculation.

To close the form and end the application, the user can click the Close button in the

upper right corner of the form or on the Exit button. Alternatively, the user can press the Esc key to exit from the form.

User can press Alt+C to access the Calculate button. User can press Alt+X to access the Exit button.

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

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

Three ways to add a control to a form Select the control in the Toolbox. Then, click in the form where

you want to place the control. Or, drag the pointer on the form to place the control and size it at the same time.

Double-click the control in the Toolbox. Then, the control is placed in the upper left corner of the form.

Drag the control from the Toolbox and drop it on the form. Then, the control is placed wherever you drop it.

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

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

A form after some controls have been added to it

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

Slide 9

How to select and work with controls (basics) To select a control on a form, click on it. Careful…Don’t double

click or you’ll end up in the code editor. To move a control, drag it. To size a selected control, drag one of its handles. To select more than one control, hold down the Shift or Ctrl key

as you click on each control. Or, click on a blank spot in the form and then dragging around the controls.

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

Slide 10

How to select and work with controls (Fancy Stuff) To align, size, or space a group of selected controls, click on a control

to make it the primary control. Then, use the commands in the Format menu or the buttons on the Layout toolbar to align, size, or space the controls relative to the primary control.

You can also size all of the controls in a group by sizing the primary

control in the group. And you can drag any of the selected controls to move all the controls.

To change the size of a form to accommodate the controls, click on

the form and then drag it by one of its handles.

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

Slide 11

How to set properties (continued) When you click on a property in the Properties window, a brief

explanation of the property appears in a pane at the bottom of the window. For more information, press F1 to display the help information for the property.

You can use the first two buttons at the top of the Properties window to sort the properties by category or alphabetically.

You can use the plus and minus signs displayed to the left of some of the properties and categories in the Properties window to expand and collapse the list of properties.

Note If a description isn’t displayed when you click on a property in the

Properties window, right-click on the window and select Description from the shortcut menu.

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

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

A form after the properties have been set

Tip: Set AutoSize to “False” & BorderStyle to “Fixed3D” before you clear out the Text property if you are using labels instead of textboxes.

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

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

The Name property Sets the name you use to identify a control in your C# code. Can be changed to provide a more descriptive and memorable

name for forms and controls that you will refer to when you write your code (such as text boxes and buttons).

Doesn’t need to be changed for controls that you won’t refer to when you write your code (such as most labels).

Can use a three-letter prefix to indicate whether the name refers to a form (frm), button (btn), label (lbl), or text box (txt).

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

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

The Text property Sets the text that’s displayed on the form or control. For a form, the Text value is displayed in the title bar. For

controls, the Text value is displayed directly on the control. For a text box, the Text value changes when the user types text

into the control, and you can write code that uses the Text property to get the text that was entered by the user.

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

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

A form after the properties have been set

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

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

Other properties for forms Property Description AcceptButton Identifies the button that will be activated when the

user presses the Enter key. CancelButton Identifies the button that will be activated when the

user presses the Esc key. StartPosition Sets the position at which the form is displayed. To

center the form, set this property to CenterScreen.

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

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

Other properties for controls Property Description Enabled Determines whether the control will be enabled

or disabled. ReadOnly Determines whether the text in some controls

like text boxes can be edited. TabIndex Indicates the control’s position in the tab order,

which determines the order in which the controls will receive the focus when the user presses the Tab key.

TabStop Determines whether the control will accept the focus when the user presses the Tab key to move from one control to another. Some controls, like labels, don’t have the TabStop property.

TextAlign Sets the alignment for the text displayed on a control.

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

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

How to adjust the tab order Tab order refers to the sequence in which the controls receive the

focus when the user presses the Tab key. You should adjust the tab order so the Tab key moves the focus from one control to the next in a logical sequence.

Each control has a TabIndex property that indicates the control’s position in the tab order. You can change this property to change a control’s tab order position.

If you don’t want a control to receive the focus when the user presses the Tab key, change that control’s TabStop property to False.

Label controls don’t have a TabStop property so they can’t receive the focus.

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

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

How to set access keys Access keys are shortcut keys that the user can use in combination

with the Alt key to quickly move to individual controls on the form.

You use the Text property to set the access key for a control by placing an ampersand immediately before the letter you want to use for the access key.

Since the access keys aren’t case sensitive, &N and &n set the same access key.

When you set access keys, make sure to use a unique letter for each control.

You can’t set the access key for a text box. However, if you set an access key for a label that immediately precedes the text box in the tab order, the access key will take the user to the text box.

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

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

How to set the Enter and Esc keys The AcceptButton property of the form sets the button that will be

activated if the user presses the Enter key. The CancelButton property of the form sets the button that will be

activated if the user presses the Esc key. This property should usually be set to the Exit button.

You set the AcceptButton or CancelButton values by choosing the button from a drop-down list that shows all of the buttons on the form. So be sure to create and name the buttons you want to use before you attempt to set these values.

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

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

The property settings for the form Default name Property Setting Form1 Text Invoice Total

AcceptButton btnCalculate CancelButton btnExit StartPosition CenterScreen

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

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

The property settings for the controls Default name Property Setting label1 Text &Subtotal:

TextAlign MiddleLeft TabIndex 0

label2 Text Discount percent: TextAlign MiddleLeft

label3 Text Discount amount: TextAlign MiddleLeft

label4 Text Total: TextAlign MiddleLeft

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

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

The property settings for the controls (continued) Default name Property Setting textBox1 Name txtSubtotal

TabIndex 1 textBox2 Name txtDiscountPercent

ReadOnly True TabStop False

textBox3 Name txtDiscountAmount ReadOnly True TabStop False

textBox4 Name txtTotal ReadOnly True TabStop False

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

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

The property settings for the controls (continued) Default name Property Setting button1 Name btnCalculate

Text &Calculate TabIndex 2

button2 Name btnExit Text E&xit TabIndex 3

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

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

The Solution Explorer as a form file is being renamed

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

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

How to rename a file, project, or solution Right-click on it in the Solution Explorer window and select the

Rename command from the shortcut menu. Or, select it in the Solution Explorer and press F2. Then, you can enter the new name.

Be sure not to change or omit the file extension when you rename a file. Remember too that using a three-letter prefix to indicate the contents of the file (like frm for a form file) makes it easier to tell what each file represents.

When you change the name of a form file, Visual Studio will also change the Name property for the form and update any references within the existing code for the form.

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

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

How to save a file, project, or solution You can use the Save All button in the Standard toolbar or the

Save All command in the File menu to save all files and projects in the solution.

You can use the Save button in the Standard toolbar or the Save command in the File menu to save a file, project, or solution. The files that are saved depend on what’s selected in the Solution Explorer window. If a single file is selected, just that file is saved. If a project is selected, the entire project and its solution are saved. And if a solution is selected, the entire solution and all its projects are saved.

If you try to close a solution that contains modified files, a dialog box is displayed that asks you if you want to save those files.

Page 28: C# Tutorial MSM_Murach chapter-02-slides

Slide 28

In-Class Activity

• Create the following form– Note:

• Form title• Justification of fields• The “Enter” key and the Alt+C keys do the same

thing as pressing the “Calculate Letter Grade” button

• The “Esc” key and Alt+X keys do the same thing as pressing the “Exit” button

• Don’t forget the tab order.– We’ll work on this program again later

Text box

Label

Page 29: C# Tutorial MSM_Murach chapter-02-slides

Slide 29

In-Class Activity

• Create the following form– Note:

• Form title• Justification of fields• The “Enter” key and the Alt+C keys do the same

thing as pressing the “Calculate Grand Total” button• The “Esc” key and Alt+X keys do the same thing as

pressing the “Exit” button• Don’t forget the tab order

– We’ll work on this program again later

Text boxes

Read Only Text Boxes

Page 30: C# Tutorial MSM_Murach chapter-02-slides

Slide 30

In-Class Activity

• Create the following form– Note:

• Form title• Justification of fields• The “Enter” key and the Alt+P keys do the same

thing as pressing the “Project Student Population” button

• The “Esc” key and Alt+X keys do the same thing as pressing the “Exit” button

• Don’t forget the tab order– We’ll work on this program again later

Text boxes

Read Only Text Box

Page 31: C# Tutorial MSM_Murach chapter-02-slides

Slide 31

In-Class Activity

• Create the following form– Note:

• Form title• Justification of fields• The “Enter” key and the Alt+C keys do the same

thing as pressing the “Convert to Numeric Only” button

• The “Esc” key and Alt+X keys do the same thing as pressing the “Exit” button

• Don’t forget the tab order– We’ll work on this program again later

Read Only Text Box

Text box