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
TypeConverterAt tribute.
--
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.Componen tModel.ITypeDes criptorContext context,
System.Type sourceType)
{
if(sourceType == typeof(string))
return true; //yes we can convert from a string
return base.CanConvert From(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(Sys tem.ComponentMo del.ITypeDescri ptorContext context,
System.Globaliz ation.CultureIn fo culture, object value)
{
if(value is string)
{
DateTime ds = DateTime.ParseE xact((string)va lue,new
string[]{"T"},culture,D ateTimeStyles.A llowWhiteSpaces );
return ds;
}
return base.ConvertFro m(context, culture, value);
}
/// <summary>
/// finding out the form the data is going to be converted to
/// </summary>
/// <param name="context"> </param>
/// <param name="destinati onType"></param>
/// <returns></returns>
public override bool
CanConvertTo(Sy stem.ComponentM odel.ITypeDescr iptorContext context,
System.Type destinationType )
{
if(destinationT ype == typeof(DateTime ))
return true;
return base.CanConvert To(context, destinationType );
}
public override object
ConvertTo(Syste m.ComponentMode l.ITypeDescript orContext context,
System.Globaliz ation.CultureIn fo culture, object value, System.Type
destinationType )
{
if(destinationT ype==typeof(str ing))
{
return ((DateTime)valu e).ToLongTimeSt ring();
}
return base.ConvertTo( context,culture ,value,destinat ionType);
}
/// <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. ITypeDescriptor Context
context, object value)
{
if(value is string)
{
try
{
DateTime ds = DateTime.ParseE xact((string)va lue,new
string[]{"T"},CultureIn fo.CurrentCultu re,DateTimeStyl es.AllowWhiteSp aces);
return true;
}
catch(Exception )
{
return false;
}
}
return false;
}
}
----------------------------------------------------------------------------
---------------------------------
"Chris Dunaway" <"dunawayc[[at]_lunchmeat_sbcg lobal[dot]]net"> wrote in
message news:nj******** *************** *****@40tude.ne t...
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_lunch meat_[DOT]net
To send me an E-mail, remove the "[", "]", underscores ,lunchmeat, and
replace certain words in my E-Mail address.