Unit 2 (Incomplete)
Unit 2 (Incomplete)
Ans.
Windows Forms is a UI framework for building Windows desktop apps. It provides one
of the most productive ways to create desktop apps based on the visual designer
provided in Visual Studio. Functionality such as drag-and-drop placement of visual
controls makes it easy to build desktop apps.
With Windows Forms, you develop graphically rich apps that are easy to deploy,
update, and work while offline or while connected to the internet. Windows Forms apps
can access the local hardware and file system of the computer where the app is
running.
Write a short note on following Windows Form Controls
a) Label
Ans.
In Windows Forms, Label control is used to display text on the form and it does not
take part in user input or in mouse or keyboard events. The Label is a class and it is
defined under System.Windows.Forms namespace. In windows form, you can create
Label in two different ways:
1. Design-Time: It is the easiest method to create a Label control using the following
steps:
Step 1: Create a windows form:
Visual Studio -> File -> New -> Project -> WindowsFormApp
Step 2: Drag the Label control from the ToolBox and drop it on the windows form. You
are allowed to place a Label control anywhere on the windows form according to your
need.
Step 3: After drag and drop you will go to the properties of the Label control to set the
properties of the Label according to your need.
Output:
2. Run-Time: It is a little bit trickier than the above method. In this method, you can
create your own Label control using the Label class. Steps to create a dynamic label:
Step 1: Create a label using the Label() constructor provided by the Label class.
In C# you can create a button on the windows form by using two different ways:
1. Design-Time: It is the easiest method to create a button. Use the below steps:
Step 1: Create a windows form:
Visual Studio -> File -> New -> Project -> WindowsFormApp
Step 2: Drag the Button control from the ToolBox and drop it on the windows form. You
are allowed to place a Button control anywhere on the window form according to your
need.
Step 3: After drag and drop you will go to the properties of the Button control to set the
properties of the Button.
Output:
2. Run-Time: It is a little bit trickier than the above method. In this method, you can
create your own Button using the Button class.
Step 1: Create a button using the Button() constructor provided by the Button class.
Step 2: After creating Button, set the properties of the Button provided by the Button
class.
Step 3: And lastly add this button control to form using the Add() method.
Step 2: Drag the Button control from the ToolBox and drop it on the windows form. You
are allowed to place a Button control anywhere on the window form according to your
need.
Step 3: After drag and drop you will go to the properties of the Button control to set the
properties of the Button.
Output:
2. Run-Time: It is a little bit trickier than the above method. In this method, you can
create your own Button using the Button class.
Step 1: Create a button using the Button() constructor provided by the Button class.
Step 2: After creating Button, set the properties of the Button provided by the Button
class.
Step 2: Drag the Button control from the ToolBox and drop it on the windows form. You
are allowed to place a Button control anywhere on the windows form according to your
need.
Step 3: After drag and drop you will go to the properties of the Button control to set the
properties of the Button.
Output:
2. Run-Time: It is a little bit trickier than the above method. In this method, you can
create your own Button using the Button class.
Step 1: Create a button using the Button() constructor provided by the Button class.
Step 3: And lastly add this button control to form using the Add() method.
The ListBox class contains three different types of collection classes, i.e.
1. ListBox.ObjectCollection: This class holds all the elements contained in the
ListBox control.
Step 2: Next, drag and drop the ListBox control from the toolbox to the form.
Step 3: After drag and drop you will go to the properties of the ListBox control to
modify ListBox according to your requirement.
Output:
2. Run-Time: It is a little bit trickier than the above method. In this method, you can
create a ListBox control programmatically with the help of syntax provided by the
ListBox class. The following steps show how to set the create ListBox dynamically:
Step 1: Create a ListBox control using the ListBox() constructor provided by the ListBox
class.
Step 2: Drag the RadioButton control from the ToolBox and drop it on the windows
form. You are allowed to place a RadioButton control anywhere on the windows form
according to your need.
Step 3: After drag and drop you will go to the properties of the RadioButton control to
modify RadioButton control according to your requirement.
Output:
2. Run-Time: It is a little bit trickier than the above method. In this method, you can
create a RadioButton programmatically with the help of the RadioButton class. The
following steps show how to create a RadioButton dynamically:
Step 1: Create a radio button using the RadioButton() constructor provided by the
RadioButton class.
Step 3: And lastly add this RadioButton control to the form using the Add() method.
1. Design-Time: It is the simplest way to create a CheckBox using the following steps:
Step 2: Drag the CheckBox control from the ToolBox and drop it on the windows form.
You can place CheckBox anywhere on the windows form according to your need.
Step 3: After drag and drop you will go to the properties of the CheckBox control to
modify the CheckBox design according to your requirement.
Output:
2. Run-Time: It is a little bit trickier than the above method. In this method, you can
create your own checkbox programmatically using the CheckBox class.
Step 1: Create a checkbox using the CheckBox() constructor provided by the CheckBox
class.
// Creating checkbox
CheckBox Mycheckbox = new CheckBox();
Step 2: After creating CheckBox, set the properties of the CheckBox provided by the
CheckBox class.
Step 3: And lastly add this checkbox control to form using the Add() method.
Creating a ColorDialog
We can create a ColorDialog control using a Forms designer at design-time or using the
ColorDialog class in code at run-time (also known as dynamically). Unlike other
Windows Forms controls, a ColorDialog does not have and does not need visual
properties like others. The only purpose of ColorDialog is to display available colors,
create custom colors and select a color from these colors. Once a color is selected, we
need that color in our code so we can apply it on other controls.
Design-time
To create a ColorDialog control at design-time, you simply drag and drop a ColorDialog
control from Toolbox to a Form in Visual Studio.
A typical Font Dialog looks like Figure 1 where you can see there is a list of fonts, styles,
size and other options. Please note a FontDialog may have different fonts on different
systems depending on what fonts are installed on a system.
FontDialog
We can create a FontDialog control using a Forms designer at design-time or using the
FontDialog class in code at run-time (also known as dynamically). Unlike other Windows
Forms controls, a FontDialog does not have and does not need visual properties like
others. You use a FontDialog to list all the fonts and select one of them and usually
apply selected fonts on controls or some contents.
Note
Even though you can create a FontDialog at design-time as well as at run-time, I
recommend using the run-time method.
Design-time
To create a FontDialog control at design-time, you simply drag and drop a FontDialog
control from Toolbox to a Form in Visual Studio.
Creating a SaveFileDialog
We can create a SaveFileDialog control using a Forms designer at design-time or using
the SaveFileDialog class in code at run-time (also known as dynamically). Unlike other
Windows Forms controls, a SaveFileDialog does not have and not need visual
properties like others.
Note
Even though you can create a SaveFileDialog at design-time it is easier to create a
SaveFileDialog at run-time.
Design-time
Creating a OpenFileDialog
We can create an OpenFileDialog control using a Forms designer at design-time or
using the OpenFileDialog class in code at run-time (also known as dynamically). Unlike
other Windows Forms controls, an OpenFileDialog does not have and does not need
visual properties like others. The only purpose of OpenFileDialog is to display available
colors, create custom colors and select a color from these colors. Once a color is
selected, we need that color in our code so we can apply it on other controls.
Again, you can create an OpenFileDialog at design-time but it is easier to create an
OpenFileDialog at run-time.
Design-time
Creating a OpenFileDialog
We can create an OpenFileDialog control using a Forms designer at design-time or
using the OpenFileDialog class in code at run-time (also known as dynamically). Unlike
other Windows Forms controls, an OpenFileDialog does not have and does not need
visual properties like others. The only purpose of OpenFileDialog is to display available
colors, create custom colors and select a color from these colors. Once a color is
selected, we need that color in our code so we can apply it on other controls.
Again, you can create an OpenFileDialog at design-time but it is easier to create an
OpenFileDialog at run-time.
Design-time
Creating a PrintDialog
We can create a PrintDialog at design-time as well as at run-time.
Design-time
To create a PrintDialog control at design-time, you simply drag and drop a PrintDialog
control from Toolbox to a Form in Visual Studio.
Run-time
Step 2: Next, drag and drop the GroupBox from the toolbox on the form.
Step 3: After drag and drop you will go to the properties of the GroupBox to modify
GroupBox according to your requirement.
Output:
2. Run-Time: It is a little bit trickier than the above method. In this method, you can
create a GroupBox programmatically with the help of syntax provided by the GroupBox
class. The following steps show how to set the create GroupBox dynamically:
Step 1: Create a GroupBox using the GroupBox() constructor provided by the GroupBox
class.
// Creating a GroupBox
GroupBox box = new GroupBox();
●
Step 2: After creating GroupBox, set the property of the GroupBox provided by the
GroupBox class.
and
Creating a MenuStrip
We can create a MenuStrip control using a Forms designer at design-time or using the
MenuStrip class in code at run-time or dynamically.
To create a MenuStrip control at design-time, you simply drag and drop a MenuStrip
control from Toolbox to a Form in Visual Studio. Once a MenuStrip is on the Form, you
can add menu items and set its properties and events.
Output:
C# Code
● MenuStrip MainMenu = new MenuStrip();
List and explain different function available in GDI+ (Search )
Ans.
Once you have the Graphics reference, you can call any of this class's members to draw
various objects. Here are some of Graphics class's methods:
DrawLine:
DrawLine function of the Graphics class draws a line. It takes three parameters, a pen,
and two Point class parameters, starting and ending points. Point class constructor
takes x, y arguments.
● protected override void OnPaint(PaintEventArgs pe)
● {
● Graphics g = pe.Graphics ;
● Pen pn = new Pen( Color.Blue );
● // Rectangle rect = new Rectangle(50, 50, 200, 100);
● Point pt1 = new Point( 30, 30);
● Point pt2 = new Point( 110, 100);
● g.DrawLine( pn, pt1, pt2 );
● }
DrawElipse:
An ellipse( or a circle) can be drawn by using DrawEllipse method. This method takes
only two parameters, Pen and rectangle.
● protected override void OnPaint(PaintEventArgs pe)
● {
● Graphics g = pe.Graphics ;
● Pen pn = new Pen( Color.Blue, 100 );
● Rectangle rect = new Rectangle(50, 50, 200, 100);
● g.DrawEllipse( pn, rect );
● }