Adding TextFormat at runtime

Stimulsoft Reports.NET discussion
Post Reply
xss
Posts: 64
Joined: Wed Jun 10, 2009 3:03 am
Location: Austria

Adding TextFormat at runtime

Post 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

Jan
Posts: 1265
Joined: Thu Feb 19, 2009 8:19 am

Adding TextFormat at runtime

Post 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.
Attachments
243.StiCustomFormatConverter.cs
(2.87 KiB) Downloaded 256 times
242.StiCustomFormatService.cs
(3.42 KiB) Downloaded 245 times
241.StiCustomEditor.cs
(8.97 KiB) Downloaded 350 times

[The extension resx has been deactivated and can no longer be displayed.]

xss
Posts: 64
Joined: Wed Jun 10, 2009 3:03 am
Location: Austria

Adding TextFormat at runtime

Post 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
Jan
Posts: 1265
Joined: Thu Feb 19, 2009 8:19 am

Adding TextFormat at runtime

Post 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.
Attachments
244.Sample.png
244.Sample.png (182.79 KiB) Viewed 2114 times
xss
Posts: 64
Joined: Wed Jun 10, 2009 3:03 am
Location: Austria

Adding TextFormat at runtime

Post by xss »

ya, solved it that way. :feelgood:

Thanks.
Jan
Posts: 1265
Joined: Thu Feb 19, 2009 8:19 am

Adding TextFormat at runtime

Post by Jan »

Hello,

Please contact us if you need any help.

Thank you.
Post Reply