Print Page | Close Window

Change Text Object at Runtime

Printed From: Crystal Reports Book
Category: Crystal Reports for Visual Studio 2005 and Newer
Forum Name: Writing Code
Forum Discription: .NET programming API, report integration
URL: http://www.crystalreportsbook.com/forum/forum_posts.asp?TID=455
Printed Date: 25 Apr 2024 at 7:17am


Topic: Change Text Object at Runtime
Posted By: BernieHunt
Subject: Change Text Object at Runtime
Date Posted: 03 Apr 2007 at 8:36pm
I'm having a terrible time getting the syntax for this. I have a Crystal report embedded in my VS05 VB.net application. I create a dataset using multiple database queries. I think instantiate a crViewer and load my report into the viewer and load my dataset into the report. All of this works.
 
I now want to add changing some of the text items on the report before I display it.
 
So far I have;
 
    frmReportForm = New frmReportViewer
    crThisReport = New Pat_Statement
    crThisReport.SetDataSource(dsPatData)
  
    crThisReport.ReportDefinition.ReportObjects.Item["Text12"] = _
         "mySting info"
 
This last line is where I'm having the syntax problem. In this example, Text12 is the name of the text object on the report.
 
Any anyone point me in the right direction or point me to a good resource that has how to access all the objects on a report?
 
I looked all through BO's documentation and there wasn't much on highly embedded reports in VS.
 
Thanks,
Bernie
 



Replies:
Posted By: BrianBischof
Date Posted: 03 Apr 2007 at 10:29pm
My book gives you a reference on the ReportDocument object model. It is for VS 2003, but the object model is about 95% the same. So this should be a big help for making your runtime changes.

For your formula you need to cast the object as a TextObject. Then assign the string to the .Text property. That should clear up your errors. Chapter 15 has lots of source code if you plan on doing more runtime changes like this.


-------------
Please support the forum! Tell others by linking to it on your blog or website:<a href="http://www.crystalreportsbook.com/forum/">Crystal Reports Forum</a>


Posted By: BrianBischof
Date Posted: 03 Apr 2007 at 10:30pm
oh yeah, the link for more info:
http://www.crystalreportsbook.com/ProgrammingGuide.asp - http://www.crystalreportsbook.com/ProgrammingGuide.asp


-------------
Please support the forum! Tell others by linking to it on your blog or website:<a href="http://www.crystalreportsbook.com/forum/">Crystal Reports Forum</a>


Posted By: BernieHunt
Date Posted: 04 Apr 2007 at 6:45am
Argh! Still striking out with
 

((CrystalDecisions.Shared.ReportObjectKind.TextObject)crThisReport.ReportDefinition.ReportObjects.Item("Text12")).text = "new text"

I ordered the book so I guess I'll have to wait a week to work on this some more.

Bernie


Posted By: BrianBischof
Date Posted: 04 Apr 2007 at 9:48am
You're code isn't making sense. You're cast has too many parentheses on the left and is C# syntax, but your array indexer is VB.NET because it uses a parentheses instead of a square bracket. Which language are you using?

If VB.NET, then use CType() to cast it. If using C# then use ReportObjects["Text12"].Text to reference the array.

You have the right idea, but the code is buggy.


-------------
Please support the forum! Tell others by linking to it on your blog or website:<a href="http://www.crystalreportsbook.com/forum/">Crystal Reports Forum</a>


Posted By: BernieHunt
Date Posted: 04 Apr 2007 at 10:17am
Argh!
 
Hahahaha, the parentheses syntax is actually right for Java, the problem is I'm writing in VB. I hate switching languages mid week.
 
Thanks and I'm looking forward to the book.
 
Also, would your Crystal Reports Encyclopedia be of interest? I work only with imbeded reports.
 
Thanks,
Bernie
 


Posted By: BrianBischof
Date Posted: 04 Apr 2007 at 10:35am
Yes, using multiple languages can make things confusing.

Volume 1 of the CR Encyclopedia will not address ANY programming topics at all. It is strictly an XI book. Volume 2 is 100% VS 2005. So Volume 2 is the one you'll want.

However, I'm going to have a lot more report samples in Volume 1 and if that appeals to you then you can check that out as well. But again, it has no code in it.


-------------
Please support the forum! Tell others by linking to it on your blog or website:<a href="http://www.crystalreportsbook.com/forum/">Crystal Reports Forum</a>


Posted By: craigb
Date Posted: 18 Apr 2007 at 8:11am

'Load report first
MyReport.Load(reportPath)


'Change text of a tet object
Dim myTextObjectOnReport As CrystalDecisions.CrystalReports.Engine.TextObject


myTextObjectOnReport = CType(MyReport.ReportDefinition.ReportObjects.Item("NameOfTextObject"), CrystalDecisions.CrystalReports.Engine.TextObject)

myTextObjectOnReport.Text = "Text you want for text object"


Posted By: MarkM
Date Posted: 01 May 2007 at 9:03am
Has anyone got this to work using VB.Net 2005 & CRXI R2?
I have seen this solution on several sites and have tried it in the form load event and with a button on the form after the CRviewer has loaded (below)...no joy.  The variable "myTextObjectOnReport" text changes no problem but it has no affect on the textObject on the report....any thoughts?
 
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim myTextObjectOnReport As CrystalDecisions.CrystalReports.Engine.TextObject

myTextObjectOnReport = CType(CustOrder1.ReportDefinition.ReportObjects.Item("txtName"), CrystalDecisions.CrystalReports.Engine.TextObject)

myTextObjectOnReport.Text = "isThisWorking"

End Sub



Posted By: BrianBischof
Date Posted: 01 May 2007 at 10:05am
I'm concerned about putting this in the Click event of a button. Has the report already been displayed to the user and then the user clicks on the button? Or are they clicking on the button and then something else displays the report? My theory is that your object reference isn't working either because the report is already displayed or it gets displayed later and the object reference is lost.

-------------
Please support the forum! Tell others by linking to it on your blog or website:<a href="http://www.crystalreportsbook.com/forum/">Crystal Reports Forum</a>


Posted By: MarkM
Date Posted: 01 May 2007 at 11:02am
I was trying anything at that point, I have put the same code on the Load event, Initialize event, even in the RefreshRepot event ...nothing. 
 
Whoops...I just caught my mystake. I dimensioned a new instance of my report "Dim Report as New CustOrder" and then forgot to reference it when I cast it back to myTextObject.
 
(wrong) myTextObject = CType(CustOrder1.ReportDefinition...
 
(right) myTextObject = CType(Report.ReportDefinition...
 
I've only been looking at this code for a day and a half now....geeez

 

 


Posted By: BrianBischof
Date Posted: 01 May 2007 at 11:31am
Glad you found it. I figured it would be something funky like that.

-------------
Please support the forum! Tell others by linking to it on your blog or website:<a href="http://www.crystalreportsbook.com/forum/">Crystal Reports Forum</a>


Posted By: shant1976
Date Posted: 06 Nov 2009 at 7:44am
Here is the c# version:-

http://prabhat.me/2009/11/06/change-the-value-of-itextobject-of-crystal-report-at-runtime/



Print Page | Close Window