Page 1 of 1
Adding TextFormat at runtime
Posted: Mon Oct 05, 2009 12:27 pm
by xss
Hi,
is it possible to register a text format? F.e. I would like to declare a text format called "VarDec" (#,##0.##) at startup and use it in the Designer.
Regards
Adding TextFormat at runtime
Posted: Tue Oct 06, 2009 2:19 pm
by Jan
Hello,
You can do this, but this is complex task. At first you need create new class which inherited from StiFormatService:
Code: Select all
[TypeConverter(typeof(Stimulsoft.Report.Components.TextFormats.Design.StiCustomFormatConverter))]
[StiFormatEditor("Stimulsoft.Report.Components.TextFormats.Design.StiCustomEditor, Stimulsoft.Report.Design, " + StiVersion.VersionInfo)]
[StiWpfFormatEditor("Stimulsoft.Report.WpfDesign.StiCustomEditor, Stimulsoft.Report.WpfDesign, " + StiVersion.VersionInfo)]
[StiServiceBitmap(typeof(StiCurrencyFormatService), "Stimulsoft.Report.Images.TextFormats.FormatCustom.png")]
public class StiCustomFormatService : StiFormatService
{
///
/// Gets a service name.
///
public override string ServiceName
{
get
{
return StiLocalization.Get("FormFormatEditor", "Custom");
}
}
public override int Position
{
get
{
return 100;
}
}
///
/// Gets value to show a sample of formatting.
///
public override object Sample
{
get
{
return string.Empty;
}
}
///
/// Gets or sets string of formatting.
///
[DefaultValue("")]
public override string StringFormat
{
get
{
return base.StringFormat;
}
set
{
base.StringFormat = value;
}
}
///
/// Creates a new format of the type StiCustomFormatService.
///
public StiCustomFormatService() : this(string.Empty)
{
}
///
/// Creates a new format of the type StiCustomFormatService.
///
/// String of formatting
public StiCustomFormatService(string stringFormat)
{
this.StringFormat = stringFormat;
}
}
After then you need add type converter:
Code: Select all
public class StiCustomFormatConverter : TypeConverter
{
public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context,
object value, Attribute[] attributes)
{
return TypeDescriptor.GetProperties(value, attributes);
}
public override bool CanConvertTo(ITypeDescriptorContext context,
Type destinationType)
{
if (destinationType == typeof(InstanceDescriptor))return true;
return base.CanConvertTo(context, destinationType);
}
public override bool GetPropertiesSupported(ITypeDescriptorContext context)
{
return true;
}
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture,
object value, Type destinationType)
{
if (destinationType == typeof(InstanceDescriptor) && value != null)
{
StiCustomFormatService service = (StiCustomFormatService)value;
Type[] types = new Type[]{ typeof(string)
};
ConstructorInfo info = typeof(StiCustomFormatService).GetConstructor(types);
if (info != null)
{
object[] objs = new object[] {
service.StringFormat
};
return new InstanceDescriptor(info, objs);
}
}
return base.ConvertTo(context, culture, value, destinationType);
}
}
Also you need specify editor for custom service. I have attached editor for StiCustomFormat. After then you need register new service in services collection:
Code: Select all
StiCustomFormatService myCustomFormatService = new StiCustomFormatService();
StiConfig.Services.Add(myCustomFormatService);
Thank you.
Adding TextFormat at runtime
Posted: Wed Oct 07, 2009 3:48 am
by xss
Thank you Jan!
It works fine, but it's a bit too risky for me. If there will be major class changes I have to adopt them and that's something i don't want. I am going to use an user defined mask in the dictionary.
Best Regards
Adding TextFormat at runtime
Posted: Thu Oct 08, 2009 12:10 am
by Jan
Hello,
You can use Custom format mark in text formating. Please check attached image. You can use variable to provide format mask.
Thank you.
Adding TextFormat at runtime
Posted: Thu Oct 08, 2009 2:41 am
by xss
ya, solved it that way. :feelgood:
Thanks.
Adding TextFormat at runtime
Posted: Fri Oct 09, 2009 12:15 am
by Jan
Hello,
Please contact us if you need any help.
Thank you.