Unit III - C# - 2022 - 23
Unit III - C# - 2022 - 23
To simplify the process of application development, Visual Studio .Net provides an environment
that is common for all .net languages is called Integrated Development Environment (IDE).
The IDE provides the tools for designing, executing and debugging applications.
File menu: contains commands to open new or old project, to save project and many other
options.
View menu: contains commands to display any toolbar or window of the IDE
Project menu: contains commands for adding items to the current project
Debug menu: contains commands to start and end an application as well as the basic debugging
tools
The Toolbox window displays controls that we can add to Visual Studio projects. To open
Toolbox, choose Toolbox on the View menu.
We can drag and drop different controls onto the surface of the form, resize and position the
controls.
This window is also known as the property browser. It displays all the properties of the selected
component and their settings.
The properties window is divided into columns, with the properties on the left and their settings
on the right. When we click on a particular control then it will open all the properties of it in the
properties window.
This window contains a list of the items in the current solution. A solution may contain multiple
projects and each project may contain multiple items. The solution explorer displays a
hierarchical list of all the components, organized by project. You can right click any component
of the project and select properties in the context menu to see the selected components.
This window is usually populated by the compiler with error messages, if the code can’t be
successfully compiled. We can double-click an error message in this window and the IDE will go
the line with the statement in error which you should fix.
8. Code window
When you open .Net studio for the first time, you will see the window as shown below:
The start page displays the list of recently opened projects as well as the New project and Open
project buttons.
The remaining options lead to Visual Studio sites with up-to-date information about the product,
such as news articles, updated documents and service packs or patches.
Building Windows Application with Visual Studio:
Windows Forms Application project type is used to develop an user interactive application for
database handling, for creating calculator, for creating application like Word, Notepad etc.
It will open a Form designer window and all other IDE components.
Double clock on the Form1, it will open the code window as follows:
using System;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
}
}
Label control
Label control is used to display the text that cannot be modified by the user.
Property setting
Property name and use
Design time Run time
Name
Represents a unique name of a label
control which can be accessed in the Name=lbl_result label1.Name = “lbl_result";
code.
Default name for label control is
“label1”
Location Location=20,50 label1.Location=new
Specifies the starting position of the Point(20,50);
label control on a form.
Size
Specifies the size of the label label1.Height=50;
control. Size=50,80 label1.Width=80;
We can also use Height and Width
property instead of Size property
BackColor
Set background color of label control label1.BackColor=Color.Red;
ForeColor
Set foreground color of label control label1.ForeColor=Color.Blue;
BorderStyle
Set border of a label control.
There are three possible values for BorderStyle=FixedSingle label1.BorderStyle=
this property: BorderStyle.FixedSingle;
None(default)
FixedSingle
Fixed3D
TextBox Control
A TextBox control is used accept an input from the user or to display text. It is an editable
control as it allows the user to edit the text within it.
CharacterCasing
Set Character Case.
There are three possible values
for this property: CharacterCasing=Upper
ScrollBars
Specifies which scroll bars
ScrollBars=Both
should appear in a multiline
TextBox control.
There are four possible values for
this property. These are:
None(default)
Horizontal
Vertical
Both
Example: textBox1.Focus();
2. Clear(): This method is used to clear all text from the textbox.
Example: textBox1.Clear();
1. TextChanged: This event occurs if the Text property is changed by either through program
modification or user input. This is the default event of textbox control.
2. Enter: This event occurs when the control becomes the active control of the form.
3. Leave: This event occurs when the control is no longer the active control of the form.
using System;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
}
}
Button control
Button control is an interactive control that enables users to communicate with an application. It
is generally used to execute code when it is clicked.
Property setting
Property name and use
Design time Run time
Name
Represents a unique name of a
button control which can be access Name=btn_result button1.Name = “btn_submit";
the control in the code.
Default name for Label control is
“button1”
Location Location=20,50 button1.Location=new
Specifies the starting position of the Point(20,50);
Button control on a form
Size Size=50,80 button1.Height=50;
Specifies the size of the button button1.Width=80;
control
We can also use Height and Width
property instead of Size property
BackColor button1.BackColor=Color.Red;
Set background color of button
control
ForeColor button1.ForeColor=Color.Green;
Set foreground color of button
control
Font Font=font type,font button1.Font=new Font(“Times
Represents the font of text of a style and font size New Roman”,12);
button control
Text
Represents the current text of a Text=Submit button1.Text=”Submit”;
button control
Enabled Enabled=False button1.Enabled=false;
This property has two possible
values either true or false.
If Enabled property is set to False,
button can not be clicked or receive
focus.
By default Enabled is set to True.
Image Image: Browse button button1.Image=Image.FromFile(“
Set a Button control background as d:\mypictures\logo.jpg”);
an image
Visible button1.Visible=false;
This property determines whether
the button is visible or hidden. By
default value is true.
1. Click-
2. MouseUp: When the mouse pointer is over the button and a mouse button is released.
3. MouseHover
5. Enter: Occurs when the button control becomes active control of the form
ListBox control
This control is used to display list of items .Users can select one or more items from the list.
1. Name: This property represents unique name of a ListBox control. It is used to access it in
the code.
2. Items: This property is a collection that holds the items on the control. At design time, you
can populate this list through the String Collection Editor. At runtime, you can access and
properties of the Items collection.
3. Enabled: This property is used to decide whether to make the control readonly or not.
5. Sorted: This is a Boolean property that is it have possible values either True or False. It
displays the items of ListBox in ascending order if sorted property is set to True. By default,
sorted property value is False.
Example: listBox1.Sorted=true;
6. SelectionMode: This property defines how items are selected in a ListBox. This property has
possible values as follows:
9. SelectedIndex-This property is used to set or grt the currently selected items index.
10. Text: This property returns the selected text on the control
ListBox control methods:
Example: listBox1.Items.Add(“C#”);
Ex:- listBox1.Items.Remove(listBox1.SelectedItem);
Clear( ) :This method clears or removes all the items from a list.
Ex. listBox1.Items.Clear();
ComboBox control
A comboBox control is a combination of TextBox and ListBox which enables the user to select
items from a list and enter a new item in a list.
4. Visible 5. Sorted
6. DropDownStyle: This property determines the style of ComboBox .It specifies whether the
list is always displayed or whether the list is displayed in a drop down and also specifies whether
new item can be inserted or not.
i)Simple: always displays full list and allows the user to enter new item in a list
ii)DropDown(default)-user can select item from a list and also allow to enter new item
7. Count
8. SelectedItem
9. SelectedIndex
10. Text
ComboBox control methods
ComboBox events
1. SelectedIndexChanged-
This event fires when you change the selected item in a comboBox. This is default event of
comboBox control.
PictureBox control
This control is used to display images on Windows forms. The images may be bitmaps, icons,
GIF, JPG etc. formats.
Properties:
3. BorderStyle 4. BackColor
5. Image -set the Image property to the image that you want to display.
Normal-places the upper left corner of the image at upper left in the PictureBox
Example:
1. Enabled: This property indicates whether the timer raises the Tick event.
Ex. Timer1.Enabled=true;
By default the Enabled property of Timer control is false. So before running the program
,we have to set the Enabled property to true ,then only the timer control starts its function.
2. Interval: This property indicates the interval n which to raise the Tick event.
The timer control allows to set Interval property in milliseconds. That is, 1 second=1000
milliseconds
Ex. Timer1.Interval=5000;
This event will be executed after every second.
using System;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}
Creating menus:
Menus are one of the most common elements of the Windows user interface.
Menus are attached only to forms and they are implemented by using MenuStrip control.
-It will be added to the form at the bottom and its default name is menuStrip1.
-Select the MenuStrip control and click the caption ”Type Here” and enter the first commands
caption as “File”.
i) One on the same level (representing the second command of the main menu)
ii)another one below the File(representing the first command on the File menu)
-Select the item under File and enter the Open. Again enter the remaining items of the File menu:
Save and Exit and click somewhere on the form.
-All temporary items(the ones with the Type Here caption) will disappear and the menu will be
finalized on the form.
-You can add more items as per the requirement by right clicking one of the existing menu item
and selecting InsertMenuItem
b) Separator bar
Separator bar divide menu items into logical groups. To create a separator bar in a menu:
ii)Right click the item you want to display below the separator and select InsertSeparator
i) Access keys
Access keys allow the user to open a menu by pressing the Alt key and a letter key.
Ex. To open the File menu in Windows applications, you can press Alt+F. F is the File menus
access key. Once the menu is opened, the user can select a command with the arrow keys.
Insert the ampersand symbol(&) in front of the character you want to use as access key in the
menu items Text property.
Ex: To assign an access key for Exit menu item ,type & symbol in front of E that is &Exit in the
Text property.
Shortcut keys are similar to access keys, but instead of opening a menu, they run a command
when pressed.
Shortcut keys are combinations of the control key and a function or character or digit key.
Ex. The usual shortcut key for the copy command is ctrl+c. To assign a shortcut key to a menu
command:
click on the menu item to which you want set shortcut keyin properties window set the
ShortcutKeys property and press Enter key.
This property allows the user to select modifiers that is ctrl, shift and alt and also a key.
d) Programming menu commands
Menu commands are similar to controls. Each menu item has its own name. They have certain
properties that are used to manipulate them.
Ex. Set the Name property of exit menuitem as mnuExit and write the following code in click
event of mnuExit menuitem as follows:
Application.Exit();
One of the important tasks in every application is to prompt the user for filename, font names
and sizes or colors to be used by the application.
1. OpenFileDialog 2.SaveFileDialog
3.FontDialog 4.ColorDialog
Open and Save are the most widely used common dialog boxes and they are implemented by the
OpenFileDialog and saveFileDialog controls.
OpenFileDialog control
This control prompts the user to open a file and allow the user to select a file to open
SaveFileDialog control
This control prompts the user to select a location for saving a file and allow the user to display
the name of the file to save data.
These two dialog controls are nearly identical and most of their properties are common.
1. InitialDirectory
This property sets the initial directory(folder) in which files are displayed the first time the open
and save dialog boxes are opened.
Ex: openFileDialog1.InitialDirectory=”c:”;
openFileDialog1.ShowDialog();
Ex: saveFileDialog1.InitialDirectory=”c:”;
saveFileDialog1.ShowDialog();
2.Title
By default. the open dialog box will display the word “Open” as a caption in the title bar of it. In
case of save dialog box, the caption on the title bar is “Save in” we can change this with the Title
property.
3. Filter
Using this property, we can display a list of specific files that can be opened or saved.
To display or save text files, set the Filter property to “Text Files|*.txt”
The pipe symbol separates the description of the files from the actual extension.
saveFileDialog1.Filter=”Text Files|*.txt”;
4. Filename: This property returns the path of the file selected by the user on the dialog box.
sfilename=openFileDialog1.Filename;
MesasgeBox.Show(strfilename);
Ex.: openFileDialog1.ShowDialog();
saveFileDialog1.ShowDialog();
2. OpenFile( ):
This method is used to open a file with read only permission when used with the openFileDialog
control.
When this method is used with saveFileDialog control, it saves a file with read-write permission.
Multiple Document Interface (MDI) applications allow multiple documents to be open at the
same time.
-MDI application must have only one parent form and one or more child forms.
-The parent form is the MDI form or MDI container because it contains all child forms.
-The child forms are called as MDI children
-The parent form may not contain controls.
Ex. Visual Studio IDE
MDI applications are in contrast to the SDI (Single Document Interface) which can open only one
document at a time.
Ex. Notepad
1. To make a form as MDI parent form, set its IsMdiContainer property to true.
2. To define a parent form to a child form set MdiParent property to true,
Example: Design the form1 and write the following code:
DateTimePicker control:
A DateTimePicker control allows users to select a date and time in Windows Forms
applications.
Properties:
1. Name
Name property represents a unique name of a DateTimePicker control. It is used to access the
control in the code.
dateTimePicker1.Value=DateTime.Today;
The value property contains the current date and time the control is set to.
MinDate and MaxDate properties are used to set the minimum and maximum value of date and
time can be selected by a control.
MaxDate=31-12-9998(default value)
MinDate=01-01-1753(default value)
4. ShowUpDown
We can also use an up-down control (spin button control) to set the date and time. To enable the
spin button control, we just need to set the ShowUpDown property to true. The following code
snippet sets these properties.
Events :
1. ValueChanged
Example:
using System;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}
ProgressBar control is used to represent the progress of a lengthy operation that takes time where
user must wait for the operation to be finished.
OR represent the progress of an operation
Properties:
1. Value:
The value property represents the current value of ProgressBar. The current value of ProgressBar
for minimum value is the color green to show the progress of the control. We can use the value
property to show the progress of an operation.
4. Step: This property is used to set a value in control that calls the PerformStep() method to
increase the current state of the progress bar by adding a defined step.
Methods:
1. PerformStep(): The PerformStep method increments the value of the progress bar by the
amount specified by the Step property.
Example:
using System;
using System.Windows.Forms;
namespace WindowsFormsApplication31
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
}
}
}
CheckBox control: This control allows the user to make multiple selections from a
number of options. We can click a checkbox to select an option and deselect also by
clicking on it.
BackColor This property is used to get or set the background color of the control.
This property is used to get or set a value which determine whether the
Checked CheckBox is in the checked state.
This property is used to get or set the font of the text displayed by the
Font control.
ForeColor This property is used to get or set the foreground color of the control.
Name This property is used to get or set the name of the control.
Text This property is used to get or set the text associated with this control.
This property is used to get or set a value which determine whether the
Visible control and all its child controls are displayed.
Important Events on CheckBox
Event Description
Example:
using System;
using System.Windows.Forms;
namespace WindowsFormsApplication32
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
if (chk1.Checked)
items = items + "Pen";
if (chk2.Checked)
items = items + "Pencil";
if (chk3.Checked)
items = items + "book";
}
}
GroupBox control
A GroupBox control is a container control that is used to place Windows Forms child controls
in a group. The purpose of a GroupBox is to define user interfaces where we can categorize
related controls in a group.
Property Description
BackColor This property is used to get or set the background color of the control.
This property is used to get or set the font of the text displayed by the
Font control.
ForeColor This property is used to get or set the foreground color of the control.
Name This property is used to get or set the name of the control.
Text This property is used to get or set the text associated with this control.
This property is used to get or set a value which determine whether the
Visible control and all its child controls are displayed.
DataGridView control:
DataGridView control is generally used for showing data from the database.
Example: If you have an Access database connected to C# project then you can show data from
it. The data from the database will be shown in rows and columns of DataGridView control.
This control also shows data from other database management systems like Oracle, MS-SQL
Server, MYSQL etc.
1. BackgroundColor:
This property is used to set background color of DataGridView control.
2. DataSource:
This is the most important property of DataGridView control through which we can show
records of dataset in this control. While using this property we have to specify the dataset.
Example:
dataGridView1.DataSource=ds.Tables[0];
3. Count:
This property returns the count of rows and columns present in DataGridView control.
Example:
MessageBox.Show((dataGridView1.Rows.Count).ToString());
MessageBox.Show((dataGridView1.Columns.Count).ToString());
4. ReadOnly:
This property is used to set a value that indicates whether the user of the program can edit the
cells of the control.
It has two possible values either true or false.
Example: dataGridView1.ReadOnly=true;
5. MultiSelect:
MultiSelect is a Boolean property which allows the user to select more than one cell, column or
row.
Example: dataGridView1.MultiSelect=true;
Program:
using System;
using System.Windows.Forms;
namespace WindowsFormsApplication33
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
this.studentTableAdapter.Fill(this.mydatabase2DataSet1.student);
WPF is a framework for building Windows applications that allow users to develop rich user
interfaces having 3D animations and rich colors with less code complexity.
It allows user to take the benefit of the large .net class library as it is built on .net technology.
1. WPF is newer framework so it is more in tune with current standards where as Windows
form is older one.
2. WPF is flexible and features rich. You can design very rich applications without coding or
buying controls. Windows form is not so feature rich.