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.

Re: REPOST: Displaying and Editing a Time value in a PropertyGrid by Bob

Bob
Tue Aug 10 15:06:44 CDT 2004

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" <"dunawayc[[at]_lunchmeat_sbcglobal[dot]]net"> wrote in
message news:njzxrt13ueer.rnod5ub719wr$.dlg@40tude.net...
> 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.



Re: REPOST: Displaying and Editing a Time value in a PropertyGrid by Chris

Chris
Tue Aug 10 15:39:05 CDT 2004

On Tue, 10 Aug 2004 22:06:44 +0200, Bob Powell [MVP] wrote:

> The code below my signature shows a similar type-converter. Any DateTime
> property can be made to use this type converter using the
> TypeConverterAttribute.

Thanks for the code and I'll definitely look into that. But I guess my
question is regarding the control used by the PropertyGrid to handle
editing the value. Right now, with a date property, if I click the drop
down arrow, it displays a calendar control to pick a date.

What I would like is a up down control like the DateTimePicker control
exposes to allow the user to edit the time.

Thanks again

--
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.

Re: REPOST: Displaying and Editing a Time value in a PropertyGrid by Bob

Bob
Wed Aug 11 02:06:39 CDT 2004

It's also possible to replace the editor with a custom one. The UITypeEditor
enables you to create a graphical editor which integrates with the
PropertyGrid. There are several examples of that in MSDN also. Sorry but I
don't have code for a clock editor.

--
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






"Chris Dunaway" <"dunawayc[[at]_lunchmeat_sbcglobal[dot]]net"> wrote in
message news:1wx8sm8pyzy4i$.1kyfw30uwgbzr$.dlg@40tude.net...
> On Tue, 10 Aug 2004 22:06:44 +0200, Bob Powell [MVP] wrote:
>
> > The code below my signature shows a similar type-converter. Any DateTime
> > property can be made to use this type converter using the
> > TypeConverterAttribute.
>
> Thanks for the code and I'll definitely look into that. But I guess my
> question is regarding the control used by the PropertyGrid to handle
> editing the value. Right now, with a date property, if I click the drop
> down arrow, it displays a calendar control to pick a date.
>
> What I would like is a up down control like the DateTimePicker control
> exposes to allow the user to edit the time.
>
> Thanks again
>
> --
> 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.



Re: REPOST: Displaying and Editing a Time value in a PropertyGrid by Chris

Chris
Wed Aug 11 08:18:36 CDT 2004

On Wed, 11 Aug 2004 09:06:39 +0200, Bob Powell [MVP] wrote:

> It's also possible to replace the editor with a custom one. The UITypeEditor

Thanks Bob, I'll check that out.

--
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.