Chapter Two
Chapter Two
Chapter Two
Event-Driven programming
Prepared by:
Ataklti Nguse
2.1 Introduction To GUI
• GUI means Graphical User Interface.
• In C#, the most rapid and convenient way to create your
user interface is to do so visually, using the Windows
Forms Designer and Toolbox.
• It is the common user Interface that includes Graphical
representation like buttons and icons, and communication
can be performed by interacting with these icons rather than
the usual text-based or command-based communication.
• Windows Forms is a Graphical User Interface(GUI)
class library which is bundled in.
• The .NET framework provides a class library of various
graphical user interface tools such as frame, text box,
buttons etc.
• C# can use to implement a GUI very easily and fast.
Continued…
• . Net Framework is to provide an easier interface
to develop the applications for desktop, tablet,
PCs.
5
Cont’d
The forms designer allows a programmer to drag and drop controls form a
Toolbox onto form
If the drop toolbox isn’t visible, you can select it from the view
Toolbox menu.
The form designer generates code based on the programmer’s drag-drop
actions and property setting
6
Cont’d
You can modify the properties of controls using the
properties window
If the properties window isn’t visible, you can select it from the view |
properties window menu
The properties can be shown by category or alphabetically by selecting
an icon from the properties window toolbar
7
Cont’d
8
Adding form to a project
To create multiple forms first create the main form
Next, we’re going to add another form. To do this, here’s the
following steps:
Go to Solution Explorer
Right click project and select Add
Then Choose “Windows Form”
And it will automatically
add a new form to your project.
9
2.1.2 C# Label Control
• Labels are one of the most frequently used C#
control.
• Label control uses to display text in a set location
on the page.
• Label controls can also be used to add descriptive
text to a Form to provide the user with helpful
information.
• The Label class is defined in the
System.Windows.Forms namespace.
Drag a label control from the toolbox to the form.
13
2.1.4 TextBox Control
• A TextBox control is used to display, or accept as input, a
single line of text.
• This control has additional functionality including multiline
editing and password character masking.
• A text box object is used to display text on a form or to get
user input while a C# program is running.
• For displaying a text in a TextBox control , you can code like
this.
textBox1.Text = "http://csharp.net-informations.com";
• You can also collect the input value from a TextBox control
to a variable like this way.
string var = textBox1.Text;
C# TextBox Properties
• Textbox Maximum Length
• Background Color and Foreground Color
• Textbox BorderStyle
• Textbox ReadOnly
• Multiline TextBox
• Textbox password character
• How to retrieve non string values from textbox ?
2.1.5 ComboBox Control
• A ComboBox displays a text box combined with a
ListBox, which enables the user to select items from
the list or enter a new value .
• The user can type a value in the text field or click
the button to display a drop down list.
• You can add individual objects with the Add method.
• You can delete items with the Remove method or
clear the entire list with the Clear method.
• Adding item
comboBox1.Items.Add(“Information Systems");
comboBox1.Items.Add(“Computer Science");
comboBox1.Items.Add(“Software Engineering");
ComboBox SelectedItem
How to retrieve value from ComboBox
• If you want to retrieve the displayed item to a string
variable , you can code like this string var;
var = comboBox1.Text;Or
var item =
this.comboBox1.GetItemText(this.comboBox1.SelectedItem)
;
MessageBox.Show(item);
Continued
How to remove an item from ComboBox
• You can remove items from a combobox in two ways.
• You can remove item at a the specified index or giving a
specified item by name.
comboBox1.Items.RemoveAt(1);
• The above code will remove the second item from the
combobox. comboBox1.Items.Remove("Friday");
• The above code will remove the item "Friday" from the
combobox.
2.1.6 Checkbox and Radio Button Control
Check Box
allows user to make multiple selection from number of options
It to give the user an option, such as true/false or yes/no
You can click a check box to select it and click it again to deselect it.
19
Radio Button control
• A radio button or option button enables the user to select a single
option from a group of choices when paired with other
RadioButton controls.
• When a user clicks on a radio button, it becomes checked, and all
other radio buttons with same group become unchecked.
• The RadioButton control can display text, an Image, or both.
• Use the Checked property to get or set the state of a RadioButton.
radioButton1.Checked = true;
• Use a radio button when you want the user to choose only one
option.
• When you want the user to choose all appropriate options, use a
check box. Like check boxes, radio buttons support a Checked
property that indicates whether the radio button is selected.
ListBox Control
• The ListBox control enables you to display a list of items to the
user that the user can select by clicking.
• In addition to display and selection functionality, the ListBox also
provides features that enable you to efficiently add items to the
ListBox and to find text within the items of the list.
• You can use the Add or Insert method to add items to a list box.
• The Add method adds new items at the end of an unsorted list box.
listBox1.Items.Add("Sunday");
• If you want to retrieve a single selected item to a variable , you can
code like this
string var;
var = listBox1.Text;
Cont…
• The SelectionMode property determines how many items in the
list can be selected at a time.
• A ListBox control can provide single or multiple selections using
the SelectionMode property .
• If you change the selection mode property to multiple select ,
then you will retrieve a collection of items from
ListBox1.SelectedItems property.
listBox1.SelectionMode = SelectionMode.MultiSimple;
foreach (Object obj in listBox1.SelectedItems )
{
MessageBox.Show(obj.ToString ());
}
Picture
box
This windows pictureBox control is used to display images
in bitmap, GIF, icon, or JPEG formats
Picture
Box
23
C# ProgressBar Control
A progress bar is a control that an application can use to indicate
the progress of a lengthy operation such as calculating a
complex result, downloading a large file from the Web etc.
24
C# ScrollBars Control
25
C# DateTimePicker Control
26
C# Treeview Control
The TreeView control contains a hierarchy of TreeViewItem controls.
It provides a way to display information in a hierarchical structure by
using collapsible nodes .
The top level in a tree view are root nodes that can be expanded or
collapsed if the nodes have child nodes.
27
C# ListView Control
The ListView control is an ItemsControl that is derived from ListBox.
28
C# Menu Control
A Menu on a Windows Form is created with a MainMenu object,
which is a collection of MenuItem objects.
MainMenu is the container for the Menu structure of the form and
menus are made of MenuItem objects that represent individual
parts of a menu.
29
NumericUpDow
n
The NumericUpDown controls is typically used to get numeric inputs and
automatically restricts user for giving invalid non- numeric values.
The NumericUpDown control appears like a textbox control, but there
are arrow buttons on its right or left side that is used to increment or decrement the
value of the control
30
C# MDI Form
31
MonthCalende
r The MonthCalender control presents an intuitive
graphical interface for users to view and set date
information.
32
The ColorDialog
Control
The colorDialog(system.Windows.Forms.ColorDialog) is
used when you want to pick different colors.
For example, when you want to pick a color of the font or
a background color for the form, you can use the
colorDialog control.
The following are some of the useful properties of
the ColorDialog control.
33
Cont’
d
34
Cont’
d
35
The FontDialog
Color
The FontDialog control(System.Windows.FontDialog) is
a handy control for selecting different kinds of font.
36
Cont’
d
37
FolderBrowserDialod
Control
Folder browser is a .NET control that prompts user to
browse and select a folder location.
Using FolderBrowserDialoge user can only select folders
and not files.
The FolderBrowserDialog
control(System.Windows.Forms.FolderBrowserDialod
allows browse a folder on your system.
38
Cont’
d
Halu Step 2
Step 1
Step 3
39
Cont’
d
40
OpenFileDialog
Control
An OpenFileDialog control is used to browse and select a
file on a computer.
41
SaveFileDialog
control
SaveFileDialog control is used to save a file using
Windows Save File Dialog.
42
C# Print Dialog Box
user can use the Print dialog box to select a printer, configure it, and
perform a print job.
Print dialog boxes provide an easy way to implement Print and Print
Setup dialog boxes in a manner consistent with Windows standards.
43
Part-II
Form Validation and
Database Connectivity
What is form validation?
Form validation is a “technical process where a web form checks
if the information provided by a user is correct or not.”
The form will either alert the user that they messed up and need to
fix something to proceed, or the form will be validated and the user
will be able to continue with their registration process.
There are many types of validation in C# that we can use to validate
the field of any registration form or any other form which takes
inputs or values from the user.
They are given below.
45
Continued…
•Textbox Validation in C#
•Length Validation in C#
•UserName Validation in C#
•Password Validation in C#
•Email Validation in C#
•Phone number Validation in C#
C# Regular Expressions
• A regular expression defines a search pattern for strings.
• Regex represents an immutable regular expression. It contains
methods to match text, replace text, or split text.
Validation For Alphabetic and
digit only
• "^[.0-9]*$" for digit numbers
• “[a-zA-Z ]*$” For alphabetic ONLY
C# regex email
.NET Framework Data It is used for Oracle data sources. It uses the
Provider for Oracle System.Data.OracleClient namespace.
It provides data access for Entity Data Model
EntityClient Provider applications. It requires the
System.Data.EntityClient namespace.
.NET Framework Data It provides data access for Microsoft SQL Server
Provider for SQL Server Compact 4.0. It requires the
Compact 4.0. System.Data.SqlServerCe namespace.
continued
• The .NET Framework Data Provider for SQL Server
classes is located in the System.Data.SqlClient
namespace.
• using System.Data.SqlClient;
This namespace contains the following important classes.
Class Description
It is used to create SQL Server connection. This class
SqlConnection
cannot be inherited.
Method Description
Open() It is used to open a database connection.