REPOST: Displaying and Editing a Time value in a PropertyGrid

C

Chris Dunaway

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

Bob Powell [MVP]

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

Chris Dunaway

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

Bob Powell [MVP]

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 said:
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.
 
C

Chris Dunaway

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top