You can create a TypeConverter for the value. Basically the type converter's
job is to convert some value to a string and enable a similar string to be
parsed back to some meaningful value.
Shawn Burke's introduction to creating designable controls in MSDN is a
great example.
The code below my signature shows a similar type-converter. Any DateTime
property can be made to use this type converter using the
TypeConverterAttribute.
--
Bob Powell [MVP]
Visual C#, System.Drawing
The Image Transition Library wraps up and LED style instrumentation is
available in the June of Well Formed for C# or VB programmers
http://www.bobpowell.net/currentissue.htm
Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm
The GDI+ FAQ RSS feed:
http://www.bobpowell.net/faqfeed.xml
Windows Forms Tips and Tricks RSS:
http://www.bobpowell.net/tipstricks.xml
Bob's Blog:
http://bobpowelldotnet.blogspot.com/atom.xml
----------------------------------------------------------------------------
---------------------------------
/// <summary>
/// Summary description for TimeConverter.
/// </summary>
public class TimeConverter : TypeConverter
{
public TimeConverter()
{
}
public override bool
CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context,
System.Type sourceType)
{
if(sourceType == typeof(string))
return true; //yes we can convert from a string
return base.CanConvertFrom(context, sourceType);
}
/// <summary>
/// finds out whether or not the type of data passed in is convertable to a
string
/// </summary>
/// <param name="context"></param>
/// <param name="culture"></param>
/// <param name="value"></param>
/// <returns></returns>
public override object
ConvertFrom(System.ComponentModel.ITypeDescriptorContext context,
System.Globalization.CultureInfo culture, object value)
{
if(value is string)
{
DateTime ds = DateTime.ParseExact((string)value,new
string[]{"T"},culture,DateTimeStyles.AllowWhiteSpaces);
return ds;
}
return base.ConvertFrom(context, culture, value);
}
/// <summary>
/// finding out the form the data is going to be converted to
/// </summary>
/// <param name="context"></param>
/// <param name="destinationType"></param>
/// <returns></returns>
public override bool
CanConvertTo(System.ComponentModel.ITypeDescriptorContext context,
System.Type destinationType)
{
if(destinationType == typeof(DateTime))
return true;
return base.CanConvertTo(context, destinationType);
}
public override object
ConvertTo(System.ComponentModel.ITypeDescriptorContext context,
System.Globalization.CultureInfo culture, object value, System.Type
destinationType)
{
if(destinationType==typeof(string))
{
return ((DateTime)value).ToLongTimeString();
}
return base.ConvertTo(context,culture,value,destinationType);
}
/// <summary>
/// checking the validity of the input data and if its valid converting it
to its destination type
/// this is then returned
/// </summary>
/// <param name="context"></param>
/// <param name="value"></param>
/// <returns></returns>
public override bool IsValid(System.ComponentModel.ITypeDescriptorContext
context, object value)
{
if(value is string)
{
try
{
DateTime ds = DateTime.ParseExact((string)value,new
string[]{"T"},CultureInfo.CurrentCulture,DateTimeStyles.AllowWhiteSpaces);
return true;
}
catch(Exception)
{
return false;
}
}
return false;
}
}
----------------------------------------------------------------------------
---------------------------------
Chris Dunaway said:
When using a PropertyGrid, I have an object with a Date property, but I am
only interested in the Time portion. How do I make the PropertyGrid allow
editing the time only? Just the hours and minutes, preferably?
Thanks
--
Chris
dunawayc[AT]sbcglobal_lunchmeat_[DOT]net
To send me an E-mail, remove the "[", "]", underscores ,lunchmeat, and
replace certain words in my E-Mail address.