You are on page 1of 13

User Control, Crystal Report & Setup Programming With C#.

NET Unit : 5

PROGRAMMING WITH C#.NET


B.C.A. Semester: 4th

Unit : 5th
User Controls, Crystal Reports & Project Setup

Dr. Bhavesh K. Lukka


Dr. V. R. Godhaniya College of I.T. – Porbandar

Page | 2
User Control, Crystal Report & Setup Programming With C#.NET Unit : 5

Introduction

You can create your own controls if you don't find what you want from the list of predefined
controls available in .NET. You may need to create your own control because the controls that
are already available for you are very general and you might need a control which will have a
specific feature. These custom controls can then be placed in the toolbar together with other
controls.

There are two types of user defined controls, user controls and custom controls.

User control is composed of preexisting controls and is very easy to create.

Custom controls are created from scratch; therefore, you need to define a lot of
functionalityand how the custom control will render.

User controls inherit from the UserControl class which is a derive class of the Control class that
other control uses. You won’t have access to the properties and methods of the components
that you will include in a user control because only properties and methods of the UserControl
class is exposed to you. But you can define properties and methods that will communicate to
the components of the user control.

Example of Custom User Control

We will be creating an EditableLabel control.

Step 1:
Open Visual Studio and create a new project. From the list of templates, choose Windows
Forms Control Library and name it EditableLabel.

Page | 3
User Control, Crystal Report & Setup Programming With C#.NET Unit : 5

Step 2:
You will have a blank canvas as displayed below:

It looks just like a Windows Forms but it has no frame. This is because you are designing a user
control. Any control you place inside it will be part of the user control.

Click the canvas and change its Name property toEditableLabel and its AutoSize property
to True.

Drag a label to the canvas and change its Text property toLabel and its Name property
to labelDisplay. Resize the canvas so that it fits the label inside it.

Adding Properties
Since we are creating a user control, then it will only contain properties and events that
the UserControlclass offers. It means there is no way for the user to access the properties of
the Label control we have added to the form. Although the UserControl class has a Text propety
which it inherits from Control, we need a more functionality involving the text inside our label.
To add a property to a user control, we simply need to add a property to the class of our user
control. While in Design View, press F7 to go to the Code Editor. Inside the EditableLabel class,
add the following property:

public override string Text


{
get {return labelDisplay.Text; }
set {labelDisplay.Text = value; }
}

Note that we also used the override keyword because the Text property already exist in the User
Control.

Page | 4
User Control, Crystal Report & Setup Programming With C#.NET Unit : 5

Adding Event Handlers


Now it’s time to add an event handler to our user control. Recall
that our EditableLabel control should transform into a textbox
Once the user double clicks it. Therefore, we need to add an
eventhandler to its DoubleClick event.
Since we only have one control inside the canvas, then select the
Label. Then go to the Event section of the Properties Window
and find the DoubleClick property and double click it. That will
createa new event handler for the DoubleClick property.
We need to add a TextBox control to our user control. Add the following fields inside the
EditableLabel class.

Compiling the User Control


We are now ready to compile our EditableLabel user control. To do that simply go to the menu
and choose Build and then Build Solution. The compiling will create a file with a .dll extension
which contains your user control.

Use Control in Windows Project


Choose Toolbox Items Window, click the Browse button.
We need to browse for the .dll file containing our user
control. Browse for the directory where your project
was saved. Go inside the folder of our Windows Control
Library project (named EditorLabel) and inside it, enter
the bin folder.

.dll file could either be in the Debug or Release folder.


Once you found the EditorLabel.dll, select it and click
Open. EditorLabel will now show up in the list of selectable controls inside the Choose Toolbox
Item Window. Be sure it’s checked and then click OK. Now you will be able to see our user
control inside the Toolbox.

Drag an EditableLabel to the form. You can also find


the Text property in the Properties Window since we added
the Browsable attribute to that property.

Page | 5
User Control, Crystal Report & Setup Programming With C#.NET Unit : 5

Code for EditableLable


We will be creating an EditableLabel control. It will exactly look at a lable but when the user
double clicks it, it will transform into an editable textbox containing the current text of the
label. You can then edit its contents and when the user presses enter, it will transform back into
a label containing the edited text.
public TextBox MyText = new TextBox();
public EditableLabel()
{
InitializeComponent();
this.Controls.Add(MyText);
MyText.KeyDown += new KeyEventHandler(MyText_KeyDown);
MyText.Hide();
}

void MyText_KeyDown(object sender, KeyEventArgs e)


{
if (e.KeyCode == Keys.Enter)
{
MyLable.Show();
MyText.Hide();

MyLable.Text = MyText.Text;
}
}
public override string Text
{
get
{
return MyLable.Text ;
}
set
{
MyLable.Text = value;
}
}
private void MyLable_DoubleClick(object sender, EventArgs e)
{
MyText.Text = MyLable.Text;
MyText.Show();
MyText.Focus();
MyLable.Hide();
}

Page | 6
User Control, Crystal Report & Setup Programming With C#.NET Unit : 5

Crystal Reports
 Crystal Reports is officially a member of the Visual Studio .NET product.
 It is included in all major editions and ships in all languages available with Visual Studio
.NET. Crystal Reports .NET provides developers with the fastest, most productive way to
create and integrate presentation-quality, interactive reports that scale to meet the
demands of end users.
 Crystal Reports .NET is a very powerful report writer. It offers a very well defined object
model that provides flexibility when integrating reports into Windows and Web
applications. It also comes with a highly customizable viewer component that gives
developers control on how their reports are presented to users.

Integrated into .NET IDE


Crystal Reports .NET has been completely re-written from the ground-up in C#. It is tightly
integrated into the Visual Studio .NET IDE. Developers no longer have to switch to a different
application to create and edit reports.

Crystal Reports Viewer


Crystal Reports .NET provides developers with two different report viewing components. The
first can be used for displaying reports in Windows forms. The other is a component that can be
used to display reports in a Web environment. The beauty of these viewers is in their simplicity.
Viewing a report is as simple as setting the ReportSource property of the respective viewer.

Allowing users to interact with the report


Developers can give users the ability to interact with their reports object via a programmable
API. Developers can change report properties by calling methods or setting properties of the
viewer object.

ADO.NET Integration
ADO.NET is now the lingua-franca of data interchange in the .NET world. Crystal Reports .NET
provides the ability to use ADO.NET datasets as the data source for reports.

Page | 7
User Control, Crystal Report & Setup Programming With C#.NET Unit : 5

Crystal Report Experts

You can use the Crystal Report Designer to create a report from scratch, or you can use one of
the Crystal Report Experts to help you through the design process. The following Report Experts
are included in Crystal Reports for Visual Studio .NET:

1. Standard
2. Cross-Tab
3. Mail Label

Standard Report

 The Standard Report Expert is the most generic


of the experts. It provides eight tabs, many of
which are common to the other Report Experts.
 The Standard Report Expert guides you through
the process of choosing a data source and
linking database tables. It also helps you add
fields and specify the grouping, summarization
(totals), and sorting criteria you want to use.
Finally, the Standard Report Expert leads you
through chart creation and record selection.
 The Style tab contains pre-defined layouts that you can apply to your report to give it
additional visual impact.

Cross-Tab Report

 The Cross-Tab Report Expert guides you through the creation of a report in which your data
is displayed as a cross-tab object. Three special tabs (Cross-Tab, Style, and Customize Style)
help you to create and format the cross-tab itself.

Mail Label Report

 The Mail Label Report Expert lets you create a report that is formatted to print on a mailing
label of any size. You can use the Label tab to select a commercial label type, or you can
define your own layout of rows and columns for any style of multi-column report.

Page | 8
User Control, Crystal Report & Setup Programming With C#.NET Unit : 5

Report Sections

Report Header – Appears at the top of the first page of the report.

Page Header – Appears after the Report Header on the first page. On all the remaining pages it appears
at the top of the page.

Group Header – Appears at the beginning of each new group.

Details – The row that displays the record information. There is usually one detail row for every record
in the table.

Group Footer – Appears after all records of a group have been printed.

Page Footer –Appears at the bottom of each page.

Report Footer – Appears as the bottom of the page for the last page in the report.

Formulas, Special Field and Summary in Report

Formulas

 The formula field in a Crystal report is a functionality provided to enhance computation of


values dynamically during the display/printing of records in a report of an application. Most
developers often fail to effectively explore the potentials of this Crystal Report functionality
and often preferred other report viewing modules.

 Formulas in Crystal report have two forms of syntax; Basic and Crystal. In this paper, a few
of the details on the syntax, data types, return values and case sensitivity will be
demonstrated. Formulas are always used to return a value or Insert a value where desired.

 When creating formulas, you have the option of using either Crystal or Basic syntax. Almost
any formula written with one syntax can be written with the other. Reports can contain
formulas that use Basic syntax as well as formulas that use Crystal syntax, but a single
formula can use only one syntax.

Page | 9
User Control, Crystal Report & Setup Programming With C#.NET Unit : 5

Special Fields

It provides general information, such as Page Numbers, Print


Date, and Report Comments, are located in the Special Fields
list.

In the above crystal report, I tried to display most of the special


fields that are commonly used in our real life:

There are various special fields which we can use for the
following purpose:

Print Date: Print Date Field is used to include the current date
when the report prints. This field can be placed in any section of
your report, depending on how often you want it to print. The
date can be changed in the Set Print Date and Time Dialog Box. I
have located print date field in the page header of the crystal
report in the above figure.

Record Selection Formula: Record Selection Formula Field is used to insert a selected record
into your report. (Use the Select Expert to create a record selection formula for your report.)
File Creation Date: File Creation Date Field is used to include a field that displays the date when
you created the report. I have located File creation field in the report footer of the crystal
report.

Record Number: Record Number Field is used to number each record printed in the Details
section of your report.

Page Number: Page Number Field is used to insert a field that prints the current page number.
These fields are most often placed in the Page Header or Page Footer sections. I have located
a Page Number field in the report footer of the crystal report.

Group Number: Group Number Field is used to number


each group in your report. You can place this field in
either the Group Header or Group Footer section of your
report. I have located a Group Number field in the group
header section of the crystal report.

Summary

To insert counts and subtotals of any filed you can use


Summary in Crystal Report. You can summarize your field
with maximum, minimum, count, etc…
Generally it place inReport Footer but you can specify the
location wherever you want.

Page | 10
User Control, Crystal Report & Setup Programming With C#.NET Unit : 5

Types of Setup Project


There are five different templates that are available when you select the project type 'Setup
and Deployment Projects'.

1. Setup Project: Creates a Windows Installer project to which files can be added. This
project type is the most generic of the five.
2. Web Setup Project: Creates a Windows Installer web project to which files can be
added.
3. Merge Module Project: Creates a Windows Installer Merge Module project to which
files can be added. This project type is most commonly used for using third party
products.
4. Setup Wizard: Creates a Windows Installer project with the aid of a wizard.
5. Cab Project: Creates a Cab project to which files can be added.

Creating Setup Project

File -> New -> Project -> Other Project Types -> Setup and Deployment -> Setup Project

Provide a name for the project as well choose the location where this project will be
established. Once you have done this click 'OK' and you should now see a screen like the
following.

Page | 11
User Control, Crystal Report & Setup Programming With C#.NET Unit : 5

File System Editor


1. Application Folder
This is main folder which is placed in c drive
program file.

2. User’s Desktop
Here you can set desktop short cut if
required.

3. User’s Program Menu


Here you can set files that will be put in
start->all programs.

The first thing we want to do here is to include the relevant files and folders we wish to install
on the user's machine.
This is accomplished by right clicking the Application Folder and selecting 'Add->Project
Output'. Click OK.

Page | 12
User Control, Crystal Report & Setup Programming With C#.NET Unit : 5

Create Shortcut

We could take additional steps such as manipulating the user's desktop with items such as a
shortcut; as well you can add a program group to the user's program group.

Name the shortcut as you want. I have given the name as Notepad. Drag and drop to User's
Desktop Folder

User Interface Editor


You can add dialogs if required.

Under Install right click on the 'Start' -> 'Add Dialog'

Page | 13
User Control, Crystal Report & Setup Programming With C#.NET Unit : 5

Launch Conditions Editor


If you are creating a Setup Project in Visual Studio 2008 (or VS2005) to deploy your new
application, you need to set which version of the .NET Framework you are targeting. If your
application is developed for .NET 2.0 you need to tell the Setup Project to check the target
machine for the .NET 2.0 Framework. By default the Setup project will check for .NET 3.5 (in
VS2008) even if your application is targeting .NET 2.0.

This Setup Project will now check the target machine for the .NET 2.0 Framework instead of the
.NET 3.5 Framework

Page | 14

You might also like