100% found this document useful (1 vote)
235 views

Introduction To Windows Forms

Windows Forms is a GUI class library included with .NET Framework that allows developing Windows desktop and mobile applications. It provides controls like labels, buttons, textboxes that users interact with. These controls raise events that are handled by event methods in the application code to perform tasks. A Windows Forms application in Visual Studio is a solution containing one or more projects with forms and code. Forms display the user interface and contain controls. Properties specify characteristics of forms and controls. Methods perform actions on controls and objects.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
235 views

Introduction To Windows Forms

Windows Forms is a GUI class library included with .NET Framework that allows developing Windows desktop and mobile applications. It provides controls like labels, buttons, textboxes that users interact with. These controls raise events that are handled by event methods in the application code to perform tasks. A Windows Forms application in Visual Studio is a solution containing one or more projects with forms and code. Forms display the user interface and contain controls. Properties specify characteristics of forms and controls. Methods perform actions on controls and objects.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 44

Introduction to Windows

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.

 Forms – Windows that you create for user interface.


 Controls – Graphical features drawn on forms to allow user interaction (textboxes, labels, scroll bars, buttons,
etc.) (Forms and Controls are objects.)
 Properties – Every characteristic of a form or control is specified by a property. Example properties include
names, captions, size, color, position, and contents. Visual C# applies default properties. You can change
properties when designing the application or even when an application is executing.
 Methods – Built-in methods that can be invoked to impart some action to a particular control or object.
 Event Methods – Code related to some object or control. This is the code that is executed when a certain event
occurs. In our applications, this code will be written in the C# language.
 General Methods – Code not related to objects. This code must be invoked or called in the 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

The first statement in every C# Program is

Output:

The using keyword is used for including namespaces in theLength: 4.5


program.
A program can include multiple using statements. Width: 3.5
Area: 15.75

18
Output:

Length: 4.5
Width: 3.5
Area: 15.75

19
The class Keyword

The class keyword is used for declaring a class.

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 –

The multiline comments in C# programs start withOutput:


/* and terminates with the characters */ as
shown below −
Length: 4.5
Width: 3.5
Area: 15.75

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.

C# uses the new keyword to instantiate an object.

28
Identifiers

An identifier is a name used to identify a class, variable, function, or any other


user-defined item. The basic rules for naming classes in C# are as follows −

 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.

 It must not contain any embedded space or symbol such as ? - + ! @ # % ^ & * ( ) [ ]


{ } . ; : " ' / and \. However, an underscore ( _ ) can be used.

 It should not be a C# keyword.

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.

In C#, some identifiers have special meaning in context of code,


such as get and set are called contextual keywords.

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

TextBox (txt) allows you to display text and to


allow the user to enter information. A multilane
textbox has a practical limit 32KB of text, if you
require any more text then you will have to
consider using a RichTextBox.
Label

Label (lbl) allows you to display text to the user.

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

ListBox (lsb) allows the user to select an item from


a list of items. Both the ComboBox and ListBox
controls are derived from the ListControl class. The
listindex property in a listbox control has been
replaced with the “selectedindex” property. ComboBox

ComboBox (cbo) allows the user to either select an


item from a drop-down box or enter a different
item. Both the ComboBox and ListBox controls are
derived from the ListControl class.

37
Items and Indices
Items

Items can be added to a Windows Forms


ComboBox, ListBox, or checked list box in a variety
of ways, because these controls can be bound to a
variety of data sources.
Indices

Index type in C# is a number that can be used for


indexing. Index counts a collection items from the
beginning. To count a collection items from the
end, a prefix (^) operator can be used with an
Index.

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

TrackBar control provides scrolling with


a little different interface than a scrollbar. We can
create a TrackBar control using a Forms designer
at design-time or using the TrackBar class in code
at run-time. To create a TrackBar control at
design-time, you simply drag and drop a TrackBar ProgressBar
control from Toolbox to a Form in Visual Studio.

ProgressBar (pgr) allows the user to


indicate progress using a graphical bar. C#
ProgressBar control of WinForms allows track and
show progress of an operation.

42
Date and Time Properties
DateTimePicker

DateTimePicker (dtp) allows the


user to select a specific date
and/or time. This prompts the user
for a date or time using a
graphical calendar with scroll Date Formats
bars. This control actually exists
of two parts. It has a label that
displays the selected date and a Long Format can be different on
popup calendar that allows the different PCs and depends on the
user to select a date. settings in the (Start > Control Panel >
Regional and Language Options).
The default is “dd MMMM yyyy”
Short format default is “dd/mm/yyyy”
Time format default is “hh:mm:ss”
43
Debug and Trace Classes
Among the many diagnostic classes the .NET Framework provides are the Debug and Trace classes. The
Debug class helps us debug code, and the Trace class helps us trace the execution of code. The Debug class is
intended for debug builds, and the Trace class is used for release builds.

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

You might also like