+ All Categories
Home > Documents > 2002 Prentice Hall. All rights reserved. 1 Chapter 12 – Graphical User Interface Concepts: Part 1...

2002 Prentice Hall. All rights reserved. 1 Chapter 12 – Graphical User Interface Concepts: Part 1...

Date post: 21-Dec-2015
Category:
View: 213 times
Download: 0 times
Share this document with a friend
Popular Tags:
60
2002 Prentice Hall. All rights reserved. 1 Chapter 12 – Graphical User Interface Concepts: Part 1 Outline 12.1 Introduction 12.2 Windows Forms 12.3 Event-Handling Model 12.3.1 Basic Event Handling 12.4 Control Properties and Layout 12.5 Labels, TextBoxes and Buttons 12.6 GroupBoxes and Panels 12.7 CheckBoxes and RadioButtons 12.8 PictureBoxes 12.9 Mouse-Event Handling 12.10 Keyboard-Event Handling
Transcript

2002 Prentice Hall. All rights reserved.

1

Chapter 12 – Graphical User Interface Concepts: Part 1

Outline12.1 Introduction12.2   Windows Forms12.3   Event-Handling Model

12.3.1 Basic Event Handling12.4   Control Properties and Layout12.5   Labels, TextBoxes and Buttons12.6   GroupBoxes and Panels12.7   CheckBoxes and RadioButtons12.8   PictureBoxes12.9   Mouse-Event Handling12.10  Keyboard-Event Handling

2002 Prentice Hall. All rights reserved.

2

12.1 Introduction

• GUI– Graphical User Interface

• Allows visual interaction

• Event driven

– User interaction generates events

• Distinctive “look” and “feel”

• Learn new applications more quickly

2002 Prentice Hall. All rights reserved.

3

12.1 Introduction

Fig. 12.1 Sample Internet Explorer window with GUI components.

2002 Prentice Hall. All rights reserved.

4

12.1 Introduction

• GUI Components– Objects with which user interacts

• Event generation

– Contained in Toolbox• Control

– Component with graphical representation

2002 Prentice Hall. All rights reserved.

5

12.1 Introduction

Component 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. This area also can display information.

Button An area that triggers an event when clicked.

CheckBox A component that is either selected or not selected.

ComboBox A drop-down list of items from which the user can make a selection either by clicking an item in the list or by typing into a box.

ListBox An area in which a list of items is displayed. The user can make a selection from the list by clicking on any element. Multiple elements can be selected.

Panel A container in which components can be placed.

Scrollbar A component that allows the user to access a range of values that normally cannot fit in the controller’s container.

Fig. 12.2 Some basic GUI components.

Fig. 12.2 Some basic GUI components.

2002 Prentice Hall. All rights reserved.

6

12.2 Windows Forms

• Form– Graphical element used to create GUIs

– Click and drag component from Toolbox• Code generated

– Component is instantiated

– Basic properties are set

2002 Prentice Hall. All rights reserved.

7

12.2 Windows Forms

Fig. 12.3 Components and controls for Windows Forms.

2002 Prentice Hall. All rights reserved.

8

12.2 Windows Forms

Form Properties and Events

Description / Delegate and Event Arguments

Common Properties

AcceptButton Button that is clicked when Enter is pressed. AutoScroll Boolean value that allows or disallows the use of scrollbars to appear

when needed.

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, and the default font of controls added to the form.

Text Text in the form’s title bar.

Common Methods Close Closes a 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.

Fig. 12.4 Common Form properties and events.

Fig. 12.4 Common Form properties and events.

2002 Prentice Hall. All rights reserved.

9

12.3 Event-Handling Model

• Event handler– Method called by event

– Receives event information

• Notion of delegates– Objects that reference methods

– Act as intermediaries between objects creating events and methods handling events

• Event multicasting– Inclusion of multiple handlers for one event

2002 Prentice Hall. All rights reserved.

10

12.3 Event-Handling Model

Fig. 12.5 Event-handling model using delegates.

Object A raises event E Delegate for event E

Handler 1 for event E

Handler 3 for event E

Handler 2 for event E

calls

calls

2002 Prentice Hall. All rights reserved.

11

12.3 Event-Handling Model

Fig. 12.6 Events section of the Properties window.

2002 Prentice Hall.All rights reserved.

Outline12

SimpleEvenExample.vb

1 ' Fig. 12.7: SimpleEventExample.vb2 ' Program demonstrating simple event handler.3 4 Public Class FrmSimple5 Inherits System.Windows.Forms.Form6 7 #Region " Windows Form Designer generated code "8 9 Public Sub New()10 MyBase.New()11 12 ' This call is required by the Windows Form Designer.13 InitializeComponent()14 15 16 ' Add any initialization after the 17 ' InitializeComponent() call18 End Sub ' New19 20 ' Form overrides dispose to clean up the component list.21 Protected Overloads Overrides Sub Dispose( _ 22 ByVal disposing As Boolean)23 24 If disposing Then25 26 If Not (components Is Nothing) Then27 components.Dispose()28 End If29 30 End If31 32 MyBase.Dispose(disposing)33 End Sub ' Dispose34 35 Friend WithEvents lblOutput As System.Windows.Forms.Label

Beginning of Visual Studiogenerated code

2002 Prentice Hall.All rights reserved.

Outline13

SimpleEvenExample.vb

36 37 ' Required by the Windows Form Designer38 Private components As System.ComponentModel.Container39 40 ' NOTE: The following procedure is required by 41 ' the Windows Form Designer.42 ' It can be modified using the Windows Form Designer. 43 ' Do not modify it using the code editor.44 <System.Diagnostics.DebuggerStepThrough()> _45 Private Sub InitializeComponent()46 Me.lblOutput = New System.Windows.Forms.Label()47 Me.SuspendLayout()48 '49 'lblOutput50 '51 Me.lblOutput.Location = New System.Drawing.Point(32, 48)52 Me.lblOutput.Name = "lblOutput"53 Me.lblOutput.Size = New System.Drawing.Size(168, 40)54 Me.lblOutput.TabIndex = 055 Me.lblOutput.Text = "Click Me!"56 '57 'FrmSimple58 '59 Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)60 Me.ClientSize = New System.Drawing.Size(272, 237)61 Me.Controls.AddRange( _62 New System.Windows.Forms.Control() {Me.lblOutput})63 64 Me.Name = "FrmSimple"65 Me.Text = "SimpleEventExample"66 Me.ResumeLayout(False)67 End Sub68 69 #End Region70

End of Visual Studiogenerated code

2002 Prentice Hall.All rights reserved.

Outline14

SimpleEvenExample.vb

71 ' handler for click event on lblOutput72 Private Sub lblOutput_Click(ByVal sender As Object, _73 ByVal e As System.EventArgs) Handles lblOutput.Click74 75 MessageBox.Show("Label was pressed")76 End Sub ' lblOutput_Click77 78 End Class ' FrmSimpleExample

Name of the handlerfollowed by an underscore

and the event name

Reference to the objectthat generated the event

Event arguments object

Event-handling codedisplays a message box

2002 Prentice Hall. All rights reserved.

15

12.3 Event-Handling Model

Fig. 12.8 List of Form events.

2002 Prentice Hall. All rights reserved.

16

12.3 Event-Handling Model

Fig. 12.9 Details of Click event.

2002 Prentice Hall. All rights reserved.

17

12.4 Control Properties and Layout

Class Control Properties and Methods

Description

Common Properties

BackColor Sets the background color of the control.

BackgroundImage Sets the background image of the control.

Enabled Indicates whether the control is enabled (i.e., if the user can interact with it). A disabled control will still be displayed, but portions of the control will appear in gray.

Focused Indicates whether a control has the focus.

Font Sets the Font to use in Text property of control.

ForeColor Sets the foreground color of the control. This is usually the color of the Text property.

TabIndex Sets the tab order of the control. When the Tab key is pressed, the focus is moved to various controls according to the tab order. This order can be set by the programmer.

TabStop Indicates whether user can employ the Tab key to select the control. If True, then this feature is enabled.

Text Sets the text associated with the control. The location and appearance varies depending on the type of control.

TextAlign Establishes the alignment of the text on the control—possibilities are one of three horizontal positions (left, center or right) and one of three vertical positions (top, middle or bottom).

Visible Indicates 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).

Fig. 12.10 Class Control properties and methods.

Fig. 12.10 Class Control properties and methods.

2002 Prentice Hall. All rights reserved.

18

12.4 Control Properties and Layout

• Method Focus– Transfers focus to control

• Active control

• Method Hide– Hides control

• Visible property is false

• Method Show– Shows control

• Visible property is true

2002 Prentice Hall. All rights reserved.

19

12.4 Control Properties and Layout

• Anchoring– Specifying layout of controls within container

– Controls remain fixed distances from inside of container

• Docking– Sets dimensions of control to dimensions of container at all

times

2002 Prentice Hall. All rights reserved.

20

12.4 Control Properties and Layout

Fig. 12.11 Anchoring demonstration.

2002 Prentice Hall. All rights reserved.

21

12.4 Control Properties and Layout

Fig. 12.12 Manipulating the Anchor property of a control.

2002 Prentice Hall. All rights reserved.

22

12.4 Control Properties and Layout

Fig. 12.13 Docking demonstration.

2002 Prentice Hall. All rights reserved.

23

12.4 Control Properties and Layout

Common Layout Properties

Description

Anchor Attaches control to the side of parent container. Used during resizing. Possible values include top, left and right.

Dock Allows controls to extend themselves along the sides of their containers—values cannot be combined.

DockPadding (for containers)

Sets the dock spacing for controls inside the container. Default is zero, causing controls to appear flush with the side of the container.

Location Specifies the location of the upper-left corner of the control, in relation to its container.

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

MinimumSize, MaximumSize (for Windows Forms)

Indicates the minimum and maximum size of the form.

Fig. 12.14 Control layout properties.

Fig. 12.14 Control layout properties.

2002 Prentice Hall. All rights reserved.

24

12.5 Labels, TextBoxes and Buttons

• Label– Displays read-only text

• Textbox– Displays text

– Text input by user

• Button– Click to trigger action

2002 Prentice Hall. All rights reserved.

25

12.5 Labels, TextBoxes and Buttons

Common Label Properties

Description / Delegate and Event Arguments

Font The font used by the text on the Label.

Text The text that appears on the Label.

TextAlign The alignment of the Label’s text on the control. Possibilities are one of three horizontal positions (left, center or right) and one of three vertical positions (top, middle or bottom).

Fig. 12.15 Common Label properties.

Fig. 12.15 Common Label properties.

2002 Prentice Hall. All rights reserved.

26

12.5 Labels, TextBoxes and Buttons

TextBox Properties and Events

Description / Delegate and Event Arguments

Common Properties

AcceptsReturn If True, pressing Enter creates a new line (if textbox is configured

to contain multiple lines.) If False, pressing Enter clicks the default button of the form.

Multiline If True, textbox can span multiple lines. The default value is False.

PasswordChar If a character is entered, the TextBox becomes a password box, and the stated character replaces all characters typed. If no character is specified, Textbox displays the typed text.

ReadOnly If True, TextBox has a gray background, and its text cannot be edited. The default value is False.

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

Text Specifies the text to be displayed in the text box.

Common Events (Delegate EventHandler, event arguments EventArgs)

TextChanged Raised when text changes in TextBox (i.e., when the user adds or deletes characters). When a user double-clicks the TextBox control in design view, an empty event handler for this event is generated.

Fig. 12.16 TextBox properties and events.

Fig. 12.16 TextBox properties and events.

2002 Prentice Hall. All rights reserved.

27

12.5 Labels, TextBoxes and Buttons

Button properties and events

Description / Delegate and Event Arguments

Common Properties

Text Specifies text displayed on the Button face.

Common Events (Delegate EventHandler, event arguments EventArgs)

Click Is raised when user clicks the control. When a user double-clicks on the Button control in design view, an empty event handler for this event is generated.

Fig. 12.17 Button properties and events.

Fig. 12.17 Button properties and events.

2002 Prentice Hall.All rights reserved.

Outline28

LabelTextBoxButtonTest.vb

1 ' Fig. 12.18: LabelTextBoxButtonTest.vb2 ' Using a textbox, label and button to display the hidden3 ' text in a password box.4 5 Public Class FrmButtonTest6 Inherits System.Windows.Forms.Form7 8 ' Visual Studio .NET generated code9 10 ' handles cmdShow_Click events11 Private Sub cmdShow_Click(ByVal sender As System.Object, _12 ByVal e As System.EventArgs) Handles cmdShow.Click13 14 lblOutput.Text = txtInput.Text15 End Sub ' cmdShow_Click16 17 End Class ' FrmButtonTest

The label’s text property is setequal to the value of the textbox’s

text property, which was entered by the user

2002 Prentice Hall. All rights reserved.

29

12.6 GroupBoxes and Panels

• Groupboxes– Arrange controls on a GUI

– Can display captions

– Do not include scrollbars

• Panels– Arrange controls on a GUI

– Cannot display captions

– Include scrollbars

2002 Prentice Hall. All rights reserved.

30

12.6 GroupBoxes and Panels

GroupBox Properties Description

Controls Lists the controls that the GroupBox contains.

Text Specifies text displayed on the top portion of the GroupBox (its caption).

Fig. 12.19 GroupBox properties.

Panel Properties Description

AutoScroll Indicates whether scrollbars appear when the Panel is too small to hold its controls. Default is False.

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

Controls Lists the controls that the Panel contains.

Fig. 12.20 Panel properties.

Fig. 12.19 GroupBox properties.

Fig. 12.20 Panel properties.

2002 Prentice Hall. All rights reserved.

31

12.6 GroupBoxes and Panels

Fig. 12.21 Creating a Panel with scrollbars.

2002 Prentice Hall.All rights reserved.

Outline32

GroupBoxPanelExample.vb

1 ' Fig. 12.22: GroupBoxPanelExample.vb 2 ' Using GroupBoxes and Panels to hold buttons.3 4 Public Class FrmGroupBox5 Inherits System.Windows.Forms.Form6 7 ' Visual Studio.NET generated code8 9 ' event handlers to change lblMessage10 Private Sub cmdHi_Click(ByVal sender As System.Object, _11 ByVal e As System.EventArgs) Handles cmdHi.Click12 13 lblMessage.Text = "Hi pressed" 14 End Sub ' cmdHi_Click15 16 ' bye button handler17 Private Sub cmdBye_Click(ByVal sender As System.Object, _18 ByVal e As System.EventArgs) Handles cmdBye.Click19 20 lblMessage.Text = "Bye pressed" 21 End Sub ' cmdBye_Click22 23 ' far left button handler24 Private Sub cmdLeft_Click(ByVal sender As System.Object, _25 ByVal e As System.EventArgs) Handles cmdLeft.Click26 27 lblMessage.Text = "Far left pressed"28 End Sub ' cmdLeft_Click29

Event handling code displays the appropriate

text property for lbl.Message

2002 Prentice Hall.All rights reserved.

Outline33

GroupBoxPanelExample.vb

30 ' far right button handler31 Private Sub cmdRight_Click(ByVal sender As System.Object, _32 ByVal e As System.EventArgs) Handles cmdRight.Click33 34 lblMessage.Text = "Far right pressed"35 End Sub ' cmdRight_Click36 37 End Class ' FrmGroupBox

2002 Prentice Hall. All rights reserved.

34

12.7 CheckBoxes and RadioButtons

• State Buttons– CheckBoxes

• Any number can be checked at a time

– RadioButtons• Usually organized in groups and only one checked at a time

2002 Prentice Hall. All rights reserved.

35

12.7 CheckBoxes and RadioButtons

CheckBox events and properties

Description / Delegate and Event Arguments

Common Properties

Checked Indicates whether the CheckBox has been checked.

CheckState Indicates whether the Checkbox is checked (contains a black checkmark) or unchecked (blank). An enumeration with values Checked, Unchecked or Indeterminate.

Text Specifies the 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. When a user double-clicks the CheckBox control in design view, an empty event handler for this event is generated.

CheckStateChanged Raised when the CheckState property changes.

Fig. 12.23 CheckBox properties and events.

Fig. 12.23 CheckBox properties and events.

2002 Prentice Hall.All rights reserved.

Outline36

CheckBoxTest.vb

1 ' Fig. 12.24: CheckBoxTest.vb2 ' Using CheckBoxes to toggle italic and bold styles.3 4 Public Class FrmCheckBox5 Inherits System.Windows.Forms.Form6 7 ' Visual Studio .NET IDE generated code8 9 ' use Xor to toggle italic, keep other styles same10 Private Sub chkItalic_CheckedChanged _11 (ByVal sender As System.Object, ByVal e As System.EventArgs) _12 Handles chkItalic.CheckedChanged13 14 lblOutput.Font = New Font(lblOutput.Font.Name, _15 lblOutput.Font.Size, lblOutput.Font.Style _16 Xor FontStyle.Italic)17 End Sub ' chkItalic_CheckedChanged18 19 ' use Xor to toggle bold, keep other styles same20 Private Sub chkBold_CheckedChanged _21 (ByVal sender As System.Object, ByVal e As System.EventArgs) _22 Handles chkBold.CheckedChanged23 24 lblOutput.Font = New Font(lblOutput.Font.Name, _25 lblOutput.Font.Size, lblOutput.Font.Style _26 Xor FontStyle.Bold)27 End Sub ' chkBold_CheckedChanged28 29 End Class ' FrmCheckBox

2002 Prentice Hall.All rights reserved.

Outline37

CheckBoxTest.vb

2002 Prentice Hall. All rights reserved.

38

12.7 CheckBoxes and RadioButtons

RadioButton properties and events

Description / Delegate and Event Arguments

Common Properties

Checked Indicates whether the RadioButton is checked.

Text Specifies the 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. When a user double-clicks the TextBox control in design view, an empty event handler for this event is generated.

Fig. 12.25 RadioButton properties and events.

Fig. 12.25 RadioButton properties and events.

訂正 : p502 上方 CheckedChanged 段之第二行 , 將 “ Textbox” 改為 “ RadioButton”

2002 Prentice Hall.All rights reserved.

Outline39

RadioButtonTest.vb

1 ' Fig. 12.26: RadioButtonTest.vb2 ' Using RadioButtons to set message window options.3 4 Public Class FrmRadioButton5 Inherits System.Windows.Forms.Form6 7 Private iconType As MessageBoxIcon8 Private buttonType As MessageBoxButtons9 10 ' Visual Studio .NET generated code11 12 ' display message box and obtain dialogue button clicked13 Private Sub cmdDisplay_Click(ByVal sender _14 As System.Object, ByVal e As System.EventArgs) _15 Handles cmdDisplay.Click16 17 Dim dialog As DialogResult = MessageBox.Show( _18 "This is Your Custom MessageBox", "VB", buttonType, _19 iconType)20 21 ' check for dialog result and display on label22 Select Case dialog23 24 Case DialogResult.OK25 lblDisplay.Text = "OK was pressed"26 27 Case DialogResult.Cancel28 lblDisplay.Text = "Cancel was pressed"29 30 Case DialogResult.Abort31 lblDisplay.Text = "Abort was pressed"32 33 Case DialogResult.Retry34 lblDisplay.Text = "Retry was pressed"35

2002 Prentice Hall.All rights reserved.

Outline40

RadioButtonTest.vb

36 Case DialogResult.Ignore37 lblDisplay.Text = "Ignore was pressed"38 39 Case DialogResult.Yes40 lblDisplay.Text = "Yes was pressed"41 42 Case DialogResult.No43 lblDisplay.Text = "No was pressed"44 End Select45 46 End Sub ' cmdDisplay_Click47 48 ' set button type to OK49 Private Sub radOk_CheckedChanged(ByVal sender _50 As System.Object, ByVal e As System.EventArgs) _51 Handles radOk.CheckedChanged52 53 buttonType = MessageBoxButtons.OK54 End Sub ' radOk_CheckedChanged55 56 ' set button type to OkCancel57 Private Sub radOkCancel_CheckedChanged(ByVal sender _58 As System.Object, ByVal e As System.EventArgs) _59 Handles radOkCancel.CheckedChanged60 61 buttonType = MessageBoxButtons.OKCancel62 End Sub ' radOkCancel_CheckedChanged63 64 ' set button type to AbortRetryIgnore65 Private Sub radAbortRetryIgnore_CheckedChanged(ByVal sender _66 As System.Object, ByVal e As System.EventArgs) _67 Handles radAbortRetryIgnore.CheckedChanged68 69 buttonType = MessageBoxButtons.AbortRetryIgnore70 End Sub ' radAbortRetryIgnore_CheckedChanged

2002 Prentice Hall.All rights reserved.

Outline41

RadioButtonTest.vb

71 72 ' set button type to YesNoCancel73 Private Sub radYesNoCancel_CheckedChanged(ByVal sender _74 As System.Object, ByVal e As System.EventArgs) _75 Handles radYesNoCancel.CheckedChanged76 77 buttonType = MessageBoxButtons.YesNoCancel78 End Sub ' radYesNoCancel_CheckedChanged79 80 ' set button type to YesNo81 Private Sub radYesNo_CheckedChanged(ByVal sender _82 As System.Object, ByVal e As System.EventArgs) _83 Handles radYesNo.CheckedChanged84 85 buttonType = MessageBoxButtons.YesNo86 End Sub ' radYesNo_CheckedChanged87 88 ' set button type to RetryCancel 89 Private Sub radRetryCancel_CheckedChanged(ByVal sender _90 As System.Object, ByVal e As System.EventArgs) _91 Handles radRetryCancel.CheckedChanged92 93 buttonType = MessageBoxButtons.RetryCancel94 End Sub ' radRetryCancel_CheckedChanged95 96 ' set icon type to Asterisk when Asterisk checked97 Private Sub radAsterisk_CheckedChanged(ByVal sender _98 As System.Object, ByVal e As System.EventArgs) _99 Handles radAsterisk.CheckedChanged100 101 iconType = MessageBoxIcon.Asterisk102 End Sub ' radAsterisk_CheckedChanged103

2002 Prentice Hall.All rights reserved.

Outline42

RadioButtonTest.vb

104 ' set icon type to Error when Error checked105 Private Sub radError_CheckedChanged(ByVal sender _106 As System.Object, ByVal e As System.EventArgs) _107 Handles radError.CheckedChanged108 109 iconType = MessageBoxIcon.Error110 End Sub ' radError_CheckedChanged111 112 ' set icon type to Exclamation when Exclamation checked113 Private Sub radExclamation_CheckedChanged(ByVal sender _114 As System.Object, ByVal e As System.EventArgs) _115 Handles radExclamation.CheckedChanged116 117 iconType = MessageBoxIcon.Exclamation118 End Sub ' radExclamation_CheckedChanged119 120 ' set icon type to Hand when Hand checked121 Private Sub radHand_CheckedChanged(ByVal sender _122 As System.Object, ByVal e As System.EventArgs) _123 Handles radHand.CheckedChanged124 125 iconType = MessageBoxIcon.Hand126 End Sub ' radHand_CheckedChanged127 128 ' set icon type to Information when Information checked129 Private Sub radInformation_CheckedChanged(ByVal sender _130 As System.Object, ByVal e As System.EventArgs) _131 Handles radInformation.CheckedChanged132 133 iconType = MessageBoxIcon.Information134 End Sub ' radInformation_CheckedChanged135

2002 Prentice Hall.All rights reserved.

Outline43

RadioButtonTest.vb

136 ' set icon type to Question when Question checked137 Private Sub radQuestion_CheckedChanged(ByVal sender _138 As System.Object, ByVal e As System.EventArgs) _139 Handles radQuestion.CheckedChanged140 141 iconType = MessageBoxIcon.Question142 End Sub ' radQuestion_CheckedChanged143 144 ' set icon type to Stop when Stop checked145 Private Sub radStop_CheckedChanged(ByVal sender _146 As System.Object, ByVal e As System.EventArgs) _147 Handles radStop.CheckedChanged148 149 iconType = MessageBoxIcon.Stop150 End Sub ' radStop_CheckedChanged151 152 ' set icon type to Warning when Warning checked153 Private Sub radWarning_CheckedChanged(ByVal sender _154 As System.Object, ByVal e As System.EventArgs) _155 Handles radWarning.CheckedChanged156 157 iconType = MessageBoxIcon.Warning158 End Sub ' radWarning_CheckedChanged159 160 End Class ' FrmRadioButtons

2002 Prentice Hall.All rights reserved.

Outline44

RadioButtonTest.vb

2002 Prentice Hall.All rights reserved.

Outline45

RadioButtonTest.vb

2002 Prentice Hall.All rights reserved.

Outline46

RadioButtonTest.vb

2002 Prentice Hall.All rights reserved.

Outline47

RadioButtonTest.vb

2002 Prentice Hall. All rights reserved.

48

12.8 PictureBoxes

• PictureBoxes– Display images

• Bitmap

• GIF (Graphics Interchange Format)

• JPEG (Joint Photographic Expert Group)

• Metafile

– Image property• Image to be displayed

2002 Prentice Hall. All rights reserved.

49

12.8 PictureBoxes

PictureBox properties and events

Description / Delegate and Event Arguments

Common Properties

Image Sets the image to display in the PictureBox.

SizeMode Enumeration that controls image sizing and positioning. Values are Normal (default), StretchImage, AutoSize and CenterImage. Normal places image in top-left corner of PictureBox, and CenterImage puts image in middle (both cut image off if it is too large). StretchImage resizes image to fit in PictureBox. AutoSize resizes PictureBox to hold image.

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.

Fig. 12.30 PictureBox properties and events.

Fig. 12.30 PictureBox properties and events.

2002 Prentice Hall.All rights reserved.

Outline50

PictureBoxTest.vb

1 ' Fig. 12.31: PictureBoxTest.vb2 ' Using a PictureBox to display images.3 4 Imports System.IO5 6 Public Class FrmPictureBox7 Inherits System.Windows.Forms.Form8 9 Private imageNumber As Integer = -110 11 ' Visual Studio.NET generated code12 13 ' replace image in picImage14 Private Sub picImage_Click(ByVal sender As System.Object, _15 ByVal e As System.EventArgs) Handles picImage.Click16 17 ' imageNumber from 0 to 218 imageNumber = (imageNumber + 1) Mod 3 19 20 ' create Image object from file, display in PictureBox21 picImage.Image = Image.FromFile _22 (Directory.GetCurrentDirectory & "\images\image" & _23 imageNumber & ".bmp")24 End Sub ' picImage_Click25 26 End Class ' FrmPictureBox

An image object is createdfrom file and set as the

PictureBox’s image property

2002 Prentice Hall.All rights reserved.

Outline51

2002 Prentice Hall. All rights reserved.

52

12.9 Mouse-Event Handling

• MouseEventArgs Properties– Button

• Mouse button pressed

– Clicks• Number of mouse clicks

– X• X-coordinate of event, within the control

– Y• Y-coordinate of event, within the control

2002 Prentice Hall. All rights reserved.

53

12.9 Mouse-Event Handling

Mouse Events, Delegates and Event Arguments

Mouse Events (Delegate EventHandler, event arguments EventArgs)

MouseEnter Raised if the mouse cursor enters the area of the control.

MouseLeave Raised if the mouse cursor leaves the area of the control.

Mouse Events (Delegate MouseEventHandler, event arguments MouseEventArgs)

MouseDown Raised if the mouse button is pressed while its cursor is over the area of the control.

MouseHover Raised if the mouse cursor hovers over the area of the control.

MouseMove Raised if the mouse cursor is moved while in the area of the control.

MouseUp Raised if the mouse button is released when the cursor is over the area of the control.

Class MouseEventArgs

Properties

Button Specifies the mouse button that was pressed (left, right, middle or none).

Clicks Indicates the number of times that the mouse button was clicked.

X The x-coordinate of the event, relative to the component. Y The y-coordinate of the event, relative to the component. Fig. 12.32 Mouse events, delegates and event arguments.

Fig. 12.32 Mouse events, delegates and event arguments.

2002 Prentice Hall.All rights reserved.

Outline54

Painter.vb

1 ' Fig. 12.33: Painter.vb2 ' Using the mouse to draw on a form.3 4 Public Class FrmPainter5 Inherits System.Windows.Forms.Form6 7 Dim shouldPaint As Boolean = False8 9 ' Visual Studio .NET IDE generated code10 11 ' draw circle if shouldPaint is True12 Private Sub FrmPainter_MouseMove( _13 ByVal sender As System.Object, _14 ByVal e As System.Windows.Forms.MouseEventArgs) _15 Handles MyBase.MouseMove16 17 ' paint circle if mouse pressed18 If shouldPaint Then19 Dim graphic As Graphics = CreateGraphics()20 21 graphic.FillEllipse _22 (New SolidBrush(Color.BlueViolet), e.X, e.Y, 4, 4)23 End If24 25 End Sub ' FrmPainter_MouseMove26 27 ' set shouldPaint to True28 Private Sub FrmPainter_MouseDown(ByVal sender As Object, _29 ByVal e As System.Windows.Forms.MouseEventArgs) _30 Handles MyBase.MouseDown31 32 shouldPaint = True33 End Sub ' FrmPainter_MousDown34

Event handling for Mouse event MouseDown sets variable

shouldPaint to True

Event handling code beginsfor event MouseMove

2002 Prentice Hall.All rights reserved.

Outline55

Painter.vb

35 ' set shouldPaint to False 36 Private Sub FrmPainter_MouseUp(ByVal sender As Object, _37 ByVal e As System.Windows.Forms.MouseEventArgs) _38 Handles MyBase.MouseUp39 40 shouldPaint = False41 End Sub ' FrmPainter_MouseUp42 43 End Class ' FrmPainter Event handling for Mouse event

MouseUp sets variable shouldPaint to False

2002 Prentice Hall. All rights reserved.

56

12.10 Keyboard-Event Handling

• Key Events– Generated when keys are pressed and released

– KeyPress• Can return a Char for any ASCII character pressed

– KeyUp and KeyDown• Test for special modifier keys

• Use KeyEventArgs

2002 Prentice Hall. All rights reserved.

57

12.10 Keyboard-Event Handling

Keyboard Events, Delegates and Event Arguments

Key Events (Delegate KeyEventHandler, event arguments KeyEventArgs)

KeyDown Raised when key is initially pushed down.

KeyUp Raised when key is released.

Key Events (Delegate KeyPressEventHandler, event arguments KeyPressEventArgs)

KeyPress Raised when key is pressed. Occurs repeatedly while key is held down, at a rate specified by the operating system.

Class KeyPressEventArgs Properties

KeyChar Returns the ASCII character for the key pressed.

Handled Indicates whether or not the KeyPress event was handled.

Class KeyEventArgs Properties

Alt Indicates whether the Alt key was pressed.

Control Indicates whether the Control key was pressed.

Shift Indicates whether the Shift key was pressed.

Handled Indicates whether the event was handled.

KeyCode Returns the key code for the key as a Keys enumeration. This does not include modifier-key information. Used to test for a specific key.

KeyData Returns the key code for a key as a Keys enumeration, combined with modifier information. Used to determine all information about the pressed key.

KeyValue Returns the key code as an int, rather than as a Keys enumeration. Used to obtain a numeric representation of the pressed key.

Modifiers Returns a Keys enumeration for any modifier keys pressed (Alt, Control and Shift). Used to determine modifier-key information only.

Fig. 12.34 Keyboard events, delegates and event arguments.

Fig. 12.34 Keyboard events, delegates and event arguments.

2002 Prentice Hall.All rights reserved.

Outline58

KeyDemo.vb

1 ' Fig. 12.35: KeyDemo.vb2 ' Displaying information about a user-pressed key.3 4 Public Class FrmKeyDemo5 Inherits System.Windows.Forms.Form6 7 ' Visual Studio.NET generated code8 9 ' event handler for key press10 Private Sub FrmKeyDemo_KeyPress(ByVal sender As System.Object, _11 ByVal e As System.windows.Forms.KeyPressEventArgs) _12 Handles MyBase.KeyPress13 14 lblCharacter.Text = "Key pressed: " & e.KeyChar15 End Sub16 17 ' display modifier keys, key code, key data and key value18 Private Sub FrmKeyDemo_KeyDown(ByVal sender As System.Object, _19 ByVal e As System.Windows.Forms.KeyEventArgs) _20 Handles MyBase.KeyDown21 22 lblInformation.Text = ""23 24 ' if key is Alt25 If e.Alt Then26 lblInformation.Text &= "Alt: Yes" & vbCrLf27 Else28 lblInformation.Text &= "Alt: No" & vbCrLf29 End If30

KeyDown event handlerdisplays information fromits KeyEventArgs object

KeyPress event handleraccesses the KeyChar property

of the KeyPressEventArgs objectand displays the Key

2002 Prentice Hall.All rights reserved.

Outline59

KeyDemo.vb

31 ' if key is Shift32 If e.Shift Then33 lblInformation.Text &= "Shift: Yes" & vbCrLf34 Else35 lblInformation.Text &= "Shift: No" & vbCrLf36 End If37 38 ' if key is Ctrl39 If e.Control Then40 lblInformation.Text &= "Ctrl: Yes" & vbCrLf & _41 "KeyCode: " & e.KeyCode.ToString & vbCrLf & _42 "KeyData: " & e.KeyData.ToString & vbCrLf & _43 "KeyValue: " & e.KeyValue44 Else45 lblInformation.Text &= "Ctrl: No" & vbCrLf & _46 "KeyCode: " & e.KeyCode.ToString & vbCrLf & _47 "KeyData: " & e.KeyData.ToString & vbCrLf & _48 "KeyValue: " & e.KeyValue49 End If50 51 End Sub ' FrmKeyDemo_KeyDown52 53 ' clear labels when key is released54 Private Sub FrmKeyDemo_KeyUp(ByVal sender As System.Object, _55 ByVal e As System.windows.Forms.KeyEventArgs) _56 Handles MyBase.KeyUp57 58 lblInformation.Text = ""59 lblCharacter.Text = ""60 End Sub ' FrmKeyDemo_KeyUp61 62 End Class ' FrmKeyDemo

The KeyCode property returnsa Keys enumeration, which must be

converted to a String via method ToString

Both labels are clearedwhen a key is released

2002 Prentice Hall.All rights reserved.

Outline60

KeyDemo.vb


Recommended