Download as pdf or txt
Download as pdf or txt
You are on page 1of 23

CS 112 Introduction to

Programming

Lecture #25:

Graphical User Interface; Events

http://flint.cs.yale.edu/cs112/

Typical C# Window Applications


r Program specifications (optional)
r Library imports (optional)
using System;
using System.Drawing;
using System.Windows.Forms;
r Class and namespace definitions

public class HelloWorld : System.Windows.Forms.Form


{
public HelloWorld() {
Size = new Size(300, 100);
Text = HelloWorld;
}

protected override void OnPaint(PaintEventArgs e) {


Graphics g = e.Graphics;
g.DrawString(Hello World , Font, Brushes.Blue, 30, 30);
}

static void Main(string[] args)


{
Application.Run(new HelloWorld());
}
} 2

1
Challenges for Window Applications

r What are the inputs?


m Many more possible input sources (e.g., mouse
click, keyboard press, repainting a window)
m Some comes directly from user actions, some are
indirect and implicit
r How to react to these inputing events?
m Need to write what we call event handlers

r What are the outputs?


m Display different GUI widgets with proper layout
m Graphics programming
m Animation

GUI Programming: Basic Concepts

r A GUI component is a class that implements the


IComponent interface
m a control, such as a button or label, is a component with a
graphical part

r Some components, which we call containers, can


contain other components
m some examples are Form, Panel, GroupBox

r Components can generate events to which event


handler can respond to

2
GUI Components/Controls
r Components and Controls are organized into
an inheritance class hierarchy so that they can
easily share characteristics
r Each component/control defines some
m properties
m methods
m events

r The easiest way to add components and


controls to a program is to use VS .NET
ToolBox

Components and Controls


for Windows Forms

3
Example: Windows.Forms.Form
Form Properties, Description / Delegate and Event Arguments
Methods and Events
Common Properties
AcceptButton Which button will be clicked when Enter is pressed.
AutoScroll Whether scrollbars appear when needed (if data fills more than one
screen).
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, as well as the default font of
controls added to the form.
Text Text in the forms title bar.
Common Methods
Close Closes 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.
Paint Each time it is redrawn

Click When a user clicks on the form

Event, Event Handler and Delegate

r Events, e.g.,
m a window becomes visible
m a graphical button is clicked
m a keyboard key is pressed
m the mouse is moved
m a mouse button is clicked
m the mouse is dragged
m a timer expires

r Event handler
m a method that processes an event and performs tasks

r In a control, there is one delegate for each event it can


generate
m a delegate of an event is an object which references (a list of )
methods, i.e., event handlers
8

4
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 delegate.

Window Application Revisited


using System;
using System.Drawing;
using System.Windows.Forms;

public class HelloWorld : System.Windows.Forms.Form


{
public HelloWorld() {
Text = SimpleEventExample;
}

protected override void OnPaint(PaintEventArgs e) {


Graphics g = e.Graphics;
g.DrawString(Hello World , Font, Brushes.Blue, 30, 30);
}

static void Main(string[] args) {


Application.Run(new HelloWorld());
}
}

We can replace OnPaint method with an event handler for the Paint event.

10

5
OnPaint via Event Handling
using System;
using System.Drawing;
using System.Windows.Forms;

public class HelloWorld : System.Windows.Forms.Form


{
public HelloWorld() {
Text = SimpleEventExample;
this.Paint += new PaintEventHandler(Form1_Paint);
}

private void Form1_Paint(object sender, PaintEventArgs e) {


Graphics g = e.Graphics;
g.DrawString("hello, world", Font, Brushes.Aqua, 30, 30);
}

// protected override void OnPaint(PaintEventArgs e) {


// Graphics g = e.Graphics;
// g.DrawString(Hello World , Font, Brushes.Blue, 30, 30);
// }

static void Main(string[] args) {


Application.Run(new HelloWorld());
}
}
11

Window Application w. Event Handling


using System;
using System.Drawing;
using System.Windows.Forms;

public class HelloWorld : System.Windows.Forms.Form


{
public HelloWorld() {
Text = SimpleEventExample;
this.Click += new System.EventHandler(MyForm_Click);
}

// protected override void OnPaint(PaintEventArgs e) {


// Graphics g = e.Graphics;
// g.DrawString(Hello World , Font, Brushes.Blue, 30, 30);
// }

private void MyForm_Click(object sender, System.EventArgs e) {


MessageBox.Show( "Form was pressed" );
}

static void Main(string[] args) {


Application.Run(new HelloWorld());
}
}

12

6
Outline

SimpleEventExamp
le.cs
Program Output

2001 Prentice Hall, Inc.


All rights reserved.

Basic Event Handling


r Event handler
m Must conform to a given signature
m Must be registered with the delegate object of the
event of the control
add event handlers to the delegates invocation list

r Event multicasting
m Can have multiple handlers for one event
m Order called for event handlers is indeterminate

14

7
Basic Event Handling

Events icon
List of events
supported by
control

Selected event
Current even
handler (none)

Event
description

Fig. 12.6 Events section of the Properties window.

15

Some Common Control Properties

r Text property
m Specifies the text that appears on a control

r TabIndex property
m Order in which controls are given focus
m Automatically set by Visual Studio .NET

r Enable property
m Indicate a controls accessibility

r Visibility control
m Hide control from user
Or use method Hide
16

8
Summary: Some Common Control Properties

Class Control Description


Properties and Methods
Common Properties

BackColor Background color of the control.


BackgroundImage Background image of the control.
Enabled Whether the control is enabled (i.e., if the user can interact with it). A
disabled control will still be displayed, but grayed-outportions of
the control will become gray.
Focused Whether a control has focus. (The control that is currently being used
in some way.)
Font Font used to display controls Text.
ForeColor Foreground color of the control. This is usually the color used to
display the controls Text property.
TabIndex Tab order of the control. When the Tab key is pressed, the focus is
moved to controls in increasing tab order. This order can be set by the
programmer.
TabStop If true, user can use the Tab key to select the control.
Text Text associated with the control. The location and appearance varies
with the type of control.
TextAlign The alignment of the text on the control. One of three horizontal
positions (left, center or right) and one of three vertical positions (top,
middle or bottom).
Visible 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.
17

Labels
r Provide text instruction
r Defined with class Label
m derived from class Control

Label D e sc rip tio n / Dele g a t e a n d Ev e n t A rg u m e n t s


Pro p e rtie s
Common
Properties
Font The font used by the text on the Label.
Text The text to appear on the Label.
TextAlign The alignment of the Labels text on the control. One of three horizontal positions
(left, center or right) and one of three vertical positions (top, middle or
bottom).
Fig. 12.15 Label p ro p e rtie s.

18

9
TextBoxes
r Provide an area for text input
m You can specify that a textbox is a password
textbox
TextBox Pro p e rtie s D e sc rip tio n / D e le g a t e a n d Ev e n t A rg u m e n t s
a n d Ev e n t s
Common Properties

AcceptsReturn If t r u e , pressing Enter creates a new line if textbox spans multiple


lines. If f a l s e , pressing Enter clicks the default button of the form.
Multilin e If t r u e , textbox can span multiple lines. Default is f a l s e .
PasswordChar Single character to display instead of typed text, making the
T e x t B o x a password box. If no character is specified, T e x t b o x
displays the typed text.
ReadOnly If t r u e , T e x t B o x h a s a gray background and its text cannot be
edited. Default is f a l s e .
ScrollBars For multiline textboxes, indicates which scrollbars appear (n o n e ,
h o r i z o n t a l , v e r t i c a l or b o t h ).
Text The text to be displayed in the text box.
Common Events (Delegate E v e n t H a n d l e r , event arguments E v e n t A r g s )
TextChanged Raised when text changes in T e x t B o x (the user added or deleted
characters). Default event when this control is double clicked in the
designer.
Fig . 12.16 T e x t B o x p ro p e rtie s a n d e v e n t s.
19

Buttons
r Control to trigger a specific action
r Derived from ButtonBase

Button properties and Description / Delegate and Event Arguments


events
Common Properties
Text Text displayed on the Button face.
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.17 Button properties and events.

20

10
1 // Fig. 12.18: LabelTextBoxButtonTest.cs Outline
2 // Using a Textbox, Label and Button to display
3 // the hidden text in a password box.
4
5 using System;
LabelTextBoxButt
6 using System.Drawing; onTest.cs
7 using System.Collections;
8 using System.ComponentModel;
9 using System.Windows.Forms;
10 using System.Data;
11
12 // namespace contains our form to display hidden text
13 namespace LabelTextBoxButtonTest Visual Studio .NET adds
14 { comments to our code
15 /// <summary>
16 /// form that creates a password textbox and
17 /// a label to display textbox contents Visual Studio .NET inserts declarations
18 /// </summary>
19 public class LabelTextBoxButtonTest : for the control we added to the form ;The
20 System.Windows.Forms.Form IDE manages these declaration
21 {
22 private System.Windows.Forms.Button displayPasswordButton;
23 private System.Windows.Forms.Label displayPasswordLabel;
24 private System.Windows.Forms.TextBox inputPasswordTextBox;
25
26 /// <summary> Declare reference components (an array)
27 /// Required designer variable.
28 /// </summary>
29 private System.ComponentModel.Container components = null;
30
31 // default contructor Method InitializeComponent creates
32 public LabelTextBoxButtonTest()
33 { components and controls in the form and
34 InitializeComponent(); sets their properties 2001 Prentice Hall, Inc.
35 }
All rights reserved.

36 Outline
37 /// <summary>
38 /// Clean up any resources being used.
39 /// </summary>
40 protected override void Dispose( bool disposing )
LabelTextBoxButt
41 { onTest.cs
42 if ( disposing )
43 {
44 if ( components != null )
Method Dispose cleans up allocated
45 { resources
46 components.Dispose();
47 }
48 }
49 #region preprocessor directives allow
50 base.Dispose( disposing );
51 } collapsible code in Visual Studio .NET
52
53 #region Windows Form Designer generated code
54 /// <summary>
55 /// Required method for Designer support - do not modify
56 /// the contents of this method with the code editor.
57 /// </summary>
58 private void InitializeComponent()
59 {
60 this.displayPasswordButton =
61 new System.Windows.Forms.Button();
Create new objects for the
62 this.inputPasswordTextBox = control we added
63 new System.Windows.Forms.TextBox();
64 this.displayPasswordLabel =
65 new System.Windows.Forms.Label();
66 this.SuspendLayout();
67

2001 Prentice Hall, Inc.


All rights reserved.

11
68 // Outline
69 // displayPasswordButton
70 //
71 this.displayPasswordButton.Location =
72 new System.Drawing.Point( 96, 96 );
LabelTextBoxButt
73 this.displayPasswordButton.Name = onTest.cs
74 "displayPasswordButton";
75 this.displayPasswordButton.TabIndex = 1;
76 this.displayPasswordButton.Text = "Show Me";
77 this.displayPasswordButton.Click += Visual Studio .NET register
78 new System.EventHandler(
79 this.displayPasswordButton_Click );
the event handler for us
80
81 //
82 // inputPasswordTextBox
83 //
84 this.inputPasswordTextBox.Location =
85 new System.Drawing.Point( 16, 16 ); Set the Name, PasswordChar
86 this.inputPasswordTextBox.Name = and Text properties for
87 "inputPasswordTextBox";
88 this.inputPasswordTextBox.PasswordChar = '*'; inputPasswordTextBox
89 this.inputPasswordTextBox.Size =
90 new System.Drawing.Size( 264, 20 );
91 this.inputPasswordTextBox.TabIndex = 0;
92 this.inputPasswordTextBox.Text = "";
93
94 //
95 // displayPasswordLabel
96 //
97 this.displayPasswordLabel.BorderStyle =
98 System.Windows.Forms.BorderStyle.Fixed3D;
99 this.displayPasswordLabel.Location =
100 new System.Drawing.Point( 16, 48 );
101 this.displayPasswordLabel.Name =
102 "displayPasswordLabel"; 2001 Prentice Hall, Inc.
All rights reserved.

103 this.displayPasswordLabel.Size = Outline


104 new System.Drawing.Size( 264, 23 );
105 this.displayPasswordLabel.TabIndex = 2;
106
107 //
LabelTextBoxButt
108 // LabelTextBoxButtonTest onTest.cs
109 //
110 this.AutoScaleBaseSize =
111 new System.Drawing.Size( 5, 13 );
112 this.ClientSize =
113 new System.Drawing.Size( 292, 133 );
114 this.Controls.AddRange(
115 new System.Windows.Forms.Control[] {
116 this.displayPasswordLabel,
117 this.inputPasswordTextBox,
118 this.displayPasswordButton});
119 this.Name = "LabelTextBoxButtonTest";
120 this.Text = "LabelTextBoxButtonTest";
121 this.ResumeLayout( false );
122
123 } // end method InitializeComponent
124
125 // end collapsible region started on line 53
126 #endregion
#endregion signal the end
127 of the collapsible region
128 /// <summary>
129 /// The main entry point for the application.
130 /// </summary>
131 [STAThread]
132 static void Main()
133 {
134 Application.Run( new LabelTextBoxButtonTest() );
135 }
136
2001 Prentice Hall, Inc.
All rights reserved.

12
137 // display user input on label Outline
138 protected void displayPasswordButton_Click(
139 object sender, System.EventArgs e )
To show the text, set
140 {
141 // text has not changed
LabelTextBoxButt
displayPasswordLabels Text to
142 displayPasswordLabel.Text = onTest.cs
inputPasswordTextBoxs Text
143 inputPasswordTextBox.Text;
144 }
145
146 } // end class LabelTextBoxButtonTest
147
148 } // end namespace LabelTextBoxButtonTest

Program Output

2001 Prentice Hall, Inc.


All rights reserved.

PictureBox
r Displays an image
r Read image from file:
Image.FromFile() method (need to add
System.IO name space)
PictureBox Desc rip tio n / Delegate and Event Arguments
p roperties and events
Common Properties

Image Image to display in the PictureBox.


SizeMode Enumeration that controls image sizing and positioning. Values
Normal (default), StretchImage, AutoSize and
CenterImage. Normal puts image in top-left corner of
PictureBox and CenterImage puts image in middle (both cut
off image if 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.27 PictureBox p roperties and events.
26

13
1 // Fig. 12.28: PictureBoxTest.cs Outline
2 // Using a PictureBox to display images.
3
4 using System;
5 using System.Drawing;
PictureBoxTest.c
6 using System.Collections; s
7 using System.ComponentModel;
8 using System.Windows.Forms;
9 using System.Data;
10 using System.IO;
11
PictureBox imagePicture
12 /// form to display different images when clicked use to display
13 public class PictureBoxTest : System.Windows.Forms.Form one of three bitmap images
14 {
15 private System.Windows.Forms.PictureBox imagePictureBox;
16 private System.Windows.Forms.Label promptLabel;
17
18 private int imageNum = -1;
19
20 /// The main entry point for the application. Includes instructions Click
21 [STAThread]
22 static void Main() Store the image we want to display
On Picture Box to View
23 { Images
24 Application.Run( new PictureBoxTest() );
25 }
26
27 // change image whenever PictureBox clicked To respond to the Click event
28 private void imagePictureBox_Click(
29 object sender, System.EventArgs e )
30 {
31 imageNum = ( imageNum + 1 ) % 3; // imageNum from 0 to 2
32

Modulus calculation insures that


2001 Prentice Hall, Inc.
number is between 0 and 2 All rights reserved.

33 // create Image object from file, display on PictureBox Outline


34 imagePictureBox.Image = Image.FromFile(
35 Directory.GetCurrentDirectory() + "\\images\\image" +
36 imageNum + ".bmp" );
37 }
PictureBoxTest.c
38 s
39 Set//the
} Image
end classproperty of
PictureBoxTest Method FromFile which
imagePictureBox to an Image takes a string and creates
an Image object
Use imageNum to append
the correct number Method GetCurrentDirectory of Class Directory
returns current directory of file as a string

Program Output

2001 Prentice Hall, Inc.


All rights reserved.

14
Layout Management
r Size structure
m Allow for specifying size range
MinimumSize and MaximumSize property
r Location structure
m Specifies upper-left corner of the control, relative to container
r Anchor and dock
m Anchor
Anchored control stays at a specific location (relative to parent)
constant distance from specified location
unanchored control moves relative to the position
m Dock
allows control to spread itself along and entire side

29

The Effect of Anchoring

Before resize After resize

Constant distance
to left and top sides

Fig. 12.11 Anchoring demonstration.

30

15
Set Anchor

Darkened bar indicates


to which wall control
is anchored

Click down-arrow
in Anchor property
to display
anchoring window

Fig. 12.12 Manipulating the Anchor property of a control.

31

Control Layout: Dock

Control expands along


top portion of the form

Fig. 12.13 Docking demonstration.

32

16
Control Layout Properties
Common Layout Desc ription
Properties
Common Properties
Anchor Side of parent container at which to anchor controlvalues can be
combined, such as Top, Left.
Dock Side of parent container to dock controlvalues cannot be combined.
DockPadding (for Sets the dock spacing for controls inside the container. Default is zero,
containers) so controls appear flush against the side of the container.
Location Location of the upper-left corner of the control, relative to its
container.
Size Size of the control. Takes a Size structure, which has properties
Height and Width.
MinimumSize, The minimum and maximum size of the form.
MaximumSize (for
Windows Forms)
Fig. 12.14 Class Control la yout properties.

33

GroupBoxes
r A GroupBox can display a caption
m Text property determines its caption

GroupBoxProperties Description
Common Properties

Controls The controls that the GroupBox contains.


Text Text displayed on the top portion of the GroupBox (its caption).
Fig. 12.19 GroupBox properties.

34

17
Panels
r A Panel can have a scrollbar
m View additional controls inside the Panel

Panel Properties Description


Common Properties

AutoScroll Whether scrollbars appear when the Panel is too small to hold its
controls. Default is false.
BorderStyle Border of the Panel (default None; other options are Fixed3D and
FixedSingle).
Controls The controls that the Panel contains.
Fig. 12.20 Panel properties.

35

GroupBoxes and Panels

panel

Controls inside panel

panel
scrollbars

Fig. 12.21 Creating a Panel with scrollbars.


36

18
1 // Fig. 12.22: GroupBoxPanelExample.cs Outline
2 // Using GroupBoxes and Panels to hold buttons.
3
4 using System;
5 using System.Drawing;
GroupBoxPanelExa
6 using System.Collections; mple.cs
7 using System.ComponentModel;
8 using System.Windows.Forms;
9 using System.Data;
10
11 /// form to display a groupbox versus a panel
12 public class GroupBoxPanelExample : System.Windows.Forms.Form
13 {
14 private System.Windows.Forms.Button hiButton;
15 private System.Windows.Forms.Button byeButton; GroupBox (name mainGroupBox)
16 private System.Windows.Forms.Button leftButton;
17 private System.Windows.Forms.Button rightButton; Panel (name mainPanel)
18
19 private System.Windows.Forms.GroupBox mainGroupBox; Control AutoScroll
20 private System.Windows.Forms.Label messageLabel;
21 private System.Windows.Forms.Panel mainPanel; property set to TRUE
22
23 private System.ComponentModel.Container components = null;
24
25 // Visual Studio .NET-generated Dispose method
26
27 [STAThread]
28 static void Main()
29 {
30 Application.Run( new GroupBoxPanelExample() );
31 }
32

2001 Prentice Hall, Inc.


All rights reserved.

33 // event handlers to change messageLabel Outline


34
35 // event handler for hi button
36 private void hiButton_Click(
37 object sender, System.EventArgs e )
GroupBoxPanelExa
38 { mple.cs
39 messageLabel.Text= "Hi pressed"; hiButton and byeButton
40 }
41
belong to GroupBox
42 // event handler for bye button
43 private void byeButton_Click(
44 object sender, System.EventArgs e )
45 {
46 messageLabel.Text = "Bye pressed";
Represent event handlers
47 }
48
49 // event handler for far left button Line messageLabel added to
50 private void leftButton_Click(
51 object sender, System.EventArgs e )
customize the text
52 {
53 messageLabel.Text = "Far left pressed"; Panel has two buttons,
54 } leftButton and rightButton
55
56 // event handler for far right button
57 private void rightButton_Click(
58 object sender, System.EventArgs e )
59 {
60 messageLabel.Text = "Far right pressed";
61 }
62
63 } // end class GroupBoxPanelExample

2001 Prentice Hall, Inc.


All rights reserved.

19
Outline

GroupBoxPanelExa
hiButton_Click mple.cs
Program Output

leftButton_Click rightButton_Click

2001 Prentice Hall, Inc.


All rights reserved.

Keyboard Event Handling

r Two types of key events


m Delegate KeyEventHandler
Handles two sub types of events: KeyUp or KeyDown

m Delegate KeyPressEventHandler
Handles one type of event: KeyPress

40

20
KeyEventHandler
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.
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 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 as a Keys enumeration,
combined with modifier information. Used to
determine all information about the key pressed.
KeyValue Returns the key code as an int, rather than as a Keys
enumeration. Used to obtain a numeric representation
of the key pressed.
Modifiers Returns a Keys enumeration for any modifier keys
pressed (Alt, Control and Shift). Used to determine
modifier key information only.
41

KeyPressEventHandler

Keyboard Events, Delegates and


Event Arguments
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 Whether or not the KeyPress event was handled.

See KeyDemo
42

21
1 // Fig. 12.32: KeyDemo.cs Outline
2 // Displaying information about the key the user pressed.
3
4 using System;
5 using System.Drawing;
KeyDemo.cs
6 using System.Collections;
7 using System.ComponentModel;
8 using System.Windows.Forms;
9 using System.Data;
10
11 // form to display key press
12 // information--contains two labels Label for key pressed
13 public class KeyDemo : System.Windows.Forms.Form
14 { Forms contain two Labels
15 private System.Windows.Forms.Label charLabel;
16 private System.Windows.Forms.Label keyInfoLabel; Label for modifier information
17
18 private System.ComponentModel.Container components = null;
19
20 /// The main entry point for the application.
21 [STAThread]
22 static void Main()
23 { KeyPress event handler
24 Application.Run( new KeyDemo() );
25 } Accesses the KeyChar property
26
27 // display the character pressed using key char
of the KeyPressEventArgs object
28 protected void KeyDemo_KeyPress(
29 object sender, System.Windows.Forms.KeyPressEventArgs e )
30 {
31 charLabel.Text = "Key pressed: " + e.KeyChar;
Key pressed as a char
32 }
33
Display the key pressed
2001 Prentice Hall, Inc.
All rights reserved.

34 // display modifier keys, key code, key data and key value Outline
35 private void KeyDemo_KeyDown(
36 object sender, System.Windows.Forms.KeyEventArgs e )
37 {
38 keyInfoLabel.Text =
KeyDemo.cs
39 "Alt: " + ( e.Alt ? "Yes" : "No") + '\n' +
40 "Shift: " + ( e.Shift ? "Yes" : "No" ) + '\n' + KeyEventArgs object
41 "Ctrl: " + ( e.Control ? "Yes" : "No" ) + '\n' +
42 "KeyCode: " + e.KeyCode + '\n' + KeyCode returns the key pressed
43 "KeyData: " + e.KeyData + '\n' + without modifier keys
44 "KeyValue: " + e.KeyValue;
45 }
information
46
47 // clear labels when key released
KeyData property returns the keys pressed
48 private void KeyDemo_KeyUp( with modifier keys
49 object sender, System.Windows.Forms.KeyEventArgs e )
50 {
51 keyInfoLabel.Text = "";
52 charLabel.Text = ""; KeyValue returns the key code as an
53 }
integer; the integer value is the Windows
virtual key code

KeyUp event handler clears both labels

2001 Prentice Hall, Inc.


All rights reserved.

22
Backup Slides

GUI
r Introduction: Components and controls; Event handling model
r Controls
m Common properties
Layout management
m Basic controls
Label, textbox, checkbox, radiobutton, picturebox, LinkLabels, ListBox,
CheckedListBox
m Containers
Groupbox, panel: for layout
Tab controls
Multiple-document interface windows
m Event handling: mouse, keyboard
m More controls
Menus
ComboBoxes
TreeView control
r Graphics
m Color, font
m Lines, arcs, polygons
m Brush specification
r Multimedia
46

23

You might also like