0% found this document useful (0 votes)
12 views23 pages

Unit 4

Unit 4 covers advanced features in C#, focusing on ArrayLists, exception handling, and various controls like DateTimePicker, Timer, and RichTextBox. It explains the dynamic nature of ArrayLists, methods for managing exceptions, and properties and methods for each control. The document provides practical examples and descriptions of how to implement these features in C# applications.

Uploaded by

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

Unit 4

Unit 4 covers advanced features in C#, focusing on ArrayLists, exception handling, and various controls like DateTimePicker, Timer, and RichTextBox. It explains the dynamic nature of ArrayLists, methods for managing exceptions, and properties and methods for each control. The document provides practical examples and descriptions of how to implement these features in C# applications.

Uploaded by

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

1

Unit – 4 (Advance Feature)

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

Creat By : kuldeep padhya


2
Unit – 4 (Advance Feature)
2. a1.add(“Example”) – This will add a String value to the collection
3. a1.add(true) – This will add a Boolean 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.

Creat By : kuldeep padhya


3
Unit – 4 (Advance Feature)
Remove()/RemoveRange() Remove() method removes the specified
element from the ArrayList.
RemoveRange() method removes a range of
elements from the ArrayList.
RemoveAt() Removes the element at the specified index from
the ArrayList.
Sort() Sorts entire elements of the ArrayList.
Reverse() Reverses the order of the elements in the entire
ArrayList.
Contains Checks whether specified element exists in the
ArrayList or not. Returns true if exists otherwise
false.
Clear Removes all the elements in ArrayList.
CopyTo Copies all the elements or range of elements to
compitible Array.
GetRange Returns specified number of elements from
specified index from ArrayList.
IndexOf Search specified element and returns zero based
index if found. Returns -1 if element not found.
ToArray Returns compitible array from an ArrayList.

Creat By : kuldeep padhya


4
Unit – 4 (Advance Feature)

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.

Creat By : kuldeep padhya


5
Unit – 4 (Advance Feature)
finally − The finally block is used to execute a given set of statements, whether
an exception is thrown or not thrown. For example, if you open a file, it must be
closed whether an exception is raised or not.
throw − A program throws an exception when a problem shows up. This is done
using a throw keyword.
Syntax
try {
// statements causing exception
} catch( ExceptionName e1 ) {
// error handling code
} catch( ExceptionName e2 ) {
// error handling code
} catch( ExceptionName eN ) {
// error handling code
} finally {
// statements to be executed
}
Example:
try
{
Int a = 10;
Int b = 0;
Int c = a/b; //error generated
}
catch
{
Messagebox.show(“zero is not divisible by zero”);
}

Creat By : kuldeep padhya


6
Unit – 4 (Advance Feature)

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.

Error Provider Control:


“Error Provider Control allows user to set an error message for any control in the
form when the input is not valid.”
ErrorProvider allows us to set an error message for any control on the form when
the input is not valid. When an error message is set, an icon indicating the error
will appear next to the control and the error message is displayed as Tool Tip
when the mouse is over the control.

Creat By : kuldeep padhya


7
Unit – 4 (Advance Feature)

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.

Creat By : kuldeep padhya


8
Unit – 4 (Advance Feature)

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.

DateTimePicker, as a drop-down list with a date represented in the text, or as a calendar


which appears when you click on the down-arrow next to the given list.

❖ 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)

Creat By : kuldeep padhya


9
Unit – 4 (Advance Feature)
MaxDate :-
Property used to set or get the maximum date that can be selected using the
control. It is use to search the maximum date.
MinDate :-
Property used to set or get the minimum date that can be selected using the
control. It is use to search the minimum date.
Width :- Property set width of the DateTimePicker.
Ex. DateTimePicker1.Width = “40”

❖ 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()

Creat By : kuldeep padhya


10
Unit – 4 (Advance Feature)

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 ()

Creat By : kuldeep padhya


11
Unit – 4 (Advance Feature)

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.

How to use Timer Control :


We can control programs with Timer Control in millisecond , seconds, minutes
and even in hours. The Timer Control allows us to set Interval property in
milliseconds. That is, one second is equal to 1000 milliseconds. For example, if
we want to set an interval of 1 minute we set the value at Interval property as
60000, means 60x1000 .
By default the Enabled property of Timer Control is False. So before running the
program we have to set the Enabled property is True , then only the Timer Control
starts its function.
Example:
private void timer1_Tick(object sender, EventArgs e)
{
label1.Text = DateTime.Now.ToString();
}

Creat By : kuldeep padhya


12
Unit – 4 (Advance Feature)

Start and Stop Timer Control:


The Timer control have included the Start and Stop methods for start and stop the
Timer control functions. The following C# program shows how to use a timer to
write some text to a text file each seconds. The program has two buttons, Start
and Stop. The application will write a line to a text file every 1 second once the
Start button is clicked. The application stops writing to the text file once the Stop
button is clicked.

Example:
private void button1_Click(object sender, EventArgs e)
{
timer1.Interval = 1000;
timer1.Start();
}
private void button2_Click(object sender, EventArgs e)
{
timer1.Stop();
}

Creat By : kuldeep padhya


13
Unit – 4 (Advance Feature)

❖ 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()

Creat By : kuldeep padhya


14
Unit – 4 (Advance Feature)

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

Creat By : kuldeep padhya


15
Unit – 4 (Advance Feature)

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.

Creat By : kuldeep padhya


16
Unit – 4 (Advance Feature)

❖ 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)

Creat By : kuldeep padhya


17
Unit – 4 (Advance Feature)

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 ()

Creat By : kuldeep padhya


18
Unit – 4 (Advance Feature)

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()

Creat By : kuldeep padhya


19
Unit – 4 (Advance Feature)

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.

2. Adding DataTable to the Typed DataSet


Our next step would be to add a DataTable to the Type DataSet.

Creat By : kuldeep padhya


20
Unit – 4 (Advance Feature)

3. Adding Columns or fields to DataTable


In the DataTable we need to specify the column names that we want to display in
the Crystal Report.

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.

Creat By : kuldeep padhya


21
Unit – 4 (Advance Feature)

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.

Creat By : kuldeep padhya


22
Unit – 4 (Advance Feature)

Once you click Finish your Crystal Report should look as below.

5. Adding Crystal Report Viewer to the Form


In order to display the Report, we will need to add CrystalReportViewer control
to the Form from the Toolbox

Creat By : kuldeep padhya


23
Unit – 4 (Advance Feature)

Once you add the CrystalReportViewer control to the Form, your Form must look
as below.

6. Populating the Crystal Report from Database


Inside the Form Load event, first the Customers DataSet is populated with records
from the Customers Table.
The Customers DataSet is set as a DataSource for the Crystal Report.
Finally, the Crystal Report is set as ReportSource for the CrystalReportViewer
control.

Creat By : kuldeep padhya

You might also like