G RAPHICAL U SER I NTERFACE C ONCEPTS : P ART 1 1 Outline Introduction Windows Forms Event-Handling...

Preview:

Citation preview

GRAPHICAL USER INTERFACE CONCEPTS: PART 1

1

OutlineIntroductionWindows FormsEvent-Handling Model

- Basic Event Handling

INTRODUCTION

Graphical user interface Is an object that allows user to interact with

program visually using keyboard or mouse Built from window gadgets aka widgets (controls) Give program distinct look and feel

Consistency enables users to learn new apps faster

2

INTRODUCTION

3

Sample Internet Explorer window with GUI components.

Button Label Menu Bar TextBox Scrollbar

INTRODUCTION

4

Control Label

TextBox

Button

CheckBox

ComboBox

ListBox

Panel

ScrollBar

What are these?

WINDOWS FORMS

Forms Create GUIs for programs Graphical element on the desktop Represented by:

Dialog Window Multiple document interface (MDI) window

Acts as a container for components and controls

5

WINDOWS FORMS Control

Visible component with graphical part Such as button or label

Component Class that implements IComponent interface

(defines behaviors components must implement) Lacks visual parts

Such as Timer or DirectorySearcher

Event Generated by movement from mouse or keyboard Event handlers performs action

Specifics written by programmer

6

DESIGN PROCESS

For creating Windows Applications Create a windows form Set its properties Add controls Set their properties Implement event handlers

7

WINDOWS FORMS

8

Form Properties and Events

Description / Delegate and Event Arguments

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 form’s 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.

Common Form properties and events.

EVENT-HANDLING MODEL GUIs are event driven

User moves or clicks mouse, clicks a button, types in a textbox, selects from a menu or listbox, closes a window

Event handlers Methods that process events and perform tasks.

Each control that can generate events has an associated delegate Object that reference methods Defines the signature for control’s event handler Event delegates are multicast

Contain lists of method references (every one is called) Each must have same signature

9

EVENT-HANDLING MODEL

10Event-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

Intermediary for object and methods

BASIC EVENT HANDLING

11

Events section of the Properties window. DEMO!

Current event handler

Selected event

Event description

List of events supported by control

Events icon

12

//SimpleEventExample.csusing System;using System.Windows.Forms;

namespace SimpleEventExample2{ //program that shows a simple event handler

public partial class MyForm : Form { public MyForm() { InitializeComponent(); }

// Visual Studio .NET creates an empty handler, // we write definition: show message box when form clicked private void MyForm_Click( object sender, System.EventArgs e ) { MessageBox.Show( "Form was clicked" ); } } //end class MyForm}

Create an event handler

Signature of the event handlerReference to the object that raised the event (sender)

Class EventArgs is base class for objects with event information

Reference to an event arguments object (e)

13

PROGRAM OUTPUT

BASIC EVENT HANDLING Event handler

ControlName_EventName MyForm_Click Two object references are passed in (sender and e) Must have same signature as corresponding delegate

Clicking name of event brings up its delegate Must be registered with delegate object

Add event handlers to the delegate’s invocation list Visual Studio.NET does this for us

Controls have a delegate reference for each event New delegate object for each event handler

this.Click += new System.EventHandler

(this.MyForm_Click); 14

BASIC EVENT HANDLING

Event multicasting Have multiple handlers for one event Order called for event handlers is indeterminate

15

16

List of Form events.

Class name

List of events

web page link

17

Details of Click event.

Event delegateEvent argument class

Event name

Recommended