week 2: windows programming

58
C#4.0 Week 2: WINDOWS PROGRAMMING Chapter 15 in “Beginning Visual C# 2010” ebook Chapter 4 in “”MCTS_Self-Paced_Training_Kit” ebook

Upload: julie

Post on 25-Feb-2016

50 views

Category:

Documents


1 download

DESCRIPTION

Week 2: WINDOWS PROGRAMMING. Chapter 15 in “Beginning Visual C# 2010” ebook Chapter 4 in “”MCTS_Self-Paced_Training_Kit” ebook. Week 2: WINDOWS PROGRAMMING. Working with Windows Forms. CONTROLS. When you work with Windows Forms, you are working with the - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Week 2:  WINDOWS PROGRAMMING

C#4.0Week 2:

WINDOWS PROGRAMMING

Chapter 15 in “Beginning Visual C# 2010” ebook Chapter 4 in “”MCTS_Self-Paced_Training_Kit”

ebook

Page 2: Week 2:  WINDOWS PROGRAMMING

C#4.0Week 2:

WINDOWS PROGRAMMING

Working with Windows Forms

Page 3: Week 2:  WINDOWS PROGRAMMING

C#4.0

CONTROLS When you work with Windows Forms, you

are working with theSystem.Windows.Forms namespace

Most controls in .NET derive from the System.Windows.Forms.Control class

Many of these classes are themselves base classes for other controls, as is the case with the Label and TextBoxBase classes

Windows Programming 1 Basic Windows Programming Slide 3

Page 4: Week 2:  WINDOWS PROGRAMMING

C#4.0

CONTROLS

Windows Programming 1 Basic Windows Programming Slide 4

Page 5: Week 2:  WINDOWS PROGRAMMING

C#4.0

PropertiesCommon Control Class Properties

Windows Programming 1 Basic Windows Programming Slide 5

Page 6: Week 2:  WINDOWS PROGRAMMING

C#4.0

Properties Common Control Class Properties

Windows Programming 1 Basic Windows Programming Slide 6

Page 7: Week 2:  WINDOWS PROGRAMMING

C#4.0

Adding Controls to a Windows Form Adding Controls by Using the Windows

Forms Designer Adding Controls Programmatically

Windows Programming 1 Basic Windows Programming Slide 7

Page 8: Week 2:  WINDOWS PROGRAMMING

C#4.0

Adding Controls Programmatically 1. Create a private variable to represent each

of the controls you want to place on the form 2. In the form, place code to instantiate each

control and to customize each control, using its properties, methods, or events.

3. Add each control to the forms control collection.

Windows Programming 1 Basic Windows Programming Slide 8

Page 9: Week 2:  WINDOWS PROGRAMMING

C#4.0

Exam (page 113)

Windows Programming 1 Basic Windows Programming Slide 9

Page 10: Week 2:  WINDOWS PROGRAMMING

C#4.0

Handling Control Events Add controls to a Windows form.

Set properties on controls. Load controls dynamically. Write code to handle control events and add the code

to a control.

Windows Programming 1 Basic Windows Programming Slide 10

Page 11: Week 2:  WINDOWS PROGRAMMING

C#4.0

Handling Control Events_Ex

Windows Programming 1 Basic Windows Programming Slide 11

Page 12: Week 2:  WINDOWS PROGRAMMING

C#4.0 Windows Programming 1 Basic Windows Programming Slide 12

HANDLING CONTROL EVENTS

Page 13: Week 2:  WINDOWS PROGRAMMING

C#4.0

HANDLING CONTROL EVENTS

Windows Programming 1 Basic Windows Programming Slide 13

Page 14: Week 2:  WINDOWS PROGRAMMING

C#4.0 Windows Programming 1 Chapter 15:Basic Windows Programming Slide 14

HANDLING CONTROL EVENTS

Page 15: Week 2:  WINDOWS PROGRAMMING

C#4.0 Windows Programming 1 Chapter 15:Basic Windows Programming Slide 15

HANDLING CONTROL EVENTS

Page 16: Week 2:  WINDOWS PROGRAMMING

C#4.0

COMMON WINDOWS FORMSCONTROLS

Control 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.

Windows Programming 1 Basic Windows Programming Slide 16

Page 17: Week 2:  WINDOWS PROGRAMMING

C#4.0

Naming Rules Always use standard names for objects No spaces or punctuation marks 3 letter lowercase prefix identifies control type

Button-btnLabel-lblForm-frm

If multiple words capitalize 1st letter of each word Each object name is an identifier

Can contain letters, digits, and underscores (_) Cannot start with digits Can start with the at symbol (@)

Windows Programming 1 Basic Windows Programming Slide 17

Page 18: Week 2:  WINDOWS PROGRAMMING

C#4.0

Recommended Naming

Object Class Prefix ExampleForm frm frmDataEntryButton btn btnExitTextBox txt txtPaymentAmountLabel lbl lblTotalRadio Button rad radBoldCheckBox chk chkPrintSummaryPictureBox pic picLandscapeComboBox cbo cboBookListListBox lst lstIndegredientsGroupBox grb grbColor

Windows Programming 1 Basic Windows Programming Slide 18

Page 19: Week 2:  WINDOWS PROGRAMMING

C#4.0

Windows Forms Windows Forms is the basic building block of

the UI It provides a container that hosts controls and

menus and enables you to present an application in a familiar and consistent fashion

You can add and configure additional forms at design time, or you can create instances of predesigned forms in code at run time.

Windows Programming 1 Basic Windows Programming Slide 19

Page 20: Week 2:  WINDOWS PROGRAMMING

C#4.0

Windows FormsSome Properties of the Form Class

Windows Programming 1 Basic Windows Programming Slide 20

Page 21: Week 2:  WINDOWS PROGRAMMING

C#4.0

Windows FormsSome Properties of the Form Class

Windows Programming 1 Basic Windows Programming Slide 21

Page 22: Week 2:  WINDOWS PROGRAMMING

C#4.0

Windows FormsSome Properties of the Form Class

Windows Programming 1 Basic Windows Programming Slide 22

Page 23: Week 2:  WINDOWS PROGRAMMING

C#4.0

Setting the Title of the Form To change the title of a form at run time, set

the Text property of the form in code, as shown in the following code:

Windows Programming 1 Basic Windows Programming Slide 23

Page 24: Week 2:  WINDOWS PROGRAMMING

C#4.0

Setting the Border Style of the Form

Windows Programming 1 Basic Windows Programming Slide 24

Page 25: Week 2:  WINDOWS PROGRAMMING

C#4.0

Specifying the Startup Location of the Form

Windows Programming 1 Basic Windows Programming Slide 25

Page 26: Week 2:  WINDOWS PROGRAMMING

C#4.0

Keeping a Form on Top of the User Interface

TopMost = True FormBorderStyle = None; StartPosition =

CenterToScreen;

Windows Programming 1 Basic Windows Programming Slide 26

Module2- 21

Page 27: Week 2:  WINDOWS PROGRAMMING

C#4.0

Opacity and Transparency in Forms The Opacity property to create striking visual

effects in your form Values between 0 percent and 100 percent result in a partially transparent form

Windows Programming 1 Basic Windows Programming Slide 27

Page 28: Week 2:  WINDOWS PROGRAMMING

C#4.0

Setting the Startup Form 1. In Solution Explorer, double-click

Program.cs to view the code. The code window opens.

2. Locate the Main method and then locate the line that reads:

Application.Run(new Form()); where Form represents the name of the form that is currently the startup form.

3. Change Form to the name of the form you want to set as the startup form.

Windows Programming 1 Basic Windows Programming Slide 28

Page 29: Week 2:  WINDOWS PROGRAMMING

C#4.0

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

Enable property Indicate a control’s accessibility

Windows Programming 1 Basic Windows Programming Slide 29

Page 30: Week 2:  WINDOWS PROGRAMMING

C#4.0

Control Properties and Layout Visibility control

Hide control from user Anchor property

Anchoring control to specific location (corner) Unanchored control moves relative to the position Docking allows control to spread itself along and

entire side Both options refer to the parent container

Windows Programming 1 Basic Windows Programming Slide 30

Page 31: Week 2:  WINDOWS PROGRAMMING

C#4.0

Control Properties and Layout

Fig. 12.11 Anchoring demonstration.

Constant distance to left and top sides

Before resize After resize

Windows Programming 1 Basic Windows Programming Slide 31

Page 32: Week 2:  WINDOWS PROGRAMMING

C#4.0

Control Properties and Layout

Fig. 12.12 Manipulating the Anchor property of a control.

Darkened bar indicates to which wall control is anchored

Click down-arrow in Anchor property to display anchoring window

Windows Programming 1 Basic Windows Programming Slide 32

Page 33: Week 2:  WINDOWS PROGRAMMING

C#4.0

Control Properties and Layout

Fig. 12.13 Docking demonstration.

Control expands along top portion of the form

Windows Programming 1 Basic Windows Programming Slide 33

Page 34: Week 2:  WINDOWS PROGRAMMING

C#4.0

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.

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.

Fig. 12.14 Class Control layout properties.

Windows Programming 1 Basic Windows Programming Slide 34

Page 35: Week 2:  WINDOWS PROGRAMMING

C#4.0

Labels and LinkLabel controls Labels : The standard Windows label LinkLabel: A label similar to the standard one

but that presents itself as an Internet link (a hyperlink) You don’t need to add event handling code for a

standard Label Some extra code is needed to enable users clicking it to

go to the target of the LinkLabel

Windows Programming 1 Basic Windows Programming Slide 35

Page 36: Week 2:  WINDOWS PROGRAMMING

C#4.0

Common Label Control Properties

Windows Programming 1 Basic Windows Programming Slide 36

Page 37: Week 2:  WINDOWS PROGRAMMING

C#4.0

Common Label Control Properties

Windows Programming 1 Basic Windows Programming Slide 37

Page 38: Week 2:  WINDOWS PROGRAMMING

C#4.0 Windows Programming 1 Chapter 15:Basic Windows Programming Slide 38

Button Control

Page 39: Week 2:  WINDOWS PROGRAMMING

C#4.0

Lable –Textbox – Button Control

Windows Programming 1 Chapter 15:Basic Windows Programming Slide 39

Page 40: Week 2:  WINDOWS PROGRAMMING

C#4.0

LinkLable - Demo

Windows Programming 1 Chapter 15:Basic Windows Programming Slide 40

Page 41: Week 2:  WINDOWS PROGRAMMING

C#4.0 Windows Programming 1 Chapter 15:Basic Windows Programming Slide 41

LinkLable - Demo

Page 42: Week 2:  WINDOWS PROGRAMMING

C#4.0

TextBoxes and RichTextBox The .NET Framework comes with two basic

controls to take text input from users: TextBox and RichTextBox.

Both controls are derived from a base class called TextBoxBase, which itself is derived from Control.

Windows Programming 1 Basic Windows Programming Slide 42

Page 43: Week 2:  WINDOWS PROGRAMMING

C#4.0

Common TextBox Control Properties

Windows Programming 1 Basic Windows Programming Slide 43

Page 44: Week 2:  WINDOWS PROGRAMMING

C#4.0

Common TextBox Control Properties

Windows Programming 1 Basic Windows Programming Slide 44

Page 45: Week 2:  WINDOWS PROGRAMMING

C#4.0

THE RICHTEXTBOX CONTROL Like the normal TextBox, the RichTextBox

control is derived from TextBoxBase Whereas a TextBox is commonly used for the

purpose of obtaining short text strings from the user

The RichTextBox is used to display and enter formatted text (e.g., bold, underline, and italic). It does so using a standard for formatted text called Rich Text Format, or RTF.

Windows Programming 1 Basic Windows Programming Slide 45

Page 46: Week 2:  WINDOWS PROGRAMMING

C#4.0

Common RichTextBox Control Properties

Windows Programming 1 Basic Windows Programming Slide 46

Page 47: Week 2:  WINDOWS PROGRAMMING

C#4.0

Common RichTextBox Control Properties

Windows Programming 1 Basic Windows Programming Slide 47

Page 48: Week 2:  WINDOWS PROGRAMMING

C#4.0

RichTextbox - demo

Windows Programming 1 Chapter 15:Basic Windows Programming Slide 48

Page 49: Week 2:  WINDOWS PROGRAMMING

C#4.0

RichTextbox - demo

Windows Programming 1 Chapter 15:Basic Windows Programming Slide 49

Page 50: Week 2:  WINDOWS PROGRAMMING

C#4.0 Windows Programming 1 Chapter 15:Basic Windows Programming Slide 50

Page 51: Week 2:  WINDOWS PROGRAMMING

C#4.0Week 2:

WINDOWS PROGRAMMING

Controls for displaying pictures

Page 52: Week 2:  WINDOWS PROGRAMMING

C#4.0

The PictureBox Control The PictureBox control is the basic control

used for displaying images in the user interface, and it can display pictures in a variety of formats, including .bmp, .jpg, .gif, metafiles, and icons.

Windows Programming 1 Basic Windows Programming Slide 52

Page 53: Week 2:  WINDOWS PROGRAMMING

C#4.0

The PictureBox Control

Windows Programming 1 Basic Windows Programming Slide 53

Page 54: Week 2:  WINDOWS PROGRAMMING

C#4.0

The PictureBox Control At run time, you can set the Image property

to an instance of an image, as shown in the following example:

Windows Programming 1 Basic Windows Programming Slide 54

Page 55: Week 2:  WINDOWS PROGRAMMING

C#4.0

Picturebox - demo

Windows Programming 1 Chapter 15:Basic Windows Programming Slide 55

Module 2- 6

Page 56: Week 2:  WINDOWS PROGRAMMING

C#4.0

Picturebox - demo

Windows Programming 1 Chapter 15:Basic Windows Programming Slide 56

Page 57: Week 2:  WINDOWS PROGRAMMING

C#4.0

Picturebox - demo

Windows Programming 1 Chapter 15:Basic Windows Programming Slide 57

Module 2- 11

Page 58: Week 2:  WINDOWS PROGRAMMING

C#4.0

Picturebox - demo

Windows Programming 1 Chapter 15:Basic Windows Programming Slide 58