FordGT90Concept
"I go fast!1!11!1!"
- Joined
- Oct 13, 2008
- Messages
- 26,263 (4.35/day)
- Location
- IA, USA
System Name | BY-2021 |
---|---|
Processor | AMD Ryzen 7 5800X (65w eco profile) |
Motherboard | MSI B550 Gaming Plus |
Cooling | Scythe Mugen (rev 5) |
Memory | 2 x Kingston HyperX DDR4-3200 32 GiB |
Video Card(s) | AMD Radeon RX 7900 XT |
Storage | Samsung 980 Pro, Seagate Exos X20 TB 7200 RPM |
Display(s) | Nixeus NX-EDG274K (3840x2160@144 DP) + Samsung SyncMaster 906BW (1440x900@60 HDMI-DVI) |
Case | Coolermaster HAF 932 w/ USB 3.0 5.25" bay + USB 3.2 (A+C) 3.5" bay |
Audio Device(s) | Realtek ALC1150, Micca OriGen+ |
Power Supply | Enermax Platimax 850w |
Mouse | Nixeus REVEL-X |
Keyboard | Tesoro Excalibur |
Software | Windows 10 Home 64-bit |
Benchmark Scores | Faster than the tortoise; slower than the hare. |
Well, it doesn't take very long to figure out that ColorEditor (the default editor System.Drawing.Color is associated with) isn't that great. The problem is, the better choice (ColorDialog) does does not inherit "UITypeEditor" (it inherits CommonDialog) so it can't be directly used by PropertyGrids. The solution is to create a new class that inherits UITypeEditor and pops up a ColorDialog on demand. For this, you'll need four new classes:
1) [MyColor] A class to store the color information. We cannot use System.Drawing.Color because it ignores the EditStyle property.
2) [MyColorConverter] A class that derives from TypeConverter. This is needed for DefaultValueAttribute and to ignore the class above when displaying in the PropertyGrid.
3) [MyColorEditor] A class that derives from UITypeEditor. This is needed to show that fancy ColorDialog.
4) [SelectableClass] A class that contains an object of the color which will be selected by the PropertyGrid.
An example of these files have been included in the attached source file (the name of the file is in the brackets above).
MyColor is a relatively generic class. The only thing to really pay attention to here is the TypeConverter attribute:
In short, this tells it to use "MyColorConverter" if it wants to convert it. The reason for needing this is mostly cosmetic and not absolutely necessary but still, it is a good thing to have.
Looking at the MyColorConverter class, you'll find only two methods: ConvertFrom and ConvertTo. Again, these aren't too complex. All it does is check for the incoming type (string or MyColor) and convert it to other. If you ConvertFrom a string, it will give you a MyColor class (I used new MyColor(string)). If you ConvertTo a string, it will take an instance of MyColor and spit out a string that represents it (I used MyColor.ToString()).
ConvertFrom is invoked by the DefaultValueAttribute. The advantage of this is the default value is no longer bold in the PropertyGrid.
ConvertTo is invoked by the PropertyGrid itself so that it displays "0 0 0" instead of "ColorDialogExample.MyColor"
Now to have a look at MyColorEditor which also only has two methods: GetEditStyle and EditValue. GetEditStyle only has three options to return: Modal, DropDown, and None. Modal (has a [...] button) basically means a dialog should pop up and prompt the user for a new value. DropDown (has a [\/] button) drops down a list of options. If you enable DropDown you should set override IsDropDownResizable as well. None means no button at all will appear so all the user can do is a simple text edit to the property.
EditValue is what happens when someone clicks on the button. It should get input from the user and return the new value that was entered.
Lastly, we have the SelectableObject class which is in turn loaded by the Main form. The important line to note from this class is the following:
This tells it to use our custom MyColorEditor instead of the default editor (ColorEditor). Right below that line is the DefaultValueAttribute discussed earlier and some other useful items (category and description).
That's pretty much it. Ask if you need help.
Here is what the final app looks like:
1) [MyColor] A class to store the color information. We cannot use System.Drawing.Color because it ignores the EditStyle property.
2) [MyColorConverter] A class that derives from TypeConverter. This is needed for DefaultValueAttribute and to ignore the class above when displaying in the PropertyGrid.
3) [MyColorEditor] A class that derives from UITypeEditor. This is needed to show that fancy ColorDialog.
4) [SelectableClass] A class that contains an object of the color which will be selected by the PropertyGrid.
An example of these files have been included in the attached source file (the name of the file is in the brackets above).
MyColor is a relatively generic class. The only thing to really pay attention to here is the TypeConverter attribute:
Code:
[TypeConverter(typeof(MyColorConverter))]
Looking at the MyColorConverter class, you'll find only two methods: ConvertFrom and ConvertTo. Again, these aren't too complex. All it does is check for the incoming type (string or MyColor) and convert it to other. If you ConvertFrom a string, it will give you a MyColor class (I used new MyColor(string)). If you ConvertTo a string, it will take an instance of MyColor and spit out a string that represents it (I used MyColor.ToString()).
ConvertFrom is invoked by the DefaultValueAttribute. The advantage of this is the default value is no longer bold in the PropertyGrid.
ConvertTo is invoked by the PropertyGrid itself so that it displays "0 0 0" instead of "ColorDialogExample.MyColor"
Now to have a look at MyColorEditor which also only has two methods: GetEditStyle and EditValue. GetEditStyle only has three options to return: Modal, DropDown, and None. Modal (has a [...] button) basically means a dialog should pop up and prompt the user for a new value. DropDown (has a [\/] button) drops down a list of options. If you enable DropDown you should set override IsDropDownResizable as well. None means no button at all will appear so all the user can do is a simple text edit to the property.
EditValue is what happens when someone clicks on the button. It should get input from the user and return the new value that was entered.
Lastly, we have the SelectableObject class which is in turn loaded by the Main form. The important line to note from this class is the following:
Code:
[EditorAttribute(typeof(MyColorEditor), typeof(UITypeEditor))]
That's pretty much it. Ask if you need help.
Here is what the final app looks like:

Attachments
Last edited: