0% found this document useful (0 votes)
40 views16 pages

Dotnet 4th Chapter - Docx. Edit

Uploaded by

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

Dotnet 4th Chapter - Docx. Edit

Uploaded by

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

C# programming

CHAPTER-4

Introductionto C# windows form 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. WinForms applications can contain the different type of controls like labels,
list boxes, tooltip etc.

Creating a Windows Forms Application Using VisualStudio :


● 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(.NETFramework) in the middle of current window. After that give the project name and Click
OK.

● After that following window will display which will be divided into three parts as follows:
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.
● 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
GFGC,TumakuruPage 1
C# programming
display in the property window.

● Properties Window: This window is used to change the different propertiesof the selected
item in the Solution Explorer. Also, you can change the properties of components or controls that
you will add to the forms.
Over view of the visualstudio express IDE
● Visual Studio is an Integrated Development Environment(IDE) developed by Microsoft to
develop Desktop applications, GUI(Graphical User Interface), console, web applications, mobile
applications, cloud, and web services, etc.
● With the help of this IDE,you can create managed code as well as native code.Itusesthe various
platforms of Microsoft software development software like Windows store, Microsoft Silverlight,
and Windows API, etc.
● It is not a language-specific IDE as you can use this to write code in C#, C++, VB(Visual
Basic), Python, JavaScript, and many more languages.
● It provides support for 36 different programming languages.
VisualStudio Editions
1. Community
2. Professional
3. Enterprise

1. Code Editor:Where the user will writecode.


2. Output Window: Here the VisualStudio shows the outputs,compiler warnings,error messages
and debugging information.
3. Solution Explorer: It shows the files on which the user is currently working.
4. Properties: It will give additional information and context about the selected parts of the
current project.

Advantages of using VisualStudio IDE


● A full-featured programming platform for several operating systems, the web, and the cloud,
Visual Studio IDE is available. Users can easily browse the UI so they can write their code quickly
and precisely.
● To help developers quickly identify potential errors in the code, Visual Studio offers a robust
debugging tool.
● Developers can host their application on the server with confidence because they have
eliminated anything that could lead to performance issues.
● No matter what programming language developers are using, users of Visual Studio can get

GFGC,TumakuruPage 2
C# programming
live coding support. For faster development, the Platform offers an autocomplete option. The
built-in intelligent system offers descriptions and tips for APIs.
● ThroughVisualStudio IDE you can easily collab with your teammates in a same project. This
IDE helps the developers to share, push and pull their code with their teammates.
● Every user of Visual Studio has the ability to customize it. They have the option to add
features based on their needs. For example, they can download add-ons and install extensions in
their IDE. Even programmers can submit their own extensions.

Commoncontrols

1. Textbox control
A Text Box control is used to display, accept the text from the user a san input,ora single line of
text.
we can add multiple text and scroll barsin textbox control.
Step1:We have to drag the Text Box control from the Tool box and drop it on the Windows
form.

Step2:Once the Text Box is added to the form, we can set various properties of the TextBox by
clicking on the TextBox control.

TextBox Properties

GFGC,TumakuruPage 3
C# programming

▪ Auto Complete Mode: It is used to get or set a value that indicate show the automatic
completion works for the textbox control.
▪ Font: It is used to set the font style of the text displayed on a Windows form.
▪ Multiline: It is used to enter more than one line in a Text Box control, by changing the
Multiline property value from False to True.
▪ Preferred Height:It is used to set the preferred height of the textbox controlin the window
form.
▪ Scroll Bars: It is used to display a scroll barona multi line textbox by setting a value for a
Textbox control.
▪ Visible:TheVisible property sets a value that indicates whether the textbox should be
displayed on a Windows Form.

TextBox Events
▪ Click:When a textbox is clicked, a click event is called in the textbox control.
▪ Accept Tabs Changed: It is found in the TextBox control when the property value of
the Accept Tab is changed.
▪ Back Color Changed: It is found in the TextBox Control when the property value of the
BackColor is changed.
▪ Border Style Changed: It is found in the TextBox Control when the value of the Border
Style is changed.
▪ Font Changed: It occurs when the property of the Font is changed.

2. Label control
A label control is used to display descriptive text for the form in control. It does not participate in
user input or keyboard or mouse events. Also,we can not rename labels at runtime.
Step1:We have to drag the Label control from the Tool box and drop it on the Windows form.

Step2:Once the Label is added to the form, we can set various properties to the Label by clicking
on the Label control.

GFGC,TumakuruPage 4
C# programming

LabelProperties
▪ AutoSize: As the name defines, an Auto Size property of label control is used to set or get a
value if it is automatically resized to display all its contents.
▪ BorderStyle: It is used to set the style of the border in theWindows form.
▪ Preferred Width: It is used to set or get the preferred width for the Label control.
▪ Font: It is used to get or set the font of the text displayed on a Windows form.
▪ Preferred Height: It is used to set the height for the LabelControl.
▪ Text Align: It is used to set the alignment of text such as centre, bottom,top,left, or right.
▪ Fore Color: It is used to set the color of the text.
▪ Image: It is used to set the image to a label in Windows Form.

Label Events
▪ Auto Size Changed: An Auto Size Changed event occurs in the Label control when the
value of Auto Size property is changed.
▪ Click: Click event is occurring in the Label Control to perform a click.
▪ Double Click: When a user performs a double- clicked in the Label control, the Double Click
event occurs.
▪ Back Color Changed: A Back Color Changed event occurs in the Label control when the
value of the Back Color property is changed.
▪ Drag Drop: A Drag Drop event occurs in the Label control when a drag and drop
operation is completed.

Check Box Control


The Check Box control is a control that allows the user to select or deselect options from the available
options.

When a checkbox is selected, a tick or check mark will appear on the Windows form.

Step1: We need to drag the CheckBox control from the tool box and drop it to the
Windows form.

GFGC,TumakuruPage 5
C# programming

Step2: Once the Check Box is added to the form, we can set various properties of the check box
by clicking on the Check Box control.

Check Box Properties


▪ Default: It is used to get the default size of the check box.
▪ Check Align: It is used to set the check mark's alignment, such as horizontal or vertical on the
checkbox.
▪ Check State: The Check State property is used to verify whether the check box status is
checked in the window form.
▪ Flat Style: It is used to obtain or set the flat appearance of a checkbox.

Check Box Events


▪ Checked Changed: The Checked Changed event is found when the value of the checked
property is changed to CheckBox.
▪ Double Click: It occurs when the user performs a double click on the CheckBox
control.
▪ Check State Changed: It occurs when the value of the Check State property changes to the
CheckBox control.

Combo Box Control


The Combo Box control is used to display more than one item in a drop-down list. It is a
combination of List box and Text box in which the user can input only one item. Further more, it
also allows a user to select an item from a drop-down list.

Step1:We need to drag the combo box control from the tool box and drop it to the
Windows form.

Step2: Once the Combo Box is added to the form, we can set various properties of the Combo
Box by clicking on the Combo Box control.

GFGC,TumakuruPage 6
C# programming

Combo Box Properties

▪ Created: Ittakes a value that determines whether the control is created or not.
▪ Data Binding: It is used to bind the data with a Combo Box Control.
▪ Back Color: The Back Color property is used to set the background color of the combo
box control.
▪ Max Length: It is used by the user to enter maximum characters in the edit able area of the
combo box.
▪ Sorted: The Sorted property is used to sort all the items in the Combo Box by setting the
value.

Combo Box Events


▪ Font Changed: It occurs when the property of the font value is changed.
▪ Select Index Changed: It occurs when the property value of Select Index Changed is
changed.
▪ Help Requested: When the user requests for help in control, the Help Requested event is
called.
▪ Margin Changed: It occurs when the property of margin is changed in the Combo
Box control.

Radio Button Control

The Radio Button is used to select one option from the number of choices.

The Radio Button is mutually exclusive that represents only one it emis active and the remains
unchecked in the form.

GFGC,TumakuruPage 7
C# programming

Step1: Drag the Radio Button control from the tool box and drop it to the Windows form.

Step2: Once the Radio Button is added to the form, we can set various properties of the Radio Button
by clicking on the Radio control.

Radio Button Properties


▪ Allow Drop: It is used to set or get a value representing whether the Radio Button allows
the user to drag on the form.
▪ Auto Size: The Auto Size property is used to check whether the radio control can be
automatically resized by setting a value in the RadioButton control.
▪ Check Align: It is used to obtain or set a value that indicates the location of the check portion
in the radio Button control.
▪ Text: The Text property is used to set the name of the Radio Button control.

Event handling

Event handling in C# is a mechanism that allows classes or objects to communicate and respond
to actions or occurrences(events)that happen during the execution of a program. Event: An event
is a member of a class that allows other classes to subscribe to it
Event Handler: An event handler is a method that gets executed when the event is raisedor
triggered.

Example

using System;
class Button
{
//Definethe event
public event EventHandler Click;

//Method to trigger the event public


void OnClick()
{
Click?.Invoke(this,EventArgs.Empty);

GFGC,TumakuruPage 8
C# programming
}

GFGC,TumakuruPage 9
C# programming

//Define the subscriber class class


Program
{
static void Main(string[]args)
{
//Instantiate the publisher object Button
button = new Button();

// Subscribe to the event


button.Click+=Button_Clicked;

//Simulate a button click


button.OnClick();
}

//Event handler method


staticvoidButton_Clicked(objectsender,EventArgs e)
{
Console.WriteLine("Buttonclicked!");
}}

GUI: Graphical USer Interface

GUI stands for Graphical User Interface. It refers to aninter face that allows one to interact with
electronic devices like computers and tablets through graphic elements.

Ituses icons, menusand other graphical representations to display information,as opposed to text-
based commands.

Mouse-event handling

Mouse event handling involves responding to actions performed by the user using the mouse,
such as clicking, moving, or scrolling.

GFGC,TumakuruPage 10
C# programming

This functionality is of tencrucialin graphical user interface (GUI) applications to provide interactivity
and user feedback.

Event Types

Mouse Down: Occurs when a mouse button is pressed. Mouse

Up: Occurs when a mouse button is released.

Mouse Move: Occurs when the mouse pointer is moved.

Mouse Click: Occurs when the mouse button is clicked.

Mouse Double Click: Occurs when the mouse button is double-clicked. MouseEnter:

Occurs when the mouse pointer enters the control.

Mouse Leave: Occurs when the mouse pointer leaves the control. MouseWheel:

Occurs when the mouse wheel is rotated.

Keyboard-event handling

Keyboard event handling involves capturing and responding to user input from the keyboard.
This functionality is crucial for building interactive applications, such as text editors, games, or
form-based applications.

Event Types:

Key Down: Occurs when a key is pressed. KeyUp:

Occurs when a key is released.

Key Press: Occurs when a key is pressed while the control has focus and can represent a
character.

Windows dialog box

A dialog box in C# is a type of window, which is used to enable common communication or


dialog between a computer and its user.

The simplest type of dialogbox is the warning which display same message and may require the
user to acknowledge that the message has been read, usually by clicking “OK” or a decision as to
whether or not an action should continue by clicking “OK” or “Cancel”.

DialogResultres=MessageBox.Show("Areyou sure you want to Delete","Confirmation",


MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
if (res == DialogResult.OK) { MessageBox.Show("You
have clicked Ok Button");
//Some task…
}

GFGC,TumakuruPage 11
C# programming

if (res == DialogResult.Cancel) {
MessageBox.Show("YouhaveclickedCancelButton");
//Some task…
}

MDI

MDI stands for Multiple Document Interface applications that allow users to work with
multiple documents by opening more than one document at a time. Single Document Interface
(SDI) application can manipulate only one document at a time.

The MDI applications act as the parent and child relationship in a form.A parent form is a
container that contains child forms, while child forms can be multiple to display different
modules in a parent form.

Rules for creating a form as an MDI form.

1. Mid Parent: The Mid Parent property is used to seta parent form to a childform.
2. Active Mdi Child: The Active Mdi Child property is used to get the reference of the current
child form.
3. Is Mdi Container: The Is Mdi Container property set a Boolean value to True that represents
the creation of a form as an MDI form.
4. Layout Mdi(): The Layout Mdi () method is used to arrange the child forms in the parent or
main form.
5. Controls: It is used to get the reference of control from the child form.

Step1:First,we have to open the Windows form and create the Menu bar with the use of Menu Strip
control.

GFGC,TumakuruPage 12
C# programming

Step2: After creating the Menu, add the Sub items into the Menu bar.

GFGC,TumakuruPage 13
C# programming

Menubar

Menus are displayed as one word strings clustered in a ro wat the top of the IDE.

Menu Description
File Containscommandsforopening, closing,addingandsavingprojects,aswellas
printingproject dataandexitingVisual Studio.
Edit Containscommandsforeditingprograms,suchascut,copy,paste,undo, redo,
delete,findandselect.
View Containscommandsfordisplayingwindows(e.g.,SolutionExplorer, Toolbox,
Propertieswindow)andforaddingtoolbarstotheIDE.
Project Containscommandsformanagingprojectsandtheir files.
Build Containscommandsforcompilingaprogram.
Debug Containscommandsfordebugging(i.e.,identifyingandcorrectingproblemsina
program) and running a program. Debugging is discussed in detail in Appendix
C.
Data Containscommandsforinteractingwithdatabases(i.e.,organizedcollectionsof data
stored on computers), which we discuss in Chapter 20, Database, SQL and
ADO.NET).
Format Containscommandsforarrangingandmodifyingaform'scontrols.Notethatthe
Formatmenu appearsonlywhenaGUIcomponentis selectedin Design view.
Tools ContainscommandsforaccessingadditionalIDE tools(e.g.,theToolbox) and
optionsthatenable youtocustomizetheIDE.
Window Containscommandsforarranginganddisplaying windows.
CommunityContainscommandsforsendingquestionsdirectlytoMicrosoft, checking
questionstatus,sendingfeedbackonVisualC#andsearchingtheCodeZone developer
center and the Microsoft developers community site.
Help ContainscommandsforaccessingtheIDE'shelp features.

Tool box
Tool box window displays controls that you can add to visual studio project.
To open tool box,choose viewtool box from the menu baror press ctrl+alt+X.
You can drag and drop different controls on to the surface of the designer you are using,and
resize and position the controls.

GFGC,TumakuruPage 14
C# programming

Solution Explorer

The Solution Explorer tool window appears as apane in the upper-right side of the Visual Studio
integrated development environment(IDE).

If you don't see the Solution Explorer tool window, you can open it from the Visual Studio menu bar
by using View >Solution Explorer, or by selecting Ctrl+Alt+L.

GFGC,TumakuruPage 15
C# programming

Properties windows

The Properties window to edit and view file, project, and solution properties. You can find
Properties Window on theView menu. You can also open it by pressing F4 or by typing
Properties in the search box.

The Properties window displays different types of editing fields, depending on the needs of a
particular property.

Tool bar

Tool bars typically are rows of buttons and other controls, such as combo boxes, list boxes, text
boxes, and menu controllers. All toolbar controls are associated with commands. When you click a
toolbar button, its associated command is activated.

Using help

Contains commands for accessing the IDE's help features.

GFGC,TumakuruPage 16

You might also like