0% found this document useful (0 votes)
9 views

Lesson 4 C# Windows Forms Application With Example

This document provides an overview of creating a Windows Forms application using C# in Visual Studio, covering basics such as adding controls, event handling, and creating a simple login screen. It details various controls including labels, textboxes, listboxes, radio buttons, checkboxes, and buttons, along with step-by-step instructions for implementation. Additionally, it emphasizes the importance of naming conventions for controls to facilitate future functionality enhancements.

Uploaded by

jobinda99
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Lesson 4 C# Windows Forms Application With Example

This document provides an overview of creating a Windows Forms application using C# in Visual Studio, covering basics such as adding controls, event handling, and creating a simple login screen. It details various controls including labels, textboxes, listboxes, radio buttons, checkboxes, and buttons, along with step-by-step instructions for implementation. Additionally, it emphasizes the importance of naming conventions for controls to facilitate future functionality enhancements.

Uploaded by

jobinda99
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

ZETECH UNIVERSITY

UNIT NAME: PROGRAMMING WITH.NET Using C#.


UNIT CODE: BIT 321
Lesson 4 C# Windows Forms Application with Example

 Windows Forms Basics


 Hello World in Windows Forms
 Adding Controls to a form
 Event Handling for Controls
 Tree and PictureBox Control

A windows form application (GUI) is an application, which is designed to run on


a computer. It will not run on web browser because then it becomes a web
application.
So far we have seen how to work with C# to create console based applications. But
in a real-life scenario team normally use Visual Studio and C# to create either Windows
Forms or Web-based applications.

window will display which will be divided into three parts as follows:
1. Editor Window or Main Window:
o Here, you will work with forms and code editing. You can notice the
layout of form which is now blank. You will double click the form then
it will open the code for that.
2. Solution Explorer Window:
o It is used to navigate between all items in solution. For example, if you
will select a file form this window then particular information will be
display in the property window.
3. Properties Window:
o This window is used to change the different properties of the selected
item in the Solution Explorer. Also, you can change the properties of
components or controls that you will add to the forms.

Windows Forms Basics


 A Windows forms application is one that runs on the desktop computer. A
Windows forms application will normally have a collection of controls such as
labels, textboxes, list boxes, etc.
 Below is an example of a simple Windows form application C#.
o It shows a simple Login screen, which is accessible by the user.

1|Page
o The user will enter the required credentials and then will click the Login
button to proceed.

1. This is a collection of label controls which are normally used to describe adjacent
controls. So in our case, we have 2 textboxes, and the labels are used to tell the user
that one textbox is for entering the user name and the other for the password.
2. The 2 textboxes are used - to hold the username and password which will be entered by
the user.
3. Finally, we have the button control. The button control will normally have some
code attached to perform a certain set of actions. So for example in the above case, we
could have the button perform an action of validating the user name and password
which is entered by the user.

Our Demo starts here.


Adding Controls to a form
 In our Windows form application in C# examples, we will create one form which
will have the following functionality.
1) The ability for the user to enter name and address.
2) An option to choose the city in which the user resides in
3) The ability for the user to enter an option for the gender.
4) An option to choose a course which the user wants to learn. There will make
choices for both C# and ASP.Net
So let’s look at each control in detail and add them to build the form with the above-
mentioned functionality.

2|Page
Group Box
 A group box is used for logical grouping controls into a section. Let’s take an
example if you had a collection of controls for entering details such as name and
address of a person. Ideally, these are details of a person, so you would want to have
these details in a separate section on the Form. For this purpose, you can have a group
box. Let’s see how we can implement this with an example shown below

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

Step 2) Once the groupbox has been added, go to the properties window by clicking on
the groupbox control. In the properties window, go to the Text property and change it to
“User Details”.

3|Page
Once you make the above changes, you will see the following output
Output:-

Label Control
Next comes the Label Control. The label control is used to display a text or a message
to the user on the form. The label control is normally used along with other controls.
Common examples are wherein a label is added along with the textbox control.

4|Page
The label indicates to the user on what is expected to fill up in the textbox. Let’s see
how we can implement this with an example shown below. We will add 2 labels, one
which will be called ‘name’ and the other called ‘address.’ They will be used in
conjunction with the textbox controls which will be added in the later section.

Step 1) The first step is to drag the label control on to the Windows Form from the
toolbox as shown below. Make sure you drag the label control 2 times so that you can
have one for the ‘name’ and the other for the ‘address’.

Step 2) Once the label has been added, go to the properties window by clicking on the
label control. In the properties window, go to the Text property of each label control.
Once you make the above changes, you will see the following output
Output:-

5|Page
Textbox
A textbox is used for allowing a user to enter some text on the Windows application in
C#. Let’s see how we can implement this with an example shown below. We will add 2
textboxes to the form, one for the Name and the other for the address to be entered for
the user
Step 1) The first step is to drag the textbox control onto the Windows Form from the
toolbox as shown below

6|Page
Step 2) Once the text boxes have been added, go to the properties window by clicking on
the textbox control. In the properties window, go to the Name property and add a
meaningful name to each textbox. For example, name the textbox for the user as txtName
and that for the address as txtAddress. A naming convention and standard should be
made for controls because it becomes easier to add extra functionality to these controls,
which we will see later on.

Once you make the above changes, you will see the following output
Output:-

7|Page
List box
A Listbox is used to showcase a list of items on the Windows form. Let’s see how we can
implement this with an example shown below. We will add a list box to the form to store
some city locations.
Step 1) The first step is to drag the list box control onto the Windows Form from the
toolbox as shown below

8|Page
First, change the property of the Listbox box control, in our case, we have changed this
to lstCity
Click on the Items property. This will allow you to add different items which can show
up in the list box. In our case, we have selected items “collection”.
In the String Collection Editor, which pops up, enter the city names. In our case, we have
entered “Mumbai”, “Bangalore” and “Hyderabad”.
Finally, click on the ‘OK’ button.

Once you make the above changes, you will see the following output
Output:-

9|Page
RadioButton
A Radiobutton is used to showcase a list of items out of which the user can choose one.
Let’s see how we can implement this with an example shown below. We will add a radio
button for a male/female option.
Step 1) The first step is to drag the ‘radiobutton’ control onto the Windows Form from
the toolbox as shown below.

Step 2) Once the Radiobutton has been added, go to the properties window by clicking
on the Radiobutton control.

10 | P a g e
First, you need to change the text property of both Radio controls. Go the properties
windows and change the text to a male of one radiobutton and the text of the other to
female.
Similarly, change the name property of both Radio controls. Go the properties windows
and change the name to ‘rdMale’ of one radiobutton and to ‘rdfemale’ for the other one.

One you make the above changes, you will see the following output
Output:-

11 | P a g e
Checkbox
A checkbox is used to provide a list of options in which the user can choose multiple
choices. Let’s see how we can implement this with an example shown below. We will add
2 checkboxes to our Windows forms. These checkboxes will provide an option to the user
on whether they want to learn C# or ASP.Net.
Step 1) The first step is to drag the checkbox control onto the Windows Form from the
toolbox as shown below

12 | P a g e
Step 2) Once the checkbox has been added, go to the properties window by clicking on
the Checkbox control.

In the properties window,

First, you need to change the text property of both checkbox controls. Go the properties
windows and change the text to C# and ASP.Net.
Similarly, change the name property of both Radio controls. Go the properties windows
and change the name to chkC of one checkbox and to chkASP for the other one.
Once you make the above changes, you will see the following output
13 | P a g e
Output:-

Button
A button is used to allow the user to click on a button which would then start the
processing of the form. Let’s see how we can implement this with an example shown
below. We will add a simple button called ‘Submit’ which will be used to submit all the
information on the form.
Step 1) The first step is to drag the button control onto the Windows Form from the
toolbox as shown below

14 | P a g e
Step 2) Once the Button has been added, go to the properties window by clicking on the
Button control.

First, you need to change the text property of the button control. Go the properties
windows and change the text to ‘submit’.
Similarly, change the name property of the control. Go the properties windows and
change the name to ‘btnSubmit’.
Once you make the above changes, you will see the following output

15 | P a g e
Output:-

Use the link below for further reference.


https://www.guru99.com/c-sharp-windows-forms-application.html

16 | P a g e

You might also like