Introduction
In .NET the CyrstalReportViewer
control is a very powerful tool.
It�s supplied with most of the basics that people would require from it, except
for one, straight off the bat. How do I hide the StatusBar
? What
makes it even more baffling is that you can hide both the group tree and the
toolbar using the DisplayGroupTree
and
DisplayToolbar
properties respectively.
However, a DisplayStatusbar
property is nowhere to be
seen.
Deadends
Under help for the CrystalReportViewer
there is supposed to be
ViewerStatusBar
property, which gets the StatusBar
object. However, this doesn't appear to be exposed in either VB.NET or C#.
However, if you could get a hold of the StatusBar
object, then
you could set the Statusbar�s visible property to false, causing it to be
hidden.
So, how do I get hold of the StatusBar
then?
More Background
In a nutshell, the CrystalReportViewer
control is actually five
controls combined. Namely:
CrystalDecisions.Windows.Forms.PageView
. The main window
System.Windows.Forms.Splitter
. A splitter between the main
window and the Group Tree.
CrystalDecisions.Windows.Forms.ReportGroupTree
. A tree view to
allow you to drill down on the side of the report.
System.Windows.Forms.ToolBar
. The tool bar at the top that
allows you to do things like export the report, print, etc.
System.Windows.Forms.StatusBar
. And our Status bar that we
would like to hide.
The Answer
So knowing that there are actually five controls within the
CrystalReportViewer
, all we need to do is search through all these
controls. Find the Statusbar
, and then set its Visible
property to false. It�s that simple!
The Code
To set it up as a property for a UserControl
(see the source
project above), the code would looks like this:
Public Property DisplayStatusBar() As Boolean
Get
Dim obj As Object
Dim tempStatusbar As New StatusBar()
For Each obj In Me.CrystalReportViewer1.Controls
If obj.GetType Is tempStatusbar.GetType Then
Return CType(obj, StatusBar).Visible
Exit For
End If
Next
tempStatusbar.Dispose()
tempStatusbar = Nothing
End Get
Set(ByVal Value As Boolean)
Dim obj As Object
Dim tempStatusbar As New StatusBar()
For Each obj In Me.CrystalReportViewer1.Controls
If obj.GetType Is tempStatusbar.GetType Then
CType(obj, StatusBar).Visible = Value
Exit For
End If
Next
tempStatusbar.Dispose()
tempStatusbar = Nothing
End Set
End Property
The above code is less than ideal because the only way I could check the type
of the control was to create an instance of the StatusBar
, and then
compare the GetType
method returns. Ideally the code should look
something like this:
Set(ByVal Value As Boolean)
Dim obj As Object
For Each obj In Me.CrystalReportViewer1.Controls
If obj.GetType Is System.Windows.Forms.StatusBar Then
CType(obj, StatusBar).Visible = Value
Exit For
End If
Next
End Set
The reason why I couldn�t do it this way is that the obj.GetType Is
System.Windows.Forms.StatusBar
seems to be incompatible (System.Type vs
System.Windows.Forms.Type).
Anyway, if someone can get it to work without creating an extra instance of
StatusBar
, I�d love to hear from them.
Summary
I would like to repeat that this implementation is less than ideal. Then
again, it�s less than ideal that we have to do this in the first place.
Hopefully, this article is absoluted by the next version of .NET or even Crystal
9, where they may give you the property by default (here�s hoping). But to put
all that aside for a moment, it�s a good work around for hiding
the StatusBar
when displaying crystal reports in .NET!
I hope this helps someone out there!