Lect Note On Chapter 3 - Part I - Event-Driven Component
Lect Note On Chapter 3 - Part I - Event-Driven Component
Objectives
• To understand the design principles of graphical user interfaces
(GUI).
• To understand, use and create events.
• To understand the namespaces containing GUI components
and event-handling classes and interfaces.
• To be able to create graphical user interfaces.
• To be able to create and manipulate buttons, labels, lists,
textboxes and panels.
• To be able to use mouse and keyboard events
Introduction
• A graphical user interface (GUI) allows users to
interact with a program visually.
• GUI gives a distinctive look, feel, and
consistent set of intuitive user-interface
components.
– E.g. GUI – browsers (chrom, firefox), windows
explorer, etc.
• Contain – menu and Set of buttons.
• Menu bar contain menus, set of buttons (toolbars), Labels –
caption/ indicate purpose, Scrollbars, etc.
• The menus, buttons, textboxes, labels and scrollbars are part of
GUI.
• They form a user-friendly interface through which the user
interacts with the program.
• GUIs are built from GUI components (also called controls or
widgets - short for window gadgets).
• A GUI component is an object with which the user interacts via
the mouse or keyboard.
Common GUI components
Windows Form
• Windows Forms (also called WinForms) create GUIs for
programs. It is a graphical element that appears on the
desktop.
• Type of form - dialog, window or MDI window (multiple
document interface window)
• A Component is a class that implements the IComponent
interface
• IComponent - defines the behaviors that components must
implement.
• Components are with or without graphical part
• component with a graphical part - control such as a button or
label. These are visible.
• Component that lack graphical part – are not visible (see next
slide)
Windows Forms controls and components contained in the Visual Studio .NET
Toolbox.
Event-handling mode
Basic Event Handling
• In most cases, we do not have to create our own events.
• Instead, we can handle the events generated by .NET controls
such as buttons and text boxes.
• These controls already have delegates for every event they
can raise.
• The programmer creates the event handler and registers it
with the delegate—Visual Studio .NET helps with this task.
– Example of windows application that displays a message box when
clicked.
• First, create a new Windows application.
• To register and define an event handler, click the Events icon in the form’s Properties window.
• This window allows the programmer to access, modify and create event handlers for a control.
• The dropdown button indicates that multiple handlers can be registered for one event.
• A brief description of the event appears on the bottom of the window.
• To create the event handler.
– Double-click the Click event in the Properties window
– This will create an empty event handler in the program code.
private void FormName_Click( object sender, System.EventArgs e )
{ //empty event handler generated, add some code to take action
}
• FormName_Click is a method called when the form is
clicked.
• For e.g we want a form display a message box.
– MessageBox.Show( "Form is pressed." );
• To do this, insert the statement in event handler to get
private void FormName_Click( object sender, System.EventArgs e )
{
MessageBox.Show( "Form is pressed" );
}
Description of an event handler
}
Using Resources Programmatically
• In this example, we added the images to the project as resources. This causes the IDE to to
copy the images into the app’s executable file and enables the app to access the images
through the project’s Properties namespace. When you do this, you don’t need to worry
about wrapping the images with the app when you move it to another location or computer.
• If you’re creating a new project, use the following steps to add images to the project as
resources:
• 1. After creating your project, right click the project’s Properties node in the Solution
Explorer and select Open to display the project’s properties.
• 2. From the tabs on the left, click the Resources tab.
• 3. At the top of the Resources tab, click the down arrow next to Add Resource and select Add
Existing File… to display the Add existing file to resources dialog.
• 4. Locate the image files you wish to add as resources and click the Open button. We
provided three sample images in the Images folder with this chapter’s examples.
• 5. Save your project.
• The files now appear in a folder named Resources in the Solution Explorer. We’ll use this
technique in most examples that use images going forward.
• A project’s resources are accessible to the app via its Resources class (of the project’s
Properties namespace). The Resources class contains a ResourceManager object for
interacting with the resources programmatically. To access an image, you can use the method
GetObject, which takes as an argument the resource name as it appears in the Resources tab
(e.g., "image0") and returns the resource as an Object.