Docsity

Event-Handling Model and Control Properties in Windows Forms Programming, Slides of Programming for Engineers

Partial preview of the text

Download Event-Handling Model and Control Properties in Windows Forms Programming and more Slides Programming for Engineers in PDF only on Docsity!

Graphical User Interface-I

Introduction

  • Graphical user interface
    • Allow interaction with program visually
    • Give program distinct look and feel
    • Built from window gadgets
    • Is an object, accessed via keyboard or mouse

Windows Forms

  • Component
    • Class that implements IComponent interface
    • Lacks visual parts
  • Control
    • Component with graphical part
      • Such as button or label
    • Are visible
  • Event
    • Generated by movement from mouse or keyboard
    • Event handlers performs action
      • Specifics written by programmer

Windows Forms

Components and controls for Windows Forms.

Event-Handling Model

  • GUIs are event driven
  • Event handlers
    • Methods that process events and perform tasks
  • Associated delegate
    • Objects that reference methods
    • Contain lists of method references
      • Must have same signature
    • Intermediaries for objects and methods
    • Signature for control’s event handler

Event-Handling Model

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

Event-handling model using delegates.

Basic Event Handling

Current even

handler (none)

Selected event

Event

description

List of events

supported by

control

Events icon

Events section of the Properties window.

1 // Fig. 12.7: Form1.h 2 // Creating an event handler. 3 4 #pragma once 5 6 7 namespace SimpleEventTest 8 { 9 using namespace System; 10 using namespace System::ComponentModel; 11 using namespace System::Collections; 12 using namespace System::Windows::Forms; 13 using namespace System::Data; 14 using namespace System::Drawing; 15 16 ///

17 /// Summary for Form 18 /// 19 /// WARNING: If you change the name of this class, you will need to 20 /// change the ’Resource File Name’ property for the managed 21 /// resource compiler tool associated with all .resx files 22 /// this class depends on. Otherwise, the designers will not 23 /// be able to interact properly with localized resources 24 /// associated with this form. 25 ///

52 // display a MessageBox when the user clicks the form 53 private: System::Void Form1_Click(System::Object * sender, 54 System::EventArgs * e) 55 { 56 MessageBox::Show( S"Form was pressed" ); 57 } 58 59 }; 60 }

1 // Fig. 12.8: Form1.cpp 2 // Demonstrating an event handler. 3 4 #include "stdafx.h" 5 #include "Form1.h" 6 #include <windows.h> 7 8 using namespace SimpleEventTest; 9 10 int APIENTRY _tWinMain(HINSTANCE hInstance, 11 HINSTANCE hPrevInstance, 12 LPTSTR lpCmdLine, 13 int nCmdShow) 14 { 15 System::Threading::Thread::CurrentThread->ApartmentState = 16 System::Threading::ApartmentState::STA; 17 Application::Run(new Form1()); 18 return 0 ; 19 }

Control Properties and Layout

  • Visibility control
    • Hide control from user
      • Or use method Hide
  • Anchor property
    • Anchoring control to specific location
      • Constant distance from specified location
    • Unanchored control moves relative to the position
    • Docking allows control to spread itself along and entire side
    • Both options refer to the parent container
  • Size structure
    • Allow for specifying size range
      • MinimumSize and MaximumSize property

Control Properties and Layout

Class Control

Properties and

Methods

Description

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-out”—

portions of the control will become gray.

Focused Whether a control has focus.

Font Font used to display control’s Text.

ForeColor Foreground color of the control. This is usually the color used to

display the control’s 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 if the TabStop property is true.

TabStop If true (the default value), 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 (equivalent to setting Visible to false).

Show Shows the control (equivalent to setting Visible to true).

Control class properties and methods.

Control Properties and Layout

Click down-arrow

in Anchor property

to display

anchoring window

Darkened bar indicates

to which wall control

is anchored

Manipulating the Anchor property of a control.

Control Properties and Layout

Control expands along

top portion of the form

Docking demonstration.