0% found this document useful (0 votes)
12 views36 pages

Unit 2 (Incomplete)

Uploaded by

mridulddubey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views36 pages

Unit 2 (Incomplete)

Uploaded by

mridulddubey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

Use of windows form in c#

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.

// Creating label using Label class


Label mylab = new Label();

Step 2: After creating Label, set the properties of the Label provided by the Label class.

// Set the text in Label


mylab.Text = "GeeksforGeeks";
// Set the location of the Label
mylab.Location = new Point(222, 90);

// Set the AutoSize property of the Label control


mylab.AutoSize = true;

// Set the font of the content present in the Label Control


mylab.Font = new Font("Calibri", 18);

// Set the foreground color of the Label control


mylab.ForeColor = Color.Green;

// Set the padding in the Label control


mylab.Padding = new Padding(6);

Step 3: And lastly add this Label control to form using the Add() method.

// Add this label to the form


● this.Controls.Add(mylab);
b) Button
Ans.
A Button is an essential part of an application, or software, or webpage. It allows the
user to interact with the application or software. For example, if a user wants to exit
from the current application, he/she clicks the exit button which closes the application.
It can be used to perform many actions like to submit, upload, download, etc.
according to the requirement of your program. It can be available with different
shapes, size, color, etc. and you can reuse them in different applications. In .NET
Framework, Button class is used to represent windows button control and it is inherited
from ButtonBase class. It is defined under System.Windows.Forms namespace.

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.

// Creating Button using Button class


Button MyButton = new Button();

Step 2: After creating Button, set the properties of the Button provided by the Button
class.

// Set the location of the button


Mybutton.Location = new Point(225, 198);

// Set text inside the button


Mybutton.Text = "Submit";

// Set the AutoSize property of the button


Mybutton.AutoSize = true;

// Set the background color of the button


Mybutton.BackColor = Color.LightBlue;

// Set the padding of the button


Mybutton.Padding = new Padding(6);

// Set font of the text present in the button


Mybutton.Font = new Font("French Script MT", 18);

Step 3: And lastly add this button control to form using the Add() method.

// Add this Button to form


this.Controls.Add(Mybutton);
c) Textbox
Ans.
A Button is an essential part of an application, or software, or webpage. It allows the
user to interact with the application or software. For example, if a user wants to exit
from the current application, he/she clicks the exit button which closes the application.
It can be used to perform many actions like to submit, upload, download, etc.
according to the requirement of your program. It can be available with different
shapes, size, color, etc. and you can reuse them in different applications. In .NET
Framework, Button class is used to represent windows button control and it is inherited
from ButtonBase class. It is defined under System.Windows.Forms namespace.
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 as shown in the below image:


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.

// Creating Button using Button class


Button MyButton = new Button();

Step 2: After creating Button, set the properties of the Button provided by the Button
class.

// Set the location of the button


Mybutton.Location = new Point(225, 198);

// Set text inside the button


Mybutton.Text = "Submit";

// Set the AutoSize property of the button


Mybutton.AutoSize = true;

// Set the background color of the button


Mybutton.BackColor = Color.LightBlue;

// Set the padding of the button


Mybutton.Padding = new Padding(6);

// Set font of the text present in the button


Mybutton.Font = new Font("French Script MT", 18);

Step 3: And lastly add this button control to form using the Add() method.

// Add this Button to form


this.Controls.Add(Mybutton);
d) Combobox
Ans.
A Button is an essential part of an application, or software, or webpage. It allows the
user to interact with the application or software. For example, if a user wants to exit
from the current application, he/she clicks the exit button which closes the application.
It can be used to perform many actions like to submit, upload, download, etc.
according to the requirement of your program. It can be available with different
shapes, size, color, etc. and you can reuse them in different applications. In .NET
Framework, Button class is used to represent windows button control and it is inherited
from ButtonBase class. It is defined under System.Windows.Forms namespace.
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 as shown in the below image:


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

// Creating Button using Button class


Button MyButton = new Button();
Step 2: After creating Button, set the properties of the Button provided by the Button
class.

// Set the location of the button


Mybutton.Location = new Point(225, 198);

// Set text inside the button


Mybutton.Text = "Submit";

// Set the AutoSize property of the button


Mybutton.AutoSize = true;

// Set the background color of the button


Mybutton.BackColor = Color.LightBlue;

// Set the padding of the button


Mybutton.Padding = new Padding(6);

// Set font of the text present in the button


Mybutton.Font = new Font("French Script MT", 18);

Step 3: And lastly add this button control to form using the Add() method.

// Add this Button to form


this.Controls.Add(Mybutton);
e) Listbox
Ans.
In Windows Forms, ListBox control is used to show multiple elements in a list, from
which a user can select one or more elements and the elements are generally displayed
in multiple columns. The ListBox class is used to represent the windows list box and also
provide different types of properties, methods, and events. It is defined under
System.Windows.Forms namespace.

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.

2. ListBox.SelectedObjectCollection: This class holds a collection of the selected


items which is a subset of the items contained in the ListBox control.

3. ListBox.SelectedIndexCollection: This class holds a collection of the selected


indexes, which is a subset of the indexes of the ListBox.ObjectCollection and
these indexes specify elements that are selected.
In C# you can create a ListBox in the windows form by using two different ways:

1. Design-Time: It is the easiest way to create a ListBox as shown in the following


steps:
Step 1: Create a windows form as shown in the below image:
Visual Studio -> File -> New -> Project -> WindowsFormApp

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.

// Creating a ListBox control


ListBox mylist = new ListBox();

Step 2: After creating ListBox control, set the property of the ListBox control provided
by the ListBox class.

ListBox mylist = new ListBox();


mylist.Location = new Point(287, 109);
mylist.Size = new Size(120, 95);
mylist.ForeColor = Color.Purple;
mylist.Items.Add(123);
mylist.Items.Add(456);
mylist.Items.Add(789);

Step 3: And last add this ListBox control to the form using the following statement:

// Adding ListBox control


// to the form
● this.Controls.Add(mylist);
f) RadioButton
Ans.
In Windows Forms, RadioButton control is used to select a single option among the
group of the options. For example, select your gender from the given list, so you will
choose only one option among three options like Male or Female or Transgender. In C#,
RadioButton is a class and it is defined under System.Windows.Forms namespace. In
RadioButton, you are allowed to display text, image, or both and when you select one
radio button in a group other radio buttons automatically clear. You can create
RadioButton in two different ways:

1. Design-Time: It is the easiest way to create a RadioButton control as shown in the


following steps:

Step 1: Create a windows form as shown in the below image:


Visual Studio -> File -> New -> Project -> WindowsFormApp

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.

// Creating radio button


RadioButton r1 = new RadioButton();

Step 2: After creating RadioButton, set the properties of the RadioButton provided by
the RadioButton class.

// Set the AutoSize property


r1.AutoSize = true;

// Add text in RadioButton


r1.Text = "Intern";

// Set the location of the RadioButton


r1.Location = new Point(286, 40);

// Set Font property


r1.Font = new Font("Berlin Sans FB", 12);

Step 3: And lastly add this RadioButton control to the form using the Add() method.

// Add this radio button to the form


this.Controls.Add(r1);
g) Checkbox
Ans.
The CheckBox control is the part of windows form which is used to take input from the
user. Or in other words, CheckBox control allows us to select single or multiple elements
from the given list or it can provide us options like yes or no, true or false, etc. It can be
displayed as an image or text or both. The CheckBox is a class and defined under
System.Windows.Forms namespace. In Windows form, you can create CheckBox in two
different ways:

1. Design-Time: It is the simplest way to create a CheckBox using the following steps:

Step 1: Create a windows form as shown in the below image:


Visual Studio -> File -> New -> Project -> WindowsFormApp

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.

// Set height of the checkbox


Mycheckbox.Height = 50;

// Set width of the checkbox


Mycheckbox.Width = 100;

// Set location of the checkbox


Mycheckbox.Location = new Point(229, 136);

// Set text in the checkbox


Mycheckbox.Text = "Married";

// Set font of the checkbox


Mycheckbox.Font = new Font("Bradley Hand ITC", 12);

Step 3: And lastly add this checkbox control to form using the Add() method.

// Add this checkbox to form


this.Controls.Add(Mycheckbox);
h) ColourDialog
Ans.
A C# ColorDialog control is used to select a color from available colors and also define
custom colors. A typical Color Dialog looks like Figure 1 where you can see there is a list
of basic solid colors and there is an option to create custom colors.

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.

Again, you can create a ColorDialog at design-time but It is easier to create a


ColorDialog at run-time.

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.

Adding a ColorDialog to a Form adds the following two lines of code.


● private System.Windows.Forms.ColorDialog colorDialog1;
● this.colorDialog1 = new System.Windows.Forms.ColorDialog();
Run-time

Creating a ColorDialog control at run-time is merely a work of creating an instance of


ColorDialog class, setting its properties and adding ColorDialog class to the Form
controls.

First step to create a dynamic ColorDialog is to create an instance of the ColorDialog


class. The following code snippet creates a ColorDialog control object.
● ColorDialog colorDlg = new ColorDialog();
The ShowDialog method of ColorDialog displays the ColorDialog. The following code
snippet sets background color, foreground color, Text, Name, and Font properties of a
ColorDialog.
● colorDlg.ShowDialog();
Once the ShowDialog method is called, you can pick colors on the dialog.
i) Font Dialog
Ans.
A FontDialog control in WinForms is used to select a font from available fonts installed
on a system.

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.

Adding a FontDialog to a Form adds the following two lines of code.


● private System.Windows.Forms.FontDialog fontDialog1;
● this.fontDialog1 = new System.Windows.Forms.FontDialog();
Run-time

Creating a FontDialog control at run-time is merely a work of creating an instance of


FontDialog class, setting its properties and adding FontDialog class to the Form
controls.

First step to create a dynamic FontDialog is to create an instance of the FontDialog


class.

The following code snippet creates a FontDialog control object.


● FontDialog fontDlg = new FontDialog();
ShowDialog method of FontDialog displays the FontDialog. The following code snippet
calls the ShowDialog method.
● fontDlg.ShowDialog();
Once the ShowDialog method is called, you can pick a font on the dialog.
j) Save File Dialog
Ans.
A SaveFileDialog control is used to save a file using Windows SaveFileDialog. A typical
SaveFileDialog looks like Figure 1 where you can see the Windows Explorer type
features to navigate through folders and save a file in a folder.

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

To create a SaveFileDialog control at design-time, you simply drag and drop a


SaveFileDialog control from Toolbox to a Form in Visual Studio.

Adding a SaveFileDialog to a Form adds the following two lines of code.


● private System.Windows.Forms.SaveFileDialog saveFileDialog1;
● this.saveFileDialog1 = new System.Windows.Forms.SaveFileDialog();
Run-time

Creating a SaveFileDialog control at run-time is merely a work of creating an instance


of SaveFileDialog class, setting its properties and adding SaveFileDialog class to the
Form controls.

The first step to create a dynamic SaveFileDialog is to create an instance of


SaveFileDialog class. The following code snippet creates a SaveFileDialog control
object.
● SaveFileDialog SaveFileDialog1 = new SaveFileDialog();
ShowDialog method displays the SaveFileDialog.
● SaveFileDialog1.ShowDialog();
Once the ShowDialog method is called, you can browse and select a
file.
k) Open File Dialog
Ans.
C# OpenFileDialog control allows us to browse and select files on a computer in an
application. A typical Open File Dialog looks like Figure 1 where you can see Windows
Explorer like features to navigate through folders and select a file.

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

To create an OpenFileDialog control at design-time, you simply drag and drop an


OpenFileDialog control from Toolbox to a Form in Visual Studio.

Adding an OpenFileDialog to a Form adds the following two lines of code.


● private System.Windows.Forms.OpenFileDialog openFileDialog1;
● this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
Run-time

Creating a OpenFileDialog control at run-time is merely a work of creating an instance


of OpenFileDialog class, setting its properties and adding OpenFileDialog class to the
Form controls.
First step to create a dynamic OpenFileDialog is to create an instance of the
OpenFileDialog class. The following code snippet creates an OpenFileDialog control
object.
● OpenFileDialog openFileDialog1 = new OpenFileDialog();

The ShowDialog method displays the OpenFileDialog.


● openFileDialog1.ShowDialog();
Once the ShowDialog method is called, you can browse and select a file.
l) Rich text box
Ans.
C# OpenFileDialog control allows us to browse and select files on a computer in an
application. A typical Open File Dialog looks like Figure 1 where you can see Windows
Explorer like features to navigate through folders and select a file.

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

To create an OpenFileDialog control at design-time, you simply drag and drop an


OpenFileDialog control from Toolbox to a Form in Visual Studio.

Adding an OpenFileDialog to a Form adds the following two lines of code.


● private System.Windows.Forms.OpenFileDialog openFileDialog1;
● this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
Run-time

Creating a OpenFileDialog control at run-time is merely a work of creating an instance


of OpenFileDialog class, setting its properties and adding OpenFileDialog class to the
Form controls.
First step to create a dynamic OpenFileDialog is to create an instance of the
OpenFileDialog class. The following code snippet creates an OpenFileDialog control
object.
● OpenFileDialog openFileDialog1 = new OpenFileDialog();

The ShowDialog method displays the OpenFileDialog.


● openFileDialog1.ShowDialog();
Once the ShowDialog method is called, you can browse and select a file.
m) Print dialog
Ans.
A PrintDialog control is used to open the Windows Print Dialog and let the user select
the printer, set printer and paper properties, and print a file. A typical Open File Dialog
looks like Figure 1 where you select a printer from available printers, set printer
properties, set print range, number of pages and copies and so on. Clicking on the OK
button sends the document to the printer.

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

Creating a PrintDialog control at run-time is simple. The first step is to create an


instance of the PrintDialog class and then call the ShowDialog method. The following
code snippet creates a PrintDialog control.
● PrintDialog PrintDialog1 = newPrintDialog();
● PrintDialog1.ShowDialog();
n) Group box
Ans.
In Windows form, GroupBox is a container which contains multiple controls on it and
the controls are related to each other. Or in other words, GroupBox is a frame display
around a group of controls with a suitable optional title. Or a GroupBox is used to
categorize the related controls in a group. The GroupBox class is used to represent the
windows group box and also provide different types of properties, methods, and
events. It is defined under System.Windows.Forms namespace. The main use of a group
box is to hold a logical group of RadioButton controls.
In C# you can create a GroupBox in the windows form by using two different ways:

1. Design-Time: It is the easiest way to create a GroupBox as shown in the following


steps:

Step 1: Create a windows form as shown in the below image:
Visual Studio -> File -> New -> Project -> WindowsFormApp

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.

// Setting the location of the GroupBox


box.Location = new Point(179, 145);

// Setting the size of the GroupBox


box.Size = new Size(329, 94);

// Setting text the GroupBox


box.Text = "Select Gender";

// Setting the name of the GroupBox


box.Name = "MyGroupbox";

Step 3: And last add this GroupBox control to the form and also add other controls on
the GroupBox using the following statements:

// Adding groupbox in the form


this.Controls.Add(box);

and

// Adding this control to the GroupBox


● box.Controls.Add(b2);
Explain menustrip with example
Ans.
The MenuStrip class is the foundation of menus functionality in Windows Forms. If you
have worked with menus in .NET 1.0 and 2.0, you must be familiar with the MainMenu
control. In .NET 3.5 and 4.0, the MainMenu control is replaced with the MenuStrip
control.

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:

Creating a MenuStrip control at run-time is merely a work of creating an instance of


MenuStrip class, setting its properties and adding MenuStrip class to the Form controls.
The first step to create a dynamic MenuStrip is to create an instance of the MenuStrip
class. The following code snippet creates a MenuStrip control object.

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:

DrawArc Draws an arc from the specified ellipse.


DrawCurve Draws a curve defined by an array of points.
DrawEllipse Draws an ellipse.
DrawImage Draws an image.
DrawLine Draws a line.
DrawPie Draws the outline of a pie section.
DrawPolygon Draws the outline of a polygon.
DrawRectangle Draws the outline of a rectangle.
FillPath Fills the interior of a path.
FillPie Fills the interior of a pie section.
FillPolygon Fills the interior of a polygon defined by an array of points.

FillRectangle Fills the interior of a rectangle with a Brush.

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 );
● }

Search for more on the web.


What is the use of user control? Explain with example.
How can we apply inheritance techniques to forms?
What is a data set?Explain
How can we write and read data from sql data?
List and explain the steps for SQL Database Connectivity

You might also like