Introduction To Windows Forms
Introduction To Windows Forms
Forms
1
Windows Forms
Getting Started with
Windows Forms (winforms)
2
Windows Forms
Windows Forms ("WinForms" for short) is a GUI WinForms is primarily event-driven. An application
class library included with the .NET Framework. It consists of multiple forms (displayed as windows on
is a sophisticated object-oriented wrapper around the screen), which contain controls (labels, buttons,
the Win32 API, allowing the development of textboxes, lists, etc.) that the user interacts with
Windows desktop and mobile applications that directly. In response to user interaction, these controls
target the .NET Framework. raise events that can be handled by the program to
perform tasks.
Like in Windows, everything in WinForms is a control, which is itself a type of window. The Base Control class
provides basic functionality, including properties for setting text, location, size, and color, as well as a common set
of events that can be handled. All controls derive from the Control class, adding additional features.
3
In Visual C#, a Windows application is defined as a solution. A solution is made up of one or
more projects. Projects are groups of forms and code that make up some application.
4
How to Create a Windows Forms Application project in
Visual Studio 5
Start Visual Studio
6
Select Create new project.
7
In the Search Box, type Windows Forms, look for the Windows Forms App (.NET Framework) with
the logo C# and click Next.
8
The Windows Forms Designer opens and displays Form1 of the project.
9
Click the Button to select it. In the Properties window, under the Appearance panel, set the Text
property to “Say Hello”.
10
From the Toolbox palette, drag a Button control onto the form.
11
Double-click the Button to add an event handler for the Click event. The Code Editor will open with the
insertion point placed within the event handler function.
12
Type the following code: MessageBox.Show(“Hello World”);
13
Press Start to run the program.
14
When the application is running, click the Say Hello Button to see the “Hello World” message.
15
C# Basic Syntax
16
Output:
Length: 4.5
Width: 3.5
Area: 15.75
17
The using Keyword
Output:
18
Output:
Length: 4.5
Width: 3.5
Area: 15.75
19
The class Keyword
Output:
Length: 4.5
Width: 3.5
Area: 15.75
20
Output:
Length: 4.5
Width: 3.5
Area: 15.75
21
Comments
Comments are used for explaining code. Compilers ignore the comment entries.
Single-line comments are indicated by the '//' symbol. For example –
22
Output:
Length: 4.5
Width: 3.5
Area: 15.75
23
Member Variables
Variables are attributes or data members of a class, used for storing data. In the preceding program, the
Rectangle class has two member variables named length and width.
Output:
Length: 4.5
Width: 3.5
Area: 15.75
24
Output:
Length: 4.5
Width: 3.5
Area: 15.75
25
Member Functions
Functions are set of statements that perform a specific task. The member functions of a class are declared
within the class. Our sample class Rectangle contains three member functions: AcceptDetails,
GetArea and Display.
Output:
Length: 4.5
Width: 3.5
Area: 15.75
26
Output:
Length: 4.5
Width: 3.5
Area: 15.75
27
Member Functions
The class ExecuteRectangle contains the Main() method and instantiates the Rectangle class.
Output:
Length: 4.5
Width: 3.5
The process of creating an object from a class is called instantiation because an object is an instance of a
Area: 15.75
class.
28
Identifiers
A name must begin with a letter that could be followed by a sequence of letters,
digits (0 - 9) or underscore. The first character in an identifier cannot be a digit.
29
C# Keywords
Keywords are reserved words predefined to the C# compiler. These keywords cannot be used as
identifiers. However, if you want to use these keywords as identifiers, you may prefix the keyword with
the @ character.
30
C# Keywords
The following table lists the reserved and contextual keywords
in C#:
31
C# Keywords
The following table lists the reserved and contextual keywords
in C#:
32
Basic Controls
33
Buttons Button Conventions
The Windows Forms Button (btn) control allows
the user to click it to perform an action. Whenever
the user clicks a button, the Click event handler is
invoked. You place code in the Click event handler
to perform any action you choose.
The text displayed on the button is contained in the Text property. If your text exceeds the width of the button, it
will wrap to the next line. However, it will be clipped if the control cannot accommodate its overall height. The
appearance of the text is controlled by the Font property and the TextAlign property. The Button control can also
display images using the Image and ImageList properties.
34
Textbox and Labels
Textbox
35
Checkbox and RadioButton
CheckBox RadioButtion
CheckBox (chb) allows the user to select or RadioButton (rad) allows the user to select from a
deselect an option. It is used for multiple-selections. list of possible distinct options. It is used for single-
The CheckedChange event does not fire if you set selection. Radio buttons are also called
the value to False when the form loads. OptionButtons. These are similar to checkboxes
because the user can select and deselect them.
CheckBoxes work independently, but RadioButtons are intended to work in a group. When you select one radio
button in a group, the others are automatically deselected. RadioButtons allow the user to select one of a set of
exclusive options. All RadioButtons in a given container such as a form make a group. RadioButtons can display
text an image or both. In addition a radio's button appearance may be altered to appear as a toggle style button by
changing the Appearance property.
36
ListBox and ComboBox
ListBox
37
Items and Indices
Items
38
DomainUpDown Control
The Windows Forms DomainUpDown
control looks like a combination of a textbox and a
pair of buttons for moving up or down through a
list. The control displays and sets a text string from One possible use for this control is for
a list of choices. The user can select the string by selecting items from an alphabetically sorted list of
clicking up and down buttons to move through a names. (To sort the list, set the Sorted property to
list, by pressing the UP and DOWN ARROW keys, true.) The function of this control is very similar to
or by typing a string that matches an item in the list. the list box or combo box, but it takes up very little
space.
The key properties of the control are Items, ReadOnly, and Wrap. The Items property contains the list of objects
whose text values are displayed in the control. If ReadOnly is set to false, the control automatically completes text that the user
types and matches it to a value in the list. If Wrap is set to true, scrolling past the last item will take you to the first item in the list
and vice versa. The key methods of the control are UpButton and DownButton.
39
NumericUpDown
NumericUpDown (nud) allows the user
to display numeric values. The Windows Forms
NumericUpDown control looks like a
combination of a text box and a pair of arrows that The control displays and sets a single
the user can click to adjust a value. numeric value from a list of choices. The user can
increase and decrease the number by clicking up
and down buttons, by pressing the UP and DOWN
ARROW keys, or by typing a number.
Clicking the UP ARROW key moves the value toward its maximum; clicking the DOWN ARROW key moves the
position toward the minimum. An example where this kind of control might be useful is for a volume control on a music player.
Numeric up-down controls are used in some Windows control panel applications.
40
ListView
ListView (lsv) allows the user to select a
single (or multiple) items from a list with multiple
columns. You can add new items to a listview by adding
ListViewItem objects to the Items collection.
41
TrackBar and ProgressBar
TrackBar
42
Date and Time Properties
DateTimePicker
You must define the DEBUG and TRACE compiler directives to activate debugging and tracing
respectively. If you neglect to define either of these directives, Trace or Debug calls will be ignored during
compilation. You can define these in Visual Studio .NET by choosing Properties from the Project menu.
44