CS6004
Application
Development
Lecture 6
Chamila Karunatilake
[email protected]
1
Outline
• Windows Forms Applications
• .NET Desktop Development
• Windows Forms Apps
• How to Create a Windows Forms App
• Important Parts of Designer Window
2
First Windows Forms Application
Form
Text Box
Label
List Box
Button
3
Form Control
Name of the class 4
Form Control
Name of the class
5
Form Control
Private Variables to hold each
control
Assign control objects to the
variables
Add those control objects to a
collection called Controls
Name and Text, two properties
of Form class 6
Set Properties of textBox1 and
textBox2
This can be done in the code
here or in the Properties window
on the design view
7
Set Properties of label1 and
label2
This can be done in the
code here or in the
Properties window on the
design view
8
Set Properties of button1 and
listBox1
This can be done in the
code here or in the
Properties window on the
design view
9
Naming Controls
• By default, the names are given to the controls as textBox1, textBox2
and so on.
• However, it is difficult to identify the control and its purpose by looking
at those names.
• Therefore, It is recommended to change them to meaningful names
specifying its purpose in the application
textBox1 txtName
You can change them on the
textBox2 txtAge properties window or in the
source code
Button1 btnAdd
10
Naming Controls
11
Add Events and Event Handlers
• After stabilizing the initial design and naming, the events can be added
to the controls.
• In the example, one of the main event would be clicking the add button.
• To initiate a click event to any control, you can simply double click on the
control.
• Then, click event will be automatically integrated to the control.
• With the event, an event handler method should also be added, which is
executed when the event is occurred.
• This event handler method will also be added automatically in the
Form.cs file (the other partial class of the Form), while event is initiated.
12
Add Events and Event Handlers
Properties Window
Form1.Designer.cs
13
Add Events and Event Handlers
Form1.cs
Event Handler
Method
14
Event Handler Method
15
Event Handler Method
• The value of the Text property of txtName TextBox control is
assigned to string type variable, name.
• The value of the Text property of txtAge TextBox control is
converted into an integer (using ToInt32() static method of
Convert static class) and assigned to int type variable, age.
• There is a property called, Items, a collection type property, to
hold all the displayed items on the list box. The name and age
together are added to that collection.
• After that, clear the input values of both text boxes by assigning
empty strings.
16