Unit 4
Unit 4
ArrayList in C#:
ArrayList represents an ordered collection of an object that can
be indexed individually. It is basically an alternative to an array. It also allows
dynamic memory allocation, adding, searching and sorting items in the list.
An ArrayList can be used to add unknown data where you don't know the types
and the size of the data.
The ArrayList collection is similar to the Arrays data type in C#. The biggest
difference is the dynamic nature of the array list collection.
Key Point of ArrayList Class:
❖ Elements can be added or removed from the Array List collection at any
point in time.
❖ The ArrayList is not guaranteed to be sorted.
❖ The capacity of an ArrayList is the number of elements the ArrayList can
hold.
❖ Elements in this collection can be accessed using an integer index. Indexes
in this collection are zero-based.
❖ It also allows duplicate elements.
❖ Using multidimensional arrays as elements in an ArrayList collection is
not support
Declaration of an Array List:
The “new” keyword is used to create an object of an ArrayList. The object is then
assigned to the variable a1. So now the variable a1 will be used to access the
different elements of the array list.
ArrayList a1 = new ArrayList()
Adding elements to an array:
The add method is used to add an element to the ArrayList. The add method can
be used to add any sort of data type element to the array list.
ArrayList.add(element)
Ex:
1. a1.add(1) – This will add an Integer value to the collection
ArrayList Properties:
Properties Description
Capacity Gets or sets the number of elements that the ArrayList can
contain.
Count Gets the number of elements actually contained in the
ArrayList.
IsFixedSize Gets a value indicating whether the ArrayList has a fixed
size.
IsReadOnly Gets a value indicating whether the ArrayList is read-only.
Item Gets or sets the element at the specified index.
ArrayList Methods:
Methods Description
Add()/AddRange() Add() method adds single elements at the end of
ArrayList.
AddRange() method adds all the elements from
the specified collection into ArrayList.
Insert()/InsertRange() Insert() method insert a single elements at the
specified index in ArrayList.
InsertRange() method insert all the elements of
the specified collection starting from specified
index in ArrayList.
Exception Handling:
Definition: An exception is an event, which occurs during the execution of a
program, that disrupts the normal flow of the program's instructions.
Exception handling is the process of responding to unwanted or unexpected
events when a computer program runs. Exception handling deals with these
events to avoid the program or system crashing, and without this process,
exceptions would disrupt the normal operation of a program.
Major reasons why an exception Occurs
❖ Invalid user input
❖ Device failure
❖ Loss of network connection
❖ Physical limitations (out of disk memory)
❖ Code errors
❖ Opening an unavailable file
Let us discuss the most important part which is the differences between Error and
Exception that is as follows:
Error: An Error indicates a serious problem that a reasonable application should
not try to catch.
(1) Syntax Errors : The spelling errors in your variables, keywords or statements.
.Net always finds out this error in compilation.
(2) Logic Errors : The errors in our logic applied in code. At Runtime errors, We
have to shut down our application at a time.
Exception: Exception indicates conditions that a reasonable application might
try to catch.
Exception handling is built upon four keywords: try, catch, finally, and throw.
try − A try block identifies a block of code for which particular exceptions is
activated. It is followed by one or more catch blocks.
catch − A program catches an exception with an exception handler at the place
in a program where you want to handle the problem. The catch keyword indicates
the catching of an exception.
Exception Classes in C#
Class Description
System.IO.IOException Handles I/O errors.
System.IndexOutOfRangeException Handles errors generated when a
method refers to an array index out
of range.
System.ArrayTypeMismatchException Handles errors generated when type
is mismatched with the array type.
System.NullReferenceException Handles errors generated from
referencing a null object.
System.DivideByZeroException Handles errors generated from
dividing a dividend with zero.
System.InvalidCastException Handles errors generated during
typecasting.
System.OutOfMemoryException Handles errors generated from
insufficient free memory.
System.StackOverflowException Handles errors generated from stack
overflow.
When any value (text or number) is entered in textbox and Submit button clicked
then form will execute.
ErrorProvider Property:
• Icon property: which allows us to set an icon that will be display when
error occurred?
• BlinkRate property: which allows setting the rate in milliseconds at
which the icon blinks.
• Icon: Gets or sets the Icon that is displayed next to a control when an
error occurred.
• BlinkStyle: Gets or sets a value indicating when the error icon flashes.
DateTimePicker Control :
A DateTimePicker control allows users to select a date and time in Windows Forms
applications. It is use to display Date & Time in user defined format.
DateTimePicker control and also provide different types of properties, methods, and
events. It is defined under System.Windows.Forms namespace.
❖ Properties :-
Name :-
It means to give unique name of DateTimePicker. To indicates the name used in
code to identify the DateTimePicker.
Ex. DateTimePicker1.Name= DateTimePicker1 (by default)
CalenderForecolor :-
It is a property to set or get the foreground color of the calender. It is used to
display and changing the color of text.
Ex. DateTimePicker1.Forecolor = Color. Red
Enabled :-
Enabled means your DateTimePicker is enable or not. It indicates whether the
DateTimePicker is enabled.
Ex. DateTimePicker1.Enabled = True (by default)
DateTimePicker1.Enabled = False
Size :-
It means the size of height & width of DateTimePicker. It is use to display the
size of the DateTimePicker in pixels.
Ex. DateTimePicker1.Size = 200.20 (by default)
❖ Methods :-
Dispose() :-
It means the DateTimePicker is not in the memory and it is removed. It is used to
remove the data to the memory
Ex. DateTimePicker1.Dispose ()
Focus (Disposing as boolean) :-
Focus means it can focus on the DateTimePicker. It’s focus on the control of the
DateTimePicker.
Ex. DateTimePicker1.Focus ()
Hide() :-
Hide means the DateTimePicker can’t show it. It can remove the data from the
DateTimePicker but do not delete from the memory.
Ex. DateTimePicker1.
Show() :-
Show means we can show the DateTimePicker. We can show the data in the
DateTimePicker.
Ex. DateTimePicker1.Show()
Refresh() :-
Refresh means to refresh or rebuild the DateTimePicker. It forces the
DateTimePicker to refresh its client area and immediately rebuild itself and any
child Controls.
Ex. DateTimePicker1.Refresh ()
FindForm() :-
It means it is find the DateTimePicker and rebuild it. It rewrite the form that the
control is on
Ex. DateTimePicker1.Findform()
Select (Directed As Boolean, Forward As Boolean) :-
Select means it can select or activates the DateTimePicker. It activates the
DateTimePicker.
Ex. DateTimePicker1.Select ()
Timer Control:-
The Timer Control plays an important role in the development of programs both
Client side and Server side development as well as in Windows Services. With
the Timer Control we can raise events at a specific interval of time without the
interaction of another thread.
We require Timer Object in many situations on our development environment.
We have to use Timer Object when we want to set an interval between events,
periodic checking, to start a process at a fixed time schedule, to increase or
decrease the speed in an animation graphics with time schedule etc.
A Timer control does not have a visual representation and works as a component
in the background.
Example:
private void button1_Click(object sender, EventArgs e)
{
timer1.Interval = 1000;
timer1.Start();
}
private void button2_Click(object sender, EventArgs e)
{
timer1.Stop();
}
❖ Properties :-
Name:-
It means to give unique name of Timer. To indicates the name used in code to
identify the Timer.
Ex. Timer1.Name= Timer1 (by default)
Enabled :-
Enabled means your Timer is enable or not. It indicates whether the Timer is
enabled.
Ex. Timer1.Enabled= True (by default)
Timer1.Enabled= False
GenerateMember :-
It means a variable which is generated for timer. It is use to generate member
variables.
Interval :-
Interval means the elapsed events in MilliSeconds. It is use to define interval of
miniseconds.
Ex. Timer1.Interval = 100(by default)
❖ Methods :-
Dispose() :-
It means the Timer is not in the memory and it is removed. It is used to remove
the data to the memory in the Timer.
Ex. Timer1.Dispose ()
Start() :-
It means to start timer. It is use to start the timer.
Ex. Timer1.Start()
Stop() :-
It means to stop the timer. It is use to stop the timer.
Ex. Timer1.Stop()
❖ Events :-
Tick :-
It means the specified timer interval has elapsed & timer is enabled. The Tick
event is use to elapse the time interval.
Timer1.Tick
Disposed :-
It occurs when the timer is disposed by a call. It is use to disposed a call which is
given by the timer.
Ex. Timer1.Disposed
RichTextBox Control: -
The RichTextBox control allows the user to display, enter, and edit text while
also providing more advanced formatting features than the conventional TextBox
control.
The RichTextBox control provides a number of properties you can use to apply
formatting to any portion of text within the control. To change the formatting of
text, it must first be selected. Only selected text can be assigned character and
paragraph formatting. Using these properties, you can make text bold or italic,
change the color, and create superscripts and subscripts. You can also adjust
paragraph formatting by setting both left and right indents, as well as hanging
indents.
In C#, RichTextBox control is a textbox which gives you rich text editing controls
and advanced formatting features also includes a loading rich text format (RTF)
files. Or in other words, RichTextBox controls allows you to display or edit flow
content, including paragraphs, images, tables, etc. The RichTextBox class is used
to represent the windows rich text box and also provide different types of
properties, methods, and events. It is defined under System.Windows.Forms
namespace.
❖ Properties -:-
Name:-
It means to give unique name of RichTextBox. To indicates the name used in
code to identify the RichTextBox.
Ex. RichTextBox1.Name = RichTextBox1 (by default)
Backcolor :-
It means you give any kind of background color to the RichTextBox. The
background color of the RichTextBox.
Ex. RichTextBox1.BackColor = Color. Blue
Forecolor :-
It means you give any type of foreground color to the RichTextBox. It is used to
display and changing the color of text.
Ex. RichTextBox1.Forecolor = Color. Red
BorderStyle :-
BorderStyle means to get or set the border style for the RichTextBox. You can
change the border style of the RichTextBox.
Ex. RichTextBox1.BorderStyle = BorderStyle.Fixed3D
There are 3 types of borderstyle ;
• Fixed3D
• FixedSingle
• None(default)
Font :-
Font means you can write text in different format in RichTextBox. It is used to
display and change the font type, style, and size of the RichTextBox.
Ex. Font type = Microsoft Suns Serif (by default)
Font style = Regular (by default)
Font size = 6 (by default)
Enabled :-
Enabled means your RichTextBox is enable or not. It indicates whether the
RichTextBox is enabled.
Ex. RichTextBox1.Enabled = True (by default)
RichTextBox1.Enabled = False
Visible :-
Visible means you can show or hide the RichTextBox. It displays the
RichTextBox is visible or hidden.
Ex. RichTextBox1.Visible = True ( by default)
RichTextBox1.Visible = False
Size :-
It means the size of height & width of RichTextBox. It is use to display the size
of the RichTextBox in pixels.
Ex. RichTextBox1.Size = 100.96 (by default)
TabIndex :-
It means that you can give the space in tab order. When index is displayed in the
tab order then the TabTndex will occupy.
Ex. RichTextBox1.TabIndex = 0 (by default)
❖ Methods :-
Dispose() :-
It means the RichTextBox is not in the memory and it is removed. It is used to
remove the data to the memory in the RichTextBox.
Ex. RichTextBox1.Dispose ()
Focus (Disposing as boolean) :-
Focus means it can focus on the RichTextBox.It’s focus on the control of the
RichTextBox.
Ex. RichTextBox1.Focus ()
Hide() :-
Hide means the RichTextBox that we can’t show it. It can remove the data from
the RichTextBox but do not delete from the memory.
RichTextBox1.Hide ()
Show() :-
Meaning: Show means we can show the RichTextBox.
Use = We can show the data in the RichTextBox.
Syn. & Ex. RichTextBox1.Show()
Refresh() :-
Refresh means to refresh or rebuild the RichTextBox. It forces the RichTextBox
to refresh its client area and immediately rebuild itself and any child Controls.
Ex. RichTextBox1.Refresh ()
FindForm() :-
It means it is find the RichTextBox and rebuild it. It rewrite the form that the
control is on.
Ex. RichTextBox1.Findform()
Select (Directed As Boolean, Forward As Boolean) :-
Select means it can select or activates the RichTextBox. It activates the
RichTextBox.
Ex. RichTextBox1.Select ()
Clear() :-
It Means it clears the text from RichTextBox. It clears all text from the
RichTextBox.
Ex. RichTextBox1.Clear()
Copy() :-
It means it copies some text from the RichTextBox. It use to copy the current
selection in the RichTextBox to the clipboard.
Syn. & Ex. RichTextBox1.Copy()
Crystal Report:-
Crystal reports allow the user to generate reports by summarizing the data. It also
allows the user to present data in a graphical format.
Crystal Report is a Reporting Application that can generate reports from various
Data Sources like Databases , XML files etc.. The Visual Studio.NET Integrated
Development Environment comes with Crystal Reports tools. The Crystal
Reports makes it easy to create simple reports, and also has comprehensive tools
that you need to produce complex or specialized reports in csharp and other
programming languages.
Crystal Reports is compatible with most popular development environments
like C# , VB.NET etc. You can use the Crystal Reports Designer in Visual Studio
.NET to create a new report or modify an existing report.
From the following sections you can find useful resources for generating
customized reports from Crystal Reports with C# .
1. Add Typed DataSet to the project
This article makes use of Disconnected Architecture to connect Crystal Reports
with Database and hence Typed DataSets will be used.
By default all the columns are of String Data Type but you can also change the
data type as per your need.
4. Add Crystal Report to the project
Now you will need to add a Crystal Report to the project. You can give it name
as per your choice.
As soon as you click OK you get the following dialog. You must select Using the
Report Wizard option.
Once you press OK in the above dialog, the Report Wizard starts and you get the
following dialog where you need to choose the type of Database connection for
your Crystal Report. Since we are using DataSet we will choose the Customers
DataSet.
Next the Wizard will ask for the Columns or Fields from the Customer DataSet
you need to display on the Crystal Reports. You can choose either all or specific
fields as per you choice.
Once you click Finish your Crystal Report should look as below.
Once you add the CrystalReportViewer control to the Form, your Form must look
as below.