0% found this document useful (0 votes)
99 views10 pages

8.introduction To C# Window Form Application

Uploaded by

safia sadaf
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
99 views10 pages

8.introduction To C# Window Form Application

Uploaded by

safia sadaf
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 10

Introduction to C# Windows Forms Applications

Windows Forms is a Graphical User Interface (GUI) class library which is


bundled in .Net Framework. Its main purpose is to provide an easier
interface to develop the applications for desktop, tablet, PCs. It is also
termed as the WinForms. The applications which are developed by using
Windows Forms or WinForms are known as the Windows Forms
Applications that runs on the desktop computer. WinForms can be used
only to develop the Windows Forms Applications not web applications.
Windows Forms applications are created using the C# programming
language and the .NET framework. They are built by dragging and
dropping controls such as buttons, text boxes, labels, and other user
interface elements onto a form.
The Windows Forms framework provides a rich set of controls that
developers can use to build applications with. These controls are designed
to provide a consistent and familiar user interface for Windows users.
Developers can customize the appearance and behavior of these controls
by setting various properties and handling events.

To create a Windows Forms application in C#, you can use Microsoft


Visual Studio, which is an integrated development environment (IDE) that
provides a visual designer to create and layout the user interface
elements. The visual designer is a drag-and-drop interface for building
your UI, and you can easily configure each control’s properties through a
user-friendly interface.

In addition to the visual designer, Visual Studio also provides a code


editor that enables developers to write the C# code for the application’s
logic. Developers can handle events and perform tasks such as data
validation, data manipulation, and business logic implementation.
Windows Forms applications are versatile and can be used to create
various types of applications such as data entry, management, and
reporting applications, as well as games and multimedia applications.
Overall, Windows Forms applications provide an easy-to-use and familiar
graphical user interface for Windows users, making it an ideal choice for
building desktop applications that require a high degree of interactivity
and user engagement.

Creating a Windows Forms Application Using Visual Studio


• First, open the Visual Studio then Go to File -> New -> Project to
create a new project and then select the language as Visual C# from
the left menu. Click on Windows Forms App(.NET Framework) in the
middle of current window. After that give the project name and
Click OK.

Here the solution is like a container which contains the projects and
files that may be required by the program.
• After that following window will display which will be divided into three
parts as follows:
1. Editor Window or Main Window: 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: 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: 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.
Now to add the controls to your WinForms application go
to Toolbox tab present in the extreme left side of Visual Studio. Here,
you can see a list of controls. To access the most commonly used
controls, go to Common Controls present in Toolbox tab.

• Now drag and drop the controls that you needed on created Form. For
example, if you can add Textbox, List Box, Button etc. as shown below.
By clicking on the particular dropped control, you can see and change
its properties present in the right most corner of Visual Studio.
• In the above image, you can see the Textbox is selected and its
properties like TextAlign, MaxLength etc. are opened in right most
corner. You can change its properties’ values as per the application
need. The code of controls will be automatically added in the
background. You can check the Form1.Designer.cs file present in the
Solution Explorer Window.
• To run the program, you can use an F5 key or Play button present in
the toolbar of Visual Studio. To stop the program you can use pause
button present in the Toolbar. You can also run the program by going
to Debug->Start Debugging menu in the menubar.
Introduction to windows form controls
In Windows app development, a control is a UI element that displays
content or enables interaction. You create the UI for your app by using
controls such as buttons, text boxes, and combo boxes to display data and
get user input.
Add a control

You can add a control to an app in several ways:

• Use a design tool like Blend for the Microsoft Visual Studio Extensible
Application Markup Language (XAML) designer.
• Add the control to the XAML markup in the Visual Studio XAML editor.
• Add the control in code. Controls that you add in code are visible when
the app runs, but are not visible in the Visual Studio XAML designer.
The Visual Studio Toolbox displays many of the controls that you can use in
your app. To add a control to your app, double-click it in the Toolbox. For
example, when you double-click the TextBox control, this XAML is added to
the XAML view.

You can also drag the control from the Toolbox to the XAML designer.
Set the name of a control

To work with a control in code, you set its x:Name attribute and reference it
by name in your code. You can set the name in the Visual Studio Properties
window or in XAML. Here's how to set the name of the currently selected
control by using the Name text box at the top of the Properties window.

To name a control

1. Select the element to name.


2. In the Properties panel, type a name into the Name text box.
3. Press Enter to commit the name.
Here's how to set the name of a control in the XAML editor by adding the
x:Name attribute.
<Button x:Name="Button1" Content="Button"/>

Set the control properties

You use properties to specify the appearance, content, and other attributes
of controls. When you add a control using a design tool, some properties
that control size, position, and content might be set for you by Visual
Studio. You can change some properties, such as Width, Height or Margin,
by selecting and manipulating the control in the Design view.

To reset a property

1. In the Properties panel, click the property marker next to the property
value. The property menu opens.
2. In the property menu, click Reset

Create an event handler

Each control has events that enable you to respond to actions from your
user or other changes in your app. For example, a Button control has a Click
event that is raised when a user clicks the Button. You create a method,
called an event handler, to handle the event. You can associate a control's
event with an event handler method in the Properties window, in XAML, or
in code.
To create an event handler, select the control and then click the Events tab
at the top of the Properties window. The Properties window lists all of the
events available for that control. Here are some of the events for a Button.

Controls in Windows Forms


• Buttons
• Check boxes
• Combo box and list box
• Hyperlinks
• Radio buttons
• Sliders
Buttons

A button gives the user a way to trigger an immediate action. Some


buttons are specialized for particular tasks, such as navigation, repeated
actions, or presenting menus. Use a Button control to let the user initiate
an immediate action, such as submitting a form.

Don't use a Button control when the action is to navigate to another page;
instead, use a HyperlinkButton control.

Check boxes

A check box is used to select or deselect action items. It can be used for a
single item or for a list of multiple items that a user can choose from. The
control has three selection states: unselected, selected, and indeterminate.
Use the indeterminate state when a collection of sub-choices have both
unselected and selected states.

Combo box and list box


Use a combo box (also known as a drop-down list) to present a list of items
that a user can select from. A combo box starts in a compact state and
expands to show a list of selectable items. A list box is similar to a combo
box, but is not collapsible/does not have a compact state.

When the combo box is closed, it either displays the current selection or is
empty if there is no selected item. When the user expands the combo box,
it displays the list of selectable items.
Hyperlinks
Hyperlinks navigate the user to another part of the app, to another app, or
launch a specific uniform resource identifier (URI) using a separate browser
app. There are two ways that you can add a hyperlink to a XAML app:
the Hyperlink text element and HyperlinkButton control. Use a hyperlink
when you need text that responds when pressed and navigates the user to
more information about the text that was pressed.

Radio buttons

Radio buttons, also called option buttons, let users select one option from a
collection of two or more mutually exclusive, but related, options. Radio
buttons are always used in groups, and each option is represented by one
radio button in the group.

In the default state, no radio button in a RadioButtons group is selected.


That is, all radio buttons are cleared. However, once a user has selected a
radio button, the user can't deselect the button to restore the group to its
initial cleared state.

The singular behavior of a RadioButtons group distinguishes it from check


boxes, which support multi-selection and deselection, or clearing.

Use radio buttons to let users select from two or more mutually exclusive
options.
Sliders

A slider is a control that lets the user select from a range of values by
moving a thumb control along a track.

Use a slider when you want your users to be able to set defined, contiguous
values (such as volume or brightness) or a range of discrete values (such as
screen resolution settings).

You might also like