How to change fonts depending on data

Stimulsoft Reports.NET discussion
Post Reply
Sacha
Posts: 19
Joined: Tue Mar 27, 2007 11:21 am
Location: Montréal

How to change fonts depending on data

Post by Sacha »

Hi, I'm new to this tool (and still evaluating) and i need a few things more before making a final recommendation.
What is left unanswered for now is :

1- We need to be able to custom the fonts of our data depending of it's value. Probably something like putting ~S~ before a value we want to have as struckout in the report and ~U~ before a value we want as underlined.

I now need to understand how i can scan my values for ~S~ and ~U~, change the field font and remove ~S~ ~U~ from the value for each value.

the code from another tool looked like this :
private void MyReport_PrintSection(object sender, C1.Win.C1Report.ReportEventArgs e)
{
if (e.Section == SectionTypeEnum.Detail)
{
foreach (Field f in _c1r.Sections[SectionTypeEnum.Detail].Fields)
{
if (f.Value.ToString().IndexOf('~') != -1)
{
string fvalue = f.Value.ToString();
int start = fvalue.IndexOf('~');
int end = fvalue.IndexOf('~', start+1);

string code = fvalue.Substring(start+1, end-start-1);
f.Value = fvalue.Substring(end+1);

if(code == "S" || code == "s")
f.Font = new Font(f.Font, System.Drawing.FontStyle.Strikeout);
else if(code == "U" || code == "u")
f.Font = new Font(f.Font, System.Drawing.FontStyle.Underline);
}
}
}
}


2- We will be using images from our ressource files and using and xml from memory instead of from a file, could that be an issue ?

And i think that's about it. For now, this tool looks like the best in the market to me :)

Sacha
Vital
Posts: 1278
Joined: Fri Jun 09, 2006 4:04 am

How to change fonts depending on data

Post by Vital »

1. You can use following code in GetValueEvent:

Code: Select all

StiText textComp = sender as StiText;

if (textComp.Text.Value.StartWith("~S~"))text.Font = new Font(textComp.Font, .FontStyle.Bold);
else if (textComp.Text.Value.StartWith("~I~"))text.Font = new Font(textComp.Font, FontStyle.Italic);
else if (textComp.Text.Value.StartWith("~U~"))text.Font = new Font(textComp.Font, FontStyle.Underline);
2. You can use BeforePrintEvent of Image component:

Code: Select all

Image1.Image = yourImage;
You can get yourImage from any source using c#.

Thank you.
Sacha
Posts: 19
Joined: Tue Mar 27, 2007 11:21 am
Location: Montréal

How to change fonts depending on data

Post by Sacha »

Thanks Vital for the answer, I just need to know where to hook to that GetValueEvent.

It needs to be something generic for all report components. I looked around, haven't found something i though would do.
I would have expected something on the report class or something.

Can you point me on where is the right event ?

Thanks
Brendan
Posts: 309
Joined: Sun Jul 16, 2006 12:42 pm
Location: Ireland

How to change fonts depending on data

Post by Brendan »

Select the Text Component on the Report Page and View the Properties Window.

Click the Event Button in the Properties Window (looks like a Yellow lightning bolt at the top of the Properties window under the ComboBox).

Put Code behind the Get Value Event
Sacha
Posts: 19
Joined: Tue Mar 27, 2007 11:21 am
Location: Montréal

How to change fonts depending on data

Post by Sacha »

Thanks Brendan, I think it could do the trick, but that not really something that would be appropriate for what we are doing.
We are using Objectivity, a non SQL and non relational object database. We will be printing lots of single value strings in
our reports. You can look at it like a table with one row and LOTS of columns.

We don't want to have to get into all the fields of all the reports we will make to add the formating code otherwise it would not be convivial at all.

There's has to be a way to do it from the outside of the report. Something like :

myReportObject.GetComponents().OnGetValue += SomeEventHandler(SomeStiComponent sender, SomeStiArgs args)
void SomeEventHandler(sender, args)
{
figure if sender is StiText
do formating of sender
}

or something like

myReportObject.OnComponentsGotTheirValues += SomeEventHandler(SomeStiComponent sender, SomeStiArgs args)
void SomeEventHandler(sender, args)
{
foreach(StiComponent comp in myReportObject.GetMyStiComponentWithTheirValues)
{
figure if comp is StiText
do formating of sender
}
}


One way or another is fine as long as i can format it in a generic way from the outside of the report and that i can access
1- the value
2- the stitext containing that value so i can apply the format.

That's about the only thing we are still stuck before being able to finalize our recommendation. We need to be able to explain that in order to have all our elements to start the project and buy the report tool.

This software seems to be real good and i'm sure there as to be way to do something as simple as that, but i just cant figure it out yet, and customizing each and every fields we will put on the report is not really an option.

Thanks for your time :)



Sacha
Posts: 19
Joined: Tue Mar 27, 2007 11:21 am
Location: Montréal

How to change fonts depending on data

Post by Sacha »

right now, i could come up with something like this :

stiReport1.Load("stiReport1 Design.mrt");
DataSet data = new DataSet();
data.ReadXml("DeliverySummary.xml");
stiReport1.RegData(data);

foreach(StiComponent comp in stiReport1.GetComponents())
{
if(comp is StiText)
{
StiText textComp = comp as StiText;

if (textComp.Text.Value.StartsWith("~S~"))textComp.Font = new Font(textComp.Font, FontStyle.Bold);
else if (textComp.Text.Value.StartsWith("~I~"))textComp.Font = new Font(textComp.Font, FontStyle.Italic);
else if (textComp.Text.Value.StartsWith("~U~"))textComp.Font = new Font(textComp.Font, FontStyle.Underline);
}
}

But it does absolutely nothing since textComp.Text.Value doesn't contain my value but something that refers to it.
And I still don't see the simple way to get that value.
Should I apply something else before getting the values ?

Per example, i find :
"{DataSource1.HospitalName}" where i want to find "~U~St-Vincent Birthing pavillon"

Any ideas ?
Brendan
Posts: 309
Joined: Sun Jul 16, 2006 12:42 pm
Location: Ireland

How to change fonts depending on data

Post by Brendan »

Hi Sacha,
The following will work but I'm not sure its the "Best" or most correct way to go about it:

Code: Select all

private void LoadMyReport()
{
	StiReport stiReport1 = new StiReport();
	stiReport1.Load("stiReport1 Design.mrt");

	DataSet data = new DataSet();
	data.ReadXml("DeliverySummary.xml");
	
	stiReport1.RegData(data);

	stiReport1.Compile();

	foreach(StiComponent comp in stiReport1.CompiledReport.GetComponents())
	{
		if(comp is StiText)
		{
			StiText txtControl = comp as StiText;
			txtControl.GetValue += new Stimulsoft.Report.Events.StiGetValueEventHandler(txtControl_GetValue);
		}
	}

	stiReport1.Show();
}

private void txtControl_GetValue(object sender, Stimulsoft.Report.Events.StiGetValueEventArgs e)
{
	StiText txtControl = sender as StiText;
	if(e.Value.StartsWith("~U~"))
	{
		e.Value = e.Value.Substring(3, e.Value.Length - 3);
		txtControl.Font = new Font(txtControl.Font.Name, txtControl.Font.Size, FontStyle.Underline);
	}
	else if (e.Value.StartsWith("~S~"))
	{
		e.Value = e.Value.Substring(3, e.Value.Length - 3);
		txtControl.Font = new Font(txtControl.Font.Name, txtControl.Font.Size, FontStyle.Bold);
	}
	else if (e.Value.StartsWith("~I~"))
	{
		e.Value = e.Value.Substring(3, e.Value.Length - 3);
		txtControl.Font = new Font(txtControl.Font.Name, txtControl.Font.Size, FontStyle.Italic);
	}
}
Regards,
Brendan
Vital
Posts: 1278
Joined: Fri Jun 09, 2006 4:04 am

How to change fonts depending on data

Post by Vital »

We have added three global events:

Code: Select all

StiOptions.Engine.GlobalEvents.GetValue
StiOptions.Engine.GlobalEvents.BeforePrint
StiOptions.Engine.GlobalEvents.AfterPrint
you can add one event handler for all text object:

Code: Select all

StiOptions.Engine.GlobalEvents.GetValue += new Stimulsoft.Report.Events.StiGetValueEventHandler(OnGetValue);

private void test(object sender, Stimulsoft.Report.Events.StiGetValueEventArgs e)
{
      e.Value = "test";
}
Update will be included in official release of 2007.1 (today).

Thank you.
Post Reply