Nothing Special   »   [go: up one dir, main page]

Chapter 2.1 - Windows Programming

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 37

Unit-2: Application Development using C#.

NET

Windows Programming
Microsoft Windows is an operating system that provides graphical objects used by people to
interact with a computer. These objects, called windows, must be primarily created by a person or
a company, then installed on a computer that will be used. To create one of these objects, you can
use a computer language such as C++, Java, Object Pascal, Assembly, C#, etc.

Windows Forms

Windows Forms is a technique of creating computer applications based on the common language
runtime (CLR). It offers a series of objects called Windows Controls or simply, controls. These
controls are already created in the .NET Framework through various classes. Application
programming consists of taking advantage of these controls and customizing them for a particular
application. To exploit these controls and other features of the .NET Framework, there are
various types of applications you can create, including graphical applications (windows), web-
based applications (ASP.NET web applications), or console applications, etc.

There are two broad categories of objects used in a Windows Forms application: the forms and
the controls. The objects used in a Windows Forms application are stored in libraries also called
assemblies. As normal libraries, these assemblies have the extension .dll (which stands for
dynamic link library). In order to use one of these objects, you must know the name of the
assembly in which it is stored. Then you must add a reference to that assembly in your
application.

Create a project
First, you'll create a C# application project. The project type comes with all the template files
you'll need, before you've even added anything.

1. Open Visual Studio 2010 / 2015 / 2015 / 2017 or 2019.


2. On the start window, choose Create a new project.
3. The next step is to choose the project type as a Windows Forms application. Here we also
need to mention the name and location of our project.

Component Based Technology


1
Unit-2: Application Development using C#.NET

1. In the project dialog box, we can see various options for creating different types of
projects in Visual Studio. Click the Windows option on the left-hand side.

2. When we click the Windows options in the previous step, we will be able to see an option
for Windows Forms Application. Click this option.

3. We then give a name for the application which in our case is DemoApplication. We also
need to provide a location to store our application.

4. Finally, we click the 'OK' button to let Visual Studio create our project.

If the above steps are followed, you will get the below output in Visual Studio.

Create the application

Add a button to the form

1. Choose Toolbox to open the Toolbox fly-out window.

Component Based Technology


2
Unit-2: Application Development using C#.NET

(If you don't see the Toolbox fly-out option, you can open it from the menu bar. To do
so, View > Toolbox. Or, press Ctrl+Alt+X.)

2. Choose the Pin icon to dock the Toolbox window.

3. Choose the Button control and then drag it onto the form.

4. In the Properties window, locate Text, change the name from Button1 to Click this, and


then press Enter.

Component Based Technology


3
Unit-2: Application Development using C#.NET

(If you don't see the Properties window, you can open it from the menu bar. To do so,
choose View > Properties Window. Or, press F4.)

5. In the Design section of the Properties window, change the name


from Button1 to btnClickThis, and then press Enter.

 Note

If you've alphabetized the list in the Properties window, Button1 appears in


the (DataBindings) section, instead.

Add a label to the form

Now that we've added a button control to create an action, let's add a label control to send text to.

1. Select the Label control from the Toolbox window, and then drag it onto the form and
drop it beneath the Click this button.
2. In either the Design section or the (DataBindings) section of the Properties window,
change the name of Label1 to lblHelloWorld, and then press Enter.

Component Based Technology


4
Unit-2: Application Development using C#.NET

Add code to the form

1. In the Form1.cs [Design] window, double-click the Click this button to open


the Form1.cs window.

(Alternatively, you can expand Form1.cs in Solution Explorer, and then choose Form1.)

2. In the Form1.cs window, after the private void line, type or enter lblHelloWorld.Text


= "Hello World!"; as shown in the following screenshot:

Run the application

1. Choose the Start button to run the application.

Several things will happen. In the Visual Studio IDE, the Diagnostics Tools window will
open, and an Output window will open, too. But outside of the IDE, a Form1 dialog box
appears. It will include your Click this button and text that says Label1.

2. Choose the Click this button in the Form1 dialog box. Notice that the Label1 text changes


to Hello World!.

Component Based Technology


5
Unit-2: Application Development using C#.NET

3. Close the Form1 dialog box to stop running the app.

Component Based Technology


6
Unit-2: Application Development using C#.NET

Working with Toolbox Controls


Button in C#
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 so, he/she click 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 shape, 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.

Component Based Technology


7
Unit-2: Application Development using C#.NET

 Step 3: After drag and drop you will go to the properties of the Button control to set the
properties of the Button.

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 is 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 last add this button control to form using Add() method.

Component Based Technology


8
Unit-2: Application Development using C#.NET

// Add this Button to form


this.Controls.Add(Mybutton);

Example:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp8 {

public partial class Form1 : Form {

public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)


{

// Creating and setting the properties of label


Label l = new Label();
l.AutoSize = true;
l.Text = "Do you want to submit this project?";
l.Location = new Point(222, 145);
l.Font = new Font("French Script MT", 18);
// Adding this label to form
this.Controls.Add(l);

// Creating and setting the properties of Button


Button Mybutton = new Button();
Mybutton.Location = new Point(225, 198);
Mybutton.Text = "Submit";
Mybutton.AutoSize = true;
Mybutton.BackColor = Color.LightBlue;
Mybutton.Padding = new Padding(6);
Mybutton.Font = new Font("French Script MT", 18);

// Adding this button to form


this.Controls.Add(Mybutton);

// Creating and setting the properties of Button


Button Mybutton1 = new Button();
Mybutton1.Location = new Point(360, 198);
Mybutton1.Text = "Cancel";
Mybutton1.AutoSize = true;
Mybutton1.BackColor = Color.LightPink;

Component Based Technology


9
Unit-2: Application Development using C#.NET

Mybutton1.Padding = new Padding(6);


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

// Adding this button to form


this.Controls.Add(Mybutton1);
}
}
}

Output:

Component Based Technology


10
Unit-2: Application Development using C#.NET

CheckBox in C#
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.

Component Based Technology


11
Unit-2: Application Development using C#.NET

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

Component Based Technology


12
Unit-2: Application Development using C#.NET

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 last add this checkbox control to form using Add() method.
// Add this checkbox to form
this.Controls.Add(Mycheckbox);

Example:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp5 {

public partial class Form1 : Form {

public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)


{
// Creating and setting the properties of label
Label l = new Label();
l.Text = "Select Status:";
l.AutoSize = true;
l.Location = new Point(233, 111);
l.Font = new Font("Bradley Hand ITC", 12);
// Adding lable to form
this.Controls.Add(l);

// Creating and setting the properties of CheckBox


CheckBox Mycheckbox = new CheckBox();
Mycheckbox.Height = 50;
Mycheckbox.Width = 100;
Mycheckbox.Location = new Point(229, 136);
Mycheckbox.Text = "Married";
Mycheckbox.Font = new Font("Bradley Hand ITC", 12);

Component Based Technology


13
Unit-2: Application Development using C#.NET

// Adding checkbox to form


this.Controls.Add(Mycheckbox);

// Creating and setting the properties of CheckBox


CheckBox Mycheckbox1 = new CheckBox();
Mycheckbox1.Location = new Point(230, 198);
Mycheckbox1.Text = "UnMarried";
Mycheckbox1.AutoSize = true;
Mycheckbox1.Font = new Font("Bradley Hand ITC", 12);

// Adding checkbox to form


this.Controls.Add(Mycheckbox1);
}
}
}

Component Based Technology


14
Unit-2: Application Development using C#.NET

ComboBox in C#
In Windows Forms, ComboBox provides two different features in a single control, it
means ComboBox works as both TextBox and ListBox. In ComboBox, only one item
is displayed at a time and the rest of the items are present in the drop-down menu.
The ComboBox is a class in C# and defined
under System.Windows.Forms Namespace. You can create ComboBox using the
two different ways:
1. Design-Time: It is the easiest method to create a ComboBox control 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 ComboBox control from the ToolBox and drop it on the windows
form. You are allowed to place a ComboBox control anywhere on the windows form
according to your need.

Component Based Technology


15
Unit-2: Application Development using C#.NET

 Step 3: After drag and drop you will go to the properties of the ComboBox control to
set the properties of the ComboBox according to your need.

Output:

Run-Time: It is a little bit trickier than the above method. In this method, you can set
create your own ComboBox control using the ComboBox class. Steps to create a
dynamic ComboBox:
Step 1: Create a combobox using the ComboBox() constructor is provided by the
ComboBox class.
// Creating combobox using ComboBox class
ComboBox mybox = new ComboBox();
Step 2: After creating ComboBox, set the properties of the ComboBox provided by the
ComboBox class.
// Set the location of the ComboBox
mybox.Location = new Point(327, 77);

// Set the size of the ComboBox


mybox.Size = new Size(216, 26);

Component Based Technology


16
Unit-2: Application Development using C#.NET

// Add items in the ComboBox


mybox.Items.Add("C#");
mybox.Items.Add("Java");
mybox.Items.Add("Scala");
mybox.Items.Add("C");
mybox.Items.Add("C++");
Step 3: And last add this ComboBox control to form using Add() method.
// Add this ComboBox to the form
this.Controls.Add(mybox);
Example:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp18 {

public partial class Form1 : Form {

public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)


{
// Creating and setting the properties of label
Label l = new Label();
l.Location = new Point(122, 80);
l.AutoSize = true;
l.Text = "Select Programming Language";

// Adding this label to the form


this.Controls.Add(l);

// Creating and setting the properties of comboBox


ComboBox mybox = new ComboBox();
mybox.Location = new Point(327, 77);
mybox.Size = new Size(216, 26);
mybox.Items.Add("C#");
mybox.Items.Add("Java");
mybox.Items.Add("Scala");
mybox.Items.Add("C");
mybox.Items.Add("C++");

// Adding this ComboBox to the form


this.Controls.Add(mybox);

Component Based Technology


17
Unit-2: Application Development using C#.NET

}
}
}
Output:

Label in C#
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 as shown in the below image:
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

Component Based Technology


18
Unit-2: Application Development using C#.NET

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:

Component Based Technology


19
Unit-2: Application Development using C#.NET

2. Run-Time: It is a little bit trickier than the above method. In this method, you can
set create your own Label control using the Label class. Steps to create a dynamic
label:
Step 1: Create a label using the Label() constructor is 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 last add this Label control to form using Add() method.
// Add this label to the form
this.Controls.Add(mylab);
Example:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp18 {

public partial class Form1 : Form {

Component Based Technology


20
Unit-2: Application Development using C#.NET

public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)


{

// Creating and setting the label


Label mylab = new Label();
mylab.Text = "GeeksforGeeks";
mylab.Location = new Point(222, 90);
mylab.AutoSize = true;
mylab.Font = new Font("Calibri", 18);
mylab.ForeColor = Color.Green;
mylab.Padding = new Padding(6);

// Adding this control to the form


this.Controls.Add(mylab);
}
}
}
Output:

ListBox in C#?
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. In ListBox, you are allowed to set the font of the
content present in the ListBox using Font Property of the ListBox which makes your
ListBox more attractive. You can set this property in two different ways:
1. Design-Time: It is the easiest way to set the font of the content present in the
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: Drag the ListBox control from the ToolBox and drop it on the windows form.
You are allowed to place a ListBox control anywhere on the windows form according to

Component Based Technology


21
Unit-2: Application Development using C#.NET

your need.

 Step 3: After drag and drop you will go to the properties of the ListBox control to set
the font of the content present in the ListBox.

Output:

Component Based Technology


22
Unit-2: Application Development using C#.NET

2. RunTime: It is a little bit trickier than the above method. In this method, you can
set the font of the content present in the ListBox control programmatically with the
help of given syntax:
public override System.Drawing.Font Font { get; set; }
Here, Font indicates the font applied on the content displayed by the ListBox. The
following steps show how to set the font of the content present in the ListBox
dynamically:
Step 1: Create a list box using the ListBox() constructor is provided by the ListBox class.
// Creating ListBox using ListBox class constructor
ListBox mylist = new ListBox();
Step 2: After creating ListBox, set the Font property of the ListBox provided by the ListBox
class.
// Setting the font of the ListBox
mylist.Font = new Font("Bradley Hand ITC", 12);
Step 3: And last add this ListBox control to the form using Add() method.
// Add this ListBox to the form
this.Controls.Add(mylist);
Example:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp26 {

public partial class Form1 : Form {

public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)


{
// Creating and setting the
// properties of ListBox
ListBox mylist = new ListBox();
mylist.Location = new Point(287, 109);
mylist.Size = new Size(120, 95);
mylist.Font = new Font("Bradley Hand ITC", 12);
mylist.Items.Add("Geeks");
mylist.Items.Add("GFG");
mylist.Items.Add("GeeksForGeeks");
mylist.Items.Add("gfg");

// Adding ListBox control to the form


this.Controls.Add(mylist);

Component Based Technology


23
Unit-2: Application Development using C#.NET

}
}
}
Output:

RadioButton in C#
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

Component Based Technology


24
Unit-2: Application Development using C#.NET

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

Component Based Technology


25
Unit-2: Application Development using C#.NET

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 is 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 last add this RadioButton control to the form using Add() method.
// Add this radio button to the form
this.Controls.Add(r1);
Example:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;

Component Based Technology


26
Unit-2: Application Development using C#.NET

using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp24 {

public partial class Form1 : Form {

public Form1()
{
InitializeComponent();
}

private void RadioButton2_CheckedChanged(object sender, EventArgs e)


{
}

private void Form1_Load(object sender, EventArgs e)


{
// Creating and setting label
Label l = new Label();
l.AutoSize = true;
l.Location = new Point(176, 40);
l.Text = "Select Post:";
l.Font = new Font("Berlin Sans FB", 12);

// Adding this label to the form


this.Controls.Add(l);

// Creating and setting the


// properties of the RadioButton
RadioButton r1 = new RadioButton();
r1.AutoSize = true;
r1.Text = "Intern";
r1.Location = new Point(286, 40);
r1.Font = new Font("Berlin Sans FB", 12);

// Adding this label to the form


this.Controls.Add(r1);

// Creating and setting the


// properties of the RadioButton
RadioButton r2 = new RadioButton();
r2.AutoSize = true;
r2.Text = "Team Leader";
r2.Location = new Point(356, 40);
r2.Font = new Font("Berlin Sans FB", 12);
// Adding this label to the form
this.Controls.Add(r2);

// Creating and setting the


// properties of the RadioButton
RadioButton r3 = new RadioButton();
r3.AutoSize = true;
r3.Text = "Software Engineer";
r3.Location = new Point(480, 40);
r3.Font = new Font("Berlin Sans FB", 12);

// Adding this label to the form


this.Controls.Add(r3);

Component Based Technology


27
Unit-2: Application Development using C#.NET

}
}
}
Output:

C# | TextBox Controls
In Windows forms, TextBox plays an important role. With the help of TextBox, the
user can enter data in the application, it can be of a single line or of multiple lines.
The TextBox is a class and it is defined under System.Windows.Forms namespace.
In C#, you can create a TextBox in two different ways:
1. Design-Time: It is the simplest way to create a TextBox as shown in the following
steps:
 Step 1: Create a windows form. As shown in the below image:
Visual Studio -> File -> New -> Project -> WindowsFormApp

Component Based Technology


28
Unit-2: Application Development using C#.NET

 Step 2: Drag the TextBox control from the ToolBox and drop it on the windows form.
You can place TextBox anywhere on the windows form according to your need.

 Step 3: After drag and drop you will go to the properties of the TextBox control to
modify the TextBox design according to your requirement.

2. Run-Time: It is a little bit trickier than the above method. In this method, you can
create your own textbox using the TextBox class.

Component Based Technology


29
Unit-2: Application Development using C#.NET

Step 1 : Create a textbox using the TextBox() constructor provided by the TextBox class.


// Creating textbox
TextBox Mytextbox = new TextBox();
Step 2 : After creating TextBox, set the properties of the TextBox provided by the TextBox
class.

// Set location of the textbox


Mytextbox.Location = new Point(187, 51);

// Set background color of the textbox


Mytextbox.BackColor = Color.LightGray;

// Set the foreground color of the textbox


Mytextbox.ForeColor = Color.DarkOliveGreen;

// Set the size of the textbox


Mytextbox.AutoSize = true;

// Set the name of the textbox


Mytextbox.Name = "text_box1";
Step 3 : And last add this textbox control to from using Add() method.
// Add this textbox to form
this.Controls.Add(Mytextbox);
Example:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace my {

public partial class Form1 : Form {

public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)

Component Based Technology


30
Unit-2: Application Development using C#.NET

// Creating and setting the properties of Lable1


Label Mylablel = new Label();
Mylablel.Location = new Point(96, 54);
Mylablel.Text = "Enter Name";
Mylablel.AutoSize = true;
Mylablel.BackColor = Color.LightGray;

// Add this label to form


this.Controls.Add(Mylablel);

// Creating and setting the properties of TextBox1


TextBox Mytextbox = new TextBox();
Mytextbox.Location = new Point(187, 51);
Mytextbox.BackColor = Color.LightGray;
Mytextbox.ForeColor = Color.DarkOliveGreen;
Mytextbox.AutoSize = true;
Mytextbox.Name = "text_box1";

// Add this textbox to form


this.Controls.Add(Mytextbox);
}
}
}
Output:

C# GroupBox Class
Last Updated: 05-09-2019

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:

Component Based Technology


31
Unit-2: Application Development using C#.NET

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

Component Based Technology


32
Unit-2: Application Development using C#.NET

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

Component Based Technology


33
Unit-2: Application Development using C#.NET

// 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);
Example:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp45 {

public partial class Form1 : Form {

public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)


{
// Creating and setting
// properties of the GroupBox
GroupBox box = new GroupBox();

Component Based Technology


34
Unit-2: Application Development using C#.NET

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


box.Size = new Size(329, 94);
box.Text = "Select Gender";
box.Name = "MyGroupbox";

// Adding groupbox in the form


this.Controls.Add(box);

// Creating and setting


// properties of the CheckBox
CheckBox b1 = new CheckBox();
b1.Location = new Point(40, 42);
b1.Size = new Size(49, 20);
b1.Text = "Male";

// Adding this control


// to the GroupBox
gbox.Controls.Add(b1);

// Creating and setting


// properties of the CheckBox
CheckBox b2 = new CheckBox();
b2.Location = new Point(183, 39);
b2.Size = new Size(69, 20);
b2.Text = "Female";

// Adding this control


// to the GroupBox
box.Controls.Add(b2);
}
}
}

Output

Component Based Technology


35
Unit-2: Application Development using C#.NET

PictureBox Control
This control is used to add images to the Windows Forms. Let's see how we can
implement this with an example shown below.

Step 1) The first step is to drag the PictureBox control onto the Windows Form
from the toolbox as shown below

Step 2) The next step is to actually attach an image to the picture box control.
This can be done by following the below steps.

Component Based Technology


36
Unit-2: Application Development using C#.NET

1. First, click on the Image property for the PictureBox control. A new
window will pops out.
2. In this window, click on the Import button. This will be used to attach an
image to the picturebox control.
3. A dialog box will pop up in which you will be able to choose the image to
attach the picturebox
4. Click on the OK button

One you make the above changes, you will see the following output

Output:-

From the output, you can see that an image is displayed on the form.

Component Based Technology


37

You might also like