Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Parse and Format Event Handlers, Where?
Message
 
À
09/12/2003 09:58:53
Information générale
Forum:
ASP.NET
Catégorie:
The Mere Mortals .NET Framework
Divers
Thread ID:
00857185
Message ID:
00857368
Vues:
18
Essentially, yes.

Here is how I used it to create a subclassed control that has a format property, this is standard stuff missing from the .NET base controls that I was able to add to all our application controls. The basic idea is to create event handlers on you application base class and then just override them as required on any subclasses. The aTextBoxCreditCardExpiration class is an example of what you can create out of it, ready for you to drag and drop onto your form. Use at your own risk!

Actually, in looking at the code now I see that for most simple subclasses you can just set the format property. In this example I actually had to override the parse event because I wanted to accept a variety of input formats.
//application level subclass from Kevin's stuff
	public class ammTextBox : OakLeaf.MM.Main.Windows.Forms.mmTextBox 
	{

		private String _format;
		[Description("Format for the value"),Category("Data")]
		public String Format
		{
			get { return _format; }
			set { _format = value; }
		}

		//This stuff is here to support formatting string in different cultures
		private IFormatProvider _formatInfo;

		[Description("Format provider"),Category("Data")]
		public IFormatProvider FormatInfo 
		{
			get { return _formatInfo;}
			set {_formatInfo = value;}
		}
		private TypeConverter typeConverter;

		public String NullText = "(null)";

		public ammTextBox()
		{
		}

		/// <summary>
		/// Found this hook from debugging through Kevin's code
		/// Here is where you set up formatter
		/// </summary>
		/// <param name="b"></param>
		public override void RegisterBindingFormatEventHandlers(Binding b)
		{
			base.RegisterBindingFormatEventHandlers (b);
			b.Format +=new ConvertEventHandler(FormatEvent);
			b.Parse +=new ConvertEventHandler(ParseEvent);
		}


		/// <summary>
		/// This happens when databinding says its time to format bound data in the textbox
		/// This is where the real work gets done
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="cevent"></param>
		protected virtual void FormatEvent(object sender, ConvertEventArgs cevent)
		{
			if (this._format != null)
			{
				cevent.Value = Util.GetText(cevent.Value,this.NullText,this._format,this._formatInfo,this.typeConverter);
			}
		}

		/// <summary>
		/// This happens when the control's changed value is going to be writen back to
		/// the datasource
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="cevent"></param>
		protected virtual void ParseEvent(object sender, ConvertEventArgs cevent)
		{
			//if the desired type <> bound type then exit???
			object o = cevent.Value;
			object y = cevent.DesiredType;
		}
}

//This is a simple example that just needs to set the format property
	/// <summary>
	/// Numeric TextBox. Right aligns the text
	/// </summary>
	public class aTextBoxNumeric : ammTextBox 
	{
		public aTextBoxNumeric()
		{
			this.TextAlign = HorizontalAlignment.Right;
			this.Format = "#,###,##0.00";
		}
	}

//This is a more complicated example where I has to override the parse event
	/// <summary>
	/// A TextBox for validating Credit card expiration dates
	/// </summary>
	public class aTextBoxCreditCardExpiration : ammTextBox
	{
		public aTextBoxCreditCardExpiration()
		{
			this.Format = "MM/yy";
			this.MaxLength = 7; // for mm/yy and mm/yyyy formats
		}

		protected override void ParseEvent(object sender, ConvertEventArgs cevent)
		{
			base.ParseEvent (sender, cevent);
			cevent.Value = DateTime.ParseExact((String)cevent.Value,
				new String[] {"My","M/y","M.y","M-y"},
				null,
				System.Globalization.DateTimeStyles.None);
		}

//this is the utility code
		/// <summary>
		/// This code was lifted from the .NET implementation of DataGridTextBoxColumn
		/// that does support the format property!
		/// Basically, it gets a string suitable for displaying
		/// </summary>
		/// <param name="value"></param>
		/// <returns></returns>
		static public string GetText(object value, 
			String nullText, 
			String format, 
			IFormatProvider formatInfo,
			TypeConverter typeConverter)
		{
			string text1;
			if ((value as DBNull) != null)
			{
				return nullText; 
			}
			if ((format != null) && ((format.Length != 0) && ((value as IFormattable) != null)))
			{
				try
				{
					text1 = ((IFormattable) value).ToString(format,formatInfo);
					return text1; 
				}
				catch (Exception)
				{
					goto L_0084;
				}

			}
			if ((typeConverter != null) && typeConverter.CanConvertTo(typeof(string)))
			{
				return ((string) typeConverter.ConvertTo(value, typeof(string))); 
			}

			L_0084:
				if (value == null)
				{
					return ""; 
				}
			return value.ToString();
		}
>Where in the MM.Net framework can I plug in binding event handlers for Parse and Format? Is the method 'RegisterBindingFormatEventHandlers' intended to be used for this purpose? If so how would I implement it for specific textbox controls?
>
>Thanks in Advance
>
>Terry Carroll
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform