Chapter 12 Graphical User Interface Concepts Part 1

  • Slides: 56
Download presentation
Chapter 12 – Graphical User Interface Concepts: Part 1 Outline 12. 1 12. 2

Chapter 12 – Graphical User Interface Concepts: Part 1 Outline 12. 1 12. 2 12. 3 12. 4 12. 5 12. 6 12. 7 12. 8 12. 9 12. 10 Introduction Windows Forms Event-Handling Model 12. 3. 1 Basic Event Handling Control Properties and Layout Labels, Text. Boxes and Buttons Group. Boxes and Panels Check. Boxes and Radio. Buttons Picture. Boxes Mouse-Event Handling Keyboard-Event Handling 2002 Prentice Hall. All rights reserved. 1

2 12. 1 Introduction • GUI – Graphical User Interface • Allows visual interaction

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.

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 •

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 Fig. 12. 2 Some basic GUI components. 2002 Prentice Hall.

5 12. 1 Introduction 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

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.

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 Fig. 12. 4 Common Form properties and events. 2002

8 12. 2 Windows Forms 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 –

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 calls Handler 1 for event E calls Object A

10 12. 3 Event-Handling Model calls Handler 1 for event E calls Object A raises event E Delegate for event E Handler 2 for event E Handler 3 for event E Fig. 12. 5 Event-handling model using delegates. 2002 Prentice Hall. All rights reserved.

11 12. 3 Event-Handling Model Fig. 12. 6 Events section of the Properties window.

11 12. 3 Event-Handling Model Fig. 12. 6 Events section of the Properties window. 2002 Prentice Hall. All rights reserved.

1 2 3 4 5 6 7 8 9 10 11 12 13 14

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 ' Fig. 12. 7: Simple. Event. Example. vb ' Program demonstrating simple event handler. Outline 12 Public Class Frm. Simple Inherits System. Windows. Form #Region " Windows Form Designer generated code " Public Sub New() My. Base. New() Beginning of Visual Studio generated code ' This call is required by the Windows Form Designer. Initialize. Component() ' Add any initialization after the ' Initialize. Component() call End Sub ' New ' Form overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose( _ By. Val disposing As Boolean) Simple. Even. Exampl e. vb If disposing Then If Not (components Is Nothing) Then components. Dispose() End If My. Base. Dispose(disposing) End Sub ' Dispose Friend With. Events lbl. Output As System. Windows. Forms. Label 2002 Prentice Hall. All rights reserved.

36 37 38 39 40 41 42 43 44 45 46 47 48 49

36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 ' Required by the Windows Form Designer Private components As System. Component. Model. Container ' NOTE: The following procedure is required by ' the Windows Form Designer. ' It can be modified using the Windows Form Designer. ' Do not modify it using the code editor. <System. Diagnostics. Debugger. Step. Through ()> _ Private Sub Initialize. Component() Me. lbl. Output = New System. Windows. Forms. Label() Me. Suspend. Layout() ' 'lbl. Output ' Me. lbl. Output. Location = New System. Drawing. Point(32, 48) Me. lbl. Output. Name = "lbl. Output" Me. lbl. Output. Size = New System. Drawing. Size(168, 40) Me. lbl. Output. Tab. Index = 0 Me. lbl. Output. Text = "Click Me!" ' 'Frm. Simple ' Me. Auto. Scale. Base. Size = New System. Drawing. Size(5, 13) Me. Client. Size = New System. Drawing. Size(272, 237) Me. Controls. Add. Range( _ New System. Windows. Forms. Control() { Me. lbl. Output}) Outline Simple. Even. Exampl e. vb Me. Name = "Frm. Simple" Me. Text = "Simple. Event. Example" Me. Resume. Layout(False) End Sub #End Region 13 of Visual Studio generated code 2002 Prentice Hall. All rights reserved.

71 72 73 74 75 76 77 78 ' handler for click event on

71 72 73 74 75 76 77 78 ' handler for click event on lbl. Output Private Sub lbl. Output_Click(By. Val sender As Object, _ By. Val e As System. Event. Args) Handles lbl. Output. Click Message. Box. Show("Label was pressed") End Sub ' lbl. Output_Click End Class ' Frm. Simple. Example Name of the handler followed by an underscore and the event name Outline 14 Reference to the object that generated the event Event arguments object Event-handling code displays a message box Simple. Even. Exampl e. vb 2002 Prentice Hall. All rights reserved.

15 12. 3 Event-Handling Model Fig. 12. 8 List of Form events. 2002 Prentice

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

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 Fig. 12. 10 Class Control properties and

17 12. 4 Control Properties and Layout 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

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

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

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 Manipulating the Anchor property of

21 12. 4 Control Properties and Layout Fig. 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

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 Fig. 12. 14 Control layout properties. 2002

23 12. 4 Control Properties and Layout Fig. 12. 14 Control layout properties. 2002 Prentice Hall. All rights reserved.

24 12. 5 Labels, Text. Boxes and Buttons • Label – Displays read-only text

24 12. 5 Labels, Text. Boxes 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, Text. Boxes and Buttons Fig. 12. 15 Common Label properties.

25 12. 5 Labels, Text. Boxes and Buttons Fig. 12. 15 Common Label properties. 2002 Prentice Hall. All rights reserved.

26 12. 5 Labels, Text. Boxes and Buttons Fig. 12. 16 Text. Box properties

26 12. 5 Labels, Text. Boxes and Buttons Fig. 12. 16 Text. Box properties and events. 2002 Prentice Hall. All rights reserved.

27 12. 5 Labels, Text. Boxes and Buttons Fig. 12. 17 Button properties and

27 12. 5 Labels, Text. Boxes and Buttons Fig. 12. 17 Button properties and events. 2002 Prentice Hall. All rights reserved.

1 2 3 4 5 6 7 8 9 10 11 12 13 14

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 ' Fig. 12. 18: Label. Text. Box. Button. Test. vb ' Using a textbox, label and button to display the hidden ' text in a password box. Outline 28 Public Class Frm. Button. Test Inherits System. Windows. Form ' Visual Studio. NET generated code ' handles cmd. Show_Click events Private Sub cmd. Show_Click(By. Val sender As System. Object, _ By. Val e As System. Event. Args) Handles cmd. Show. Click lbl. Output. Text = txt. Input. Text End Sub ' cmd. Show_Click End Class ' Frm. Button. Test The label’s text property is set equal to the value of the textbox’s Label. Text. Box. Butt text property, which was on. Test. vb entered by the user 2002 Prentice Hall. All rights reserved.

29 12. 6 Group. Boxes and Panels • Groupboxes – Arrange controls on a

29 12. 6 Group. Boxes 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 Group. Boxes and Panels Fig. 12. 19 Group. Box properties. Fig.

30 12. 6 Group. Boxes and Panels Fig. 12. 19 Group. Box properties. Fig. 12. 20 Panel properties. 2002 Prentice Hall. All rights reserved.

31 12. 6 Group. Boxes and Panels Fig. 12. 21 Creating a Panel with

31 12. 6 Group. Boxes and Panels Fig. 12. 21 Creating a Panel with scrollbars. 2002 Prentice Hall. All rights reserved.

1 2 3 4 5 6 7 8 9 10 11 12 13 14

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 ' Fig. 12. 22: Group. Box. Panel. Example. vb ' Using Group. Boxes and Panels to hold buttons. Outline 32 Public Class Frm. Group. Box Inherits System. Windows. Form ' Visual Studio. NET generated code ' event handlers to change lbl. Message Private Sub cmd. Hi_Click(By. Val sender As System. Object, _ By. Val e As System. Event. Args) Handles cmd. Hi. Click lbl. Message. Text = "Hi pressed" End Sub ' cmd. Hi_Click ' bye button handler Private Sub cmd. Bye_Click(By. Val sender As System. Object, _ By. Val e As System. Event. Args) Handles cmd. Bye. Click lbl. Message. Text = "Bye pressed" End Sub ' cmd. Bye_Click Event handling code displays the appropriate Group. Box. Panel. Exa text property for lbl. Message mple. vb ' far left button handler Private Sub cmd. Left_Click(By. Val sender As System. Object, _ By. Val e As System. Event. Args) Handles cmd. Left. Click lbl. Message. Text = "Far left pressed" End Sub ' cmd. Left_Click 2002 Prentice Hall. All rights reserved.

30 31 32 33 34 35 36 37 ' far right button handler Private

30 31 32 33 34 35 36 37 ' far right button handler Private Sub cmd. Right_Click(By. Val sender As System. Object, _ By. Val e As System. Event. Args) Handles cmd. Right. Click Outline 33 lbl. Message. Text = "Far right pressed" End Sub ' cmd. Right_Click End Class ' Frm. Group. Box. Panel. Exa mple. vb 2002 Prentice Hall. All rights reserved.

34 12. 7 Check. Boxes and Radio. Buttons • State Buttons – Check. Boxes

34 12. 7 Check. Boxes and Radio. Buttons • State Buttons – Check. Boxes • Any number can be checked at a time – Radio. Buttons • Usually organized in groups and only one checked at a time 2002 Prentice Hall. All rights reserved.

35 12. 7 Check. Boxes and Radio. Buttons Fig. 12. 23 Check. Box properties

35 12. 7 Check. Boxes and Radio. Buttons Fig. 12. 23 Check. Box properties and events. 2002 Prentice Hall. All rights reserved.

1 2 3 4 5 6 7 8 9 10 11 12 13 14

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 ' Fig. 12. 24: Check. Box. Test. vb ' Using Check. Boxes to toggle italic and bold styles. Outline 36 Public Class Frm. Check. Box Inherits System. Windows. Form ' Visual Studio. NET IDE generated code ' use Xor to toggle italic, keep other styles same Private Sub chk. Italic_Checked. Changed _ (By. Val sender As System. Object, By. Val e As System. Event. Args) _ Handles chk. Italic. Checked. Changed lbl. Output. Font = New Font(lbl. Output. Font. Name , _ lbl. Output. Font. Size, lbl. Output. Font. Style _ Xor Font. Style. Italic) End Sub ' chk. Italic_Checked. Changed ' use Xor to toggle bold, keep other styles same Private Sub chk. Bold_Checked. Changed _ (By. Val sender As System. Object, By. Val e As System. Event. Args) _ Handles chk. Bold. Checked. Changed Check. Box. Test. vb lbl. Output. Font = New Font(lbl. Output. Font. Name , _ lbl. Output. Font. Size, lbl. Output. Font. Style _ Xor Font. Style. Bold) End Sub ' chk. Bold_Checked. Changed End Class ' Frm. Check. Box 2002 Prentice Hall. All rights reserved.

Outline 37 Check. Box. Test. vb 2002 Prentice Hall. All rights reserved.

Outline 37 Check. Box. Test. vb 2002 Prentice Hall. All rights reserved.

38 12. 7 Check. Boxes and Radio. Buttons Fig. 12. 25 Radio. Button properties

38 12. 7 Check. Boxes and Radio. Buttons Fig. 12. 25 Radio. Button properties and events. 2002 Prentice Hall. All rights reserved.

1 2 3 4 5 6 7 8 9 10 11 12 13 14

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 ' Fig. 12. 26: Radio. Button. Test. vb ' Using Radio. Buttons to set message window options. Outline 39 Public Class Frm. Radio. Button Inherits System. Windows. Form Private icon. Type As Message. Box. Icon Private button. Type As Message. Box. Buttons ' Visual Studio. NET generated code ' display message box and obtain dialogue button clicked Private Sub cmd. Display_Click(By. Val sender _ As System. Object, By. Val e As System. Event. Args) _ Handles cmd. Display. Click Dim dialog As Dialog. Result = Message. Box. Show( _ "This is Your Custom Message. Box", "VB", button. Type, _ icon. Type) Radio. Button. Test. vb ' check for dialog result and display on label Select Case dialog Case Dialog. Result. OK lbl. Display. Text = "OK was pressed" Case Dialog. Result. Cancel lbl. Display. Text = "Cancel was pressed" Case Dialog. Result. Abort lbl. Display. Text = "Abort was pressed" Case Dialog. Result. Retry lbl. Display. Text = "Retry was pressed" 2002 Prentice Hall. All rights reserved.

36 37 38 39 40 41 42 43 44 45 46 47 48 49

36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 Case Dialog. Result. Ignore lbl. Display. Text = "Ignore was pressed" Outline 40 Case Dialog. Result. Yes lbl. Display. Text = "Yes was pressed" Case Dialog. Result. No lbl. Display. Text = "No was pressed" End Select End Sub ' cmd. Display_Click ' set button type to OK Private Sub rad. Ok_Checked. Changed(By. Val sender _ As System. Object, By. Val e As System. Event. Args) _ Handles rad. Ok. Checked. Changed button. Type = Message. Box. Buttons. OK End Sub ' rad. Ok_Checked. Changed Radio. Button. Test. vb ' set button type to Ok. Cancel Private Sub rad. Ok. Cancel_Checked. Changed( By. Val sender _ As System. Object, By. Val e As System. Event. Args) _ Handles rad. Ok. Cancel. Checked. Changed button. Type = Message. Box. Buttons. OKCancel End Sub ' rad. Ok. Cancel_Checked. Changed ' set button type to Abort. Retry. Ignore Private Sub rad. Abort. Retry. Ignore_Checked. Changed( By. Val sender _ As System. Object, By. Val e As System. Event. Args) _ Handles rad. Abort. Retry. Ignore. Checked. Changed button. Type = Message. Box. Buttons. Abort. Retry. Ignore End Sub ' rad. Abort. Retry. Ignore_Checked. Changed 2002 Prentice Hall. All rights reserved.

71 72 73 74 75 76 77 78 79 80 81 82 83 84

71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 ' set button type to Yes. No. Cancel Private Sub rad. Yes. No. Cancel_Checked. Changed( By. Val sender _ As System. Object, By. Val e As System. Event. Args) _ Handles rad. Yes. No. Cancel. Checked. Changed Outline 41 button. Type = Message. Box. Buttons. Yes. No. Cancel End Sub ' rad. Yes. No. Cancel_Checked. Changed ' set button type to Yes. No Private Sub rad. Yes. No_Checked. Changed( By. Val sender _ As System. Object, By. Val e As System. Event. Args) _ Handles rad. Yes. No. Checked. Changed button. Type = Message. Box. Buttons. Yes. No End Sub ' rad. Yes. No_Checked. Changed ' set button type to Retry. Cancel Private Sub rad. Retry. Cancel_Checked. Changed( By. Val sender _ As System. Object, By. Val e As System. Event. Args) _ Handles rad. Retry. Cancel. Checked. Changed Radio. Button. Test. vb button. Type = Message. Box. Buttons. Retry. Cancel End Sub ' rad. Retry. Cancel_Checked. Changed ' set icon type to Asterisk when Asterisk checked Private Sub rad. Asterisk_Checked. Changed( By. Val sender _ As System. Object, By. Val e As System. Event. Args) _ Handles rad. Asterisk. Checked. Changed icon. Type = Message. Box. Icon. Asterisk End Sub ' rad. Asterisk_Checked. Changed 2002 Prentice Hall. All rights reserved.

104 105 106 107 108 109 110 111 112 113 114 115 116 117

104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 ' set icon type to Error when Error checked Private Sub rad. Error_Checked. Changed( By. Val sender _ As System. Object, By. Val e As System. Event. Args) _ Handles rad. Error. Checked. Changed Outline 42 icon. Type = Message. Box. Icon. Error End Sub ' rad. Error_Checked. Changed ' set icon type to Exclamation when Exclamation checked Private Sub rad. Exclamation_Checked. Changed( By. Val sender _ As System. Object, By. Val e As System. Event. Args) _ Handles rad. Exclamation. Checked. Changed icon. Type = Message. Box. Icon. Exclamation End Sub ' rad. Exclamation_Checked. Changed ' set icon type to Hand when Hand checked Private Sub rad. Hand_Checked. Changed(By. Val sender _ As System. Object, By. Val e As System. Event. Args) _ Handles rad. Hand. Checked. Changed Radio. Button. Test. vb icon. Type = Message. Box. Icon. Hand End Sub ' rad. Hand_Checked. Changed ' set icon type to Information when Information checked Private Sub rad. Information_Checked. Changed( By. Val sender _ As System. Object, By. Val e As System. Event. Args) _ Handles rad. Information. Checked. Changed icon. Type = Message. Box. Icon. Information End Sub ' rad. Information_Checked. Changed 2002 Prentice Hall. All rights reserved.

136 137 138 139 140 141 142 143 144 145 146 147 148 149

136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 ' set icon type to Question when Question checked Private Sub rad. Question_Checked. Changed( By. Val sender _ As System. Object, By. Val e As System. Event. Args) _ Handles rad. Question. Checked. Changed Outline 43 icon. Type = Message. Box. Icon. Question End Sub ' rad. Question_Checked. Changed ' set icon type to Stop when Stop checked Private Sub rad. Stop_Checked. Changed(By. Val sender _ As System. Object, By. Val e As System. Event. Args) _ Handles rad. Stop. Checked. Changed icon. Type = Message. Box. Icon. Stop End Sub ' rad. Stop_Checked. Changed ' set icon type to Warning when Warning checked Private Sub rad. Warning_Checked. Changed( By. Val sender _ As System. Object, By. Val e As System. Event. Args) _ Handles rad. Warning. Checked. Changed Radio. Button. Test. vb icon. Type = Message. Box. Icon. Warning End Sub ' rad. Warning_Checked. Changed End Class ' Frm. Radio. Buttons 2002 Prentice Hall. All rights reserved.

Outline 44 Radio. Button. Test. vb 2002 Prentice Hall. All rights reserved.

Outline 44 Radio. Button. Test. vb 2002 Prentice Hall. All rights reserved.

Outline 45 Radio. Button. Test. vb 2002 Prentice Hall. All rights reserved.

Outline 45 Radio. Button. Test. vb 2002 Prentice Hall. All rights reserved.

Outline 46 Radio. Button. Test. vb 2002 Prentice Hall. All rights reserved.

Outline 46 Radio. Button. Test. vb 2002 Prentice Hall. All rights reserved.

Outline 47 Radio. Button. Test. vb 2002 Prentice Hall. All rights reserved.

Outline 47 Radio. Button. Test. vb 2002 Prentice Hall. All rights reserved.

48 12. 8 Picture. Boxes • Picture. Boxes – Display images • • Bitmap

48 12. 8 Picture. Boxes • Picture. Boxes – 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 Picture. Boxes Fig. 12. 30 Picture. Box properties and events. 2002

49 12. 8 Picture. Boxes Fig. 12. 30 Picture. Box properties and events. 2002 Prentice Hall. All rights reserved.

1 2 3 4 5 6 7 8 9 10 11 12 13 14

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 Outline ' Fig. 12. 31: Picture. Box. Test. vb ' Using a Picture. Box to display images. 50 Imports System. IO Public Class Frm. Picture. Box Inherits System. Windows. Form Private image. Number As Integer = -1 ' Visual Studio. NET generated code ' replace image in pic. Image Private Sub pic. Image_Click(By. Val sender As System. Object, _ By. Val e As System. Event. Args) Handles pic. Image. Click ' image. Number from 0 to 2 image. Number = (image. Number + 1) Mod 3 An image object is created from file and set as the Picture. Box. Test. v Picture. Box’s image property b ' create Image object from file, display in Picture. Box pic. Image = Image. From. File _ (Directory. Get. Current. Directory & "imagesimage" & _ image. Number & ". bmp") End Sub ' pic. Image_Click End Class ' Frm. Picture. Box 2002 Prentice Hall. All rights reserved.

Outline 2002 Prentice Hall. All rights reserved. 51

Outline 2002 Prentice Hall. All rights reserved. 51

52 12. 10 Keyboard-Event Handling • Key Events – Generated when keys are pressed

52 12. 10 Keyboard-Event Handling • Key Events – Generated when keys are pressed and released – Key. Press • Can return a Char for any ASCII character pressed – Key. Up and Key. Down • Test for special modifier keys • Use Key. Event. Args 2002 Prentice Hall. All rights reserved.

53 12. 10 Keyboard-Event Handling Fig. 12. 34 Keyboard events, delegates and event arguments.

53 12. 10 Keyboard-Event Handling Fig. 12. 34 Keyboard events, delegates and event arguments. 2002 Prentice Hall. All rights reserved.

1 2 3 4 5 6 7 8 9 10 11 12 13 14

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 Outline ' Fig. 12. 35: Key. Demo. vb ' Displaying information about a user-pressed key. Public Class Frm. Key. Demo Inherits System. Windows. Form ' Visual Studio. NET generated code ' event handler for key press Key. Press event handler Private Sub Frm. Key. Demo_Key. Press(By. Val sender As System. Object, _ By. Val e As System. windows. Forms. Key. Press. Event. Args ) _ accesses the Key. Char property of the Key. Press. Event. Args object Handles My. Base. Key. Press lbl. Character. Text = "Key pressed: " & e. Key. Char End Sub and displays the Key ' display modifier keys, key code, key data and key value Private Sub Frm. Key. Demo_Key. Down(By. Val sender As System. Object, _ By. Val e As System. Windows. Forms. Key. Event. Args ) _ Handles My. Base. Key. Down lbl. Information. Text = "" ' if key is Alt If e. Alt Then lbl. Information. Text &= "Alt: Yes" & vb. Cr. Lf Else lbl. Information. Text &= "Alt: No" & vb. Cr. Lf End If Key. Demo. vb Key. Down event handler displays information from its Key. Event. Args object 2002 Prentice Hall. All rights reserved. 54

31 32 33 34 35 36 37 38 39 40 41 42 43 44

31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 Outline ' if key is Shift If e. Shift Then lbl. Information. Text &= "Shift: Yes" & vb. Cr. Lf Else lbl. Information. Text &= "Shift: No" & vb. Cr. Lf. The End If Key. Code property returns a Keys enumeration, which must be converted to a String via method To. String ' if key is Ctrl If e. Control Then lbl. Information. Text &= "Ctrl: Yes" & vb. Cr. Lf & _ "Key. Code: " & e. Key. Code. To. String & vb. Cr. Lf & _ "Key. Data: " & e. Key. Data. To. String & vb. Cr. Lf & _ "Key. Value: " & e. Key. Value Else lbl. Information. Text &= "Ctrl: No" & vb. Cr. Lf & _ "Key. Code: " & e. Key. Code. To. String & vb. Cr. Lf & _ "Key. Data: " & e. Key. Data. To. String & vb. Cr. Lf & _ "Key. Value: " & e. Key. Value End If Key. Demo. vb End Sub ' Frm. Key. Demo_Key. Down ' clear labels when key is released Private Sub Frm. Key. Demo_Key. Up(By. Val sender As System. Object, _ By. Val e As System. windows. Forms. Key. Event. Args ) _ Handles My. Base. Key. Up Both labels lbl. Information. Text = "" lbl. Character. Text = "" End Sub ' Frm. Key. Demo_Key. Up are cleared when a key is released End Class ' Frm. Key. Demo 2002 Prentice Hall. All rights reserved. 55

Outline Key. Demo. vb 2002 Prentice Hall. All rights reserved. 56

Outline Key. Demo. vb 2002 Prentice Hall. All rights reserved. 56