COMPILE ERROR when using custom controls w/ custom properties w/ custom TypeConverters

COMPILE ERROR when using custom controls w/ custom properties w/ custom TypeConverters

Post by Steve Rose » Wed, 16 Jul 2003 07:21:56



I have a custom control that has a custom property using the following
enum:

[ TypeConverter( typeof( MyPropertyConverter ) ) ]
public enum MyProperty { Value1, Value2, Value3 };

The TypeConverter does nothing more than convert the Value1 value to a
"Value 1" string for display in a property grid and vice versa. The
problem is that when I add my custom control to a form, the Component
Designer places the following line in my C# code:

this.MyCtrl.MyProp = MyProperty.Value 1;

Note the space between the "Value" and the "1" at the end. This results
in a compile error since there should be no space. I assume
MyPropertyConverter is messin' with me because I'm missing a conversion
required by the Component Designer to generate the correct
initialization value in the above statement.


AND FINALLY THE QUESTION: Does anyone know what that conversion type
should be? Or am I completely off-base? Thanks.


For reference, below is the MyPropertyConverter in all its glory:

class MyPropertyConverter : System.ComponentModel.TypeConverter
{

public override bool
GetStandardValuesSupported( ITypeDescriptorContext context )
{ return( true ); }

public override bool
GetStandardValuesExclusive( ITypeDescriptorContext context )
{ return( false ); }

public override StandardValuesCollection
GetStandardValues( ITypeDescriptorContext context )
{
MyProperty[] sv =
{
MyProperty.Value1,
MyProperty.Value2,
MyProperty.Value3,
};
return( new StandardValuesCollection( sv ) );
}

public override bool
CanConvertFrom( ITypeDescriptorContext context,
Type sourceType )
{
if( sourceType == typeof( string ) ) return true;
return( base.CanConvertFrom( context, sourceType ) );
}

public override object
ConvertFrom( ITypeDescriptorContext context,
CultureInfo culture,
object value )
{
if( value is string )
{
switch( (string)value )
{
case "Value 1": return( MyProperty.Value1 );
case "Value 2": return( MyProperty.Value2 );
case "Value 3": return( MyProperty.Value3 );
};
}
return base.ConvertFrom( context, culture, value );
}

public override object
ConvertTo( ITypeDescriptorContext context,
CultureInfo culture,
object value,
Type destinationType )
{
if( destinationType == typeof( string ) )
{
if( value is MyProperty )
{
switch( (MyProperty)value )
{
case MyProperty.Value1: return( "Value 1" );
case MyProperty.Value2: return( "Value 2" );
case MyProperty.Value3: return( "Value 3" );
};
}
else if( value is string )
{
return( (string)value );
}
}
return base.ConvertTo( context, culture, value, destinationType );
}

} /**** class MyPropertyConverter ****/



--
Steve Rosenberry
Sr. Partner

Electronic Solutions Company -- For the Home of Integration
http://www.yqcomputer.com/

(610) 670-1710
 
 
 

1. w'S ***Hot stuff - check this out !!! w'S

2. Using a collection of custom objects as a property in a custom control

I have written a custom control derived from usercontrol and one of the
properties is a list of child objects using the List<> generic
(List<MyClass>). Using the
DesignerSerializationVisibility(DesignerSerializationVisibility.Content)
attribute, the property will show up in the properties grid for the control
and allows adding/removing items in the designer using the standard
Collection Editor. My control uses whats in that collection to dynamically
build child controls. Problem is that when that collection is edited, it is
serialized correctly and I can compile and run the application that uses the
control fine. It works. But at design time, the child controls will not be
created and displayed because the code that does that isn't called
(currently that code is in the handler for OnLoad). So, at design time, how
do I get my code to execute that actually creates the child controls?
Because the property is a collection (List<>), it doesn't really get "set"
so I can't add anything to its setter since that won't execute. It seems
that I need to be able to react to when something is added to my
List<MyClass> instance at design time but I don't know how to do that.

As a test, I created a simple int property and, in its setter, I run my code
that creates the child controls from the collection and it drew them fine at
design time when I set that test property. I really just need to know where
to hook up that code.

Any ideas?

Thanks

3. Custom Control : One custom property to array of controls

4. Displaying a form to accept user input from a property in property grid (custom TypeConverter??)

5. Compile error! when using TypeConverter for a custom property in a custom control

6. Custom Control with Custom Component as Property

7. custom events in custom controls not appear in properties window

8. Keep custom property-value in custom rendered control

9. Using TypeConverter to serialize custom control in form's InitializeComponent

10. Custom Properties and Custom Controls - design time changes reflected visually.

11. Using TypeConverter to serialize custom control in form's InitializeComponent

12. ControlDesigner not invoked on custom control when control is rendered within another custom control

13. using custom callout to show custom properties

14. Adding Custom Property, applying Custom Property settings limited?

15. Adding Custom Property, applying Custom Property settings limi