Introduction To Windows Forms Applications: E E U F F

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

E

U
F

INTRODUCTION TO
WINDOWS FORMS APPLICATIONS

Faculty of IT - UEF
E
U
F Table of contents

1. GUI

2. Event Driven Programming

3. Windows Forms

4. Demo

2
E
U
F GUI

Command line interface: CLI Text user interface: TUI

Input from keyboard GUI base on Text mode


Execute sequentially More interactive

3
E
U
F GUI

Graphical User Interface: GUI

Interact through a
high resolution
graphical interface

Most modern OSs


use GUI

allows users to easily


manipulate

4
E
U
F Building GUI

 GUI's are built from GUI controls

 Also known as components or widgets

 They are objects that can:


 Display information on the screen, or

 Enable users to interact with an application via the mouse,


keyboard or other form of input

 Examples - commonly used types of GUI controls


 Label, TextBox, Button, CheckBox, ComboBox, ListBox...

5
E
U
F Event- Driven Programming

Sequential programming Event-Driven Programming

• Execute commands sequentially • Objects can trigger events,


and other objects can react to
• The next job is performed by those events
the next command • The next thing that happens
depends on the next event
• The program controls execution • Program flow is controlled by
User-Computer interaction

6
E
U
F Event-Driven Programming

• GUI App based on Event-Drive Programming

• The program waits for the event to appear and handle it

• Events:

• Firing an event: when the object initiates the event

• Listener: The object waits for the event to appear

• Event handler: method handles the event

7
E
U
F Event-Driven Programming

• Use delegate and event in C# for event-driven programming

Event E
subscribe
publish
B
A
C

Handler B for E
A fires event E Delegate for E
Handler C for E

8
E
U
F Event-Driven Programming

• Event handling in Windows Forms

Click
User input some
texts on texbox →
invoke
click Button to add
data into listbox

Get data from textbox


Add to listbox

Button gives a click event


Form gives an event handler for click button

9
E
U
F Event-Driven Programming
Event
• GUI-based events
• Mouse move

• Mouse click

• Mouse double-click
List of events
• Key press for the Form

• Button click

• Menu selection

• Change in focus

• Window activation

• …

10
E
U
F

Windows Forms Application

11
E
U
F Windows Form App

• Based on GUI
• Use event-driven programming
• Windows forms app contains some common items
• Menu
• Toolbar
• StatusBar
• TextBox, Label, Button…
• Base class for using in WFA is Form class

System.Windows.Forms. Form
Namespace Class

12
E
U
F Windows Form App

13
E
U
F GUI Components/Controls

• Components / controls are organized into inheritance classes,

allowing for easy sharing of properties

• Component/control defines

• Property (may modify the values when designing)

• Method

• Event

• The easiest way is to use the VS .NET Toolbox to add controls

and components to the form (in design view).

• Use code to add controls/components to a form (in code view)


14
E
U
F
Components and Controls in Windows Form

Toolbox in Visual Studio

15
E
U
F Simple Windows Forms App

Form1.cs
Base class

Label

Design form & control

Add control to form

Run Form1 as a main


form

16
E
U
F Basic steps to create Windows Forms

1. Create class inheriting from the base Form class

2. Add some controls into form


Ex: label, menu, button, textbox…

3. Design the layout of the form


Adjust the size, location, font, color

4. Write codes

5. Display the form

17
E
U
F Form and control

• All elements in the form are objects

• Controls created by classes in FCL

(Form Class Library)


• System.Windows.Forms.Label
object

• System.Windows.Forms.TextBox object

• System.Windows.Forms.Button
object
• …

• Controls are instances of FLC object

classes. object object

18
E
U
F Form
Property Description Default
Name Indicates the name used in code to identify the object Form1, Form2…

ControlBox Display control box in caption bar True

FormBorderStyle Border style of form: none, single, 3D, sizable Sizable

StartPosition Determines the position of a form when it first appears WindowsDefaultLocation

Text The test associated with the control Form1, Form2, Form3

Font The font used to display text in the control

BackColor The background color of the component

ForeColor The foreground color of this component, to display text

Method Description
Close Closing form and free resource

Hide Hide form

Show Display form

Event Description
Load Occurs before a form is displayed for the first time.

19
E
U
F Naming Controls

• In C#, default names for controls/components are:


– button1, label1, textbox1, etc.
- not very descriptive (“generic”)

• Use better, descriptive names


– Names to have meanings that make sense

20
E
U
F Naming Controls

Control Begin name with


Form frm
Button btn
TextBox txt
ListBox lst
Label lbl
CheckBox chk
GroupBox gbx
ComboBox cmb
DataGrid dg
Dataset ds
GridView gv

21
E
U
F
Create Windows Forms App from VS

Code behind event Hỗ trợ WYSIWYG


handling mechanism cho GUI design

Quickly and easily create a


Windows Form

22
E
U
F
Demo

23
E
U
F
Demo

24
E
U
F
Demo

25
E
U
F
Demo

26
E
U
F
Demo

27
E
U
F
Demo

28
E
U
F
Demo

29
E
U
F
Demo

30
E
U
F
Demo

31
E
U
F
Demo

32

You might also like