Windows Develop Bookmark and Share   
 index > Windows Forms Designer > TypeConverter problems
 

TypeConverter problems

Please i need help. Look to this code:

namespace WindowsApplication2
{
public partial class MyControl : Control
{
public MyControl()
{
InitializeComponent();
}

protected override void OnPaint(PaintEventArgs pe)
{
// TODO: Add custom paint code here

// Calling the base class OnPaint
base.OnPaint(pe);
}

private MyInteger myInt = new MyInteger(100);

[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
[DefaultValue(typeof(MyInteger), "100")]
public MyInteger Integer
{
get { return myInt; }
set { myInt = value; }
}

}

[TypeConverter(typeof(MyIntegerConverter))]
public class MyInteger
{
private int _v = 0;

public int Value
{
get { return _v = 0; }
set { _v = value; }
}

public MyInteger(int value)
{
_v = value;
}
}

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

public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
{
if (value is string)
{
return new MyInteger(Int32.Parse((string)value));
}
else
return base.ConvertFrom(context, culture, value);
}

public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
if (destinationType == typeof(string))
return true;
else if (destinationType == typeof(InstanceDescriptor))
return true;
else
return base.CanConvertTo(context, destinationType);
}

public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
{
if (destinationType == typeof(string))
{
return ((MyInteger)value).Value.ToString();
}
else if (destinationType == typeof(InstanceDescriptor))
{
ConstructorInfo info = typeof(MyInteger).GetConstructor(new Type[] { typeof(int) });
InstanceDescriptor desc = new InstanceDescriptor(info, new object[] { ((MyInteger)value).Value });

return desc;
}
else
return base.ConvertTo(context, culture, value, destinationType);
}
}
}

I created this control only to learn how TypeConverter works.
Everything compiles ok but when i add my control to a form, some weird things happens at design time:
1) The integer property doesn't have the value 100. It's void. I don't understand why this happen.
2) When i try to set the property, i get the message: "Object of type 'WindowsApplication2.MyInteger' cannot be converted to type 'WindowsApplication2.MyInteger'." MyInteger cannot be converted to itself?!?! WTF!?!

I really need your help because i tried everything and nothing worked for me. Thanks for any help you can give me.
P.S.: The control and the form that i am inserting the control is in the same project.
danskcarvalho  Tuesday, September 05, 2006 3:21 PM
 NateRickard wrote:

In your property, you're resetting the value of _v everytime Get is called:

get { return _v = 0; }

Instead, you want to just return _v:

get { return _v; }

I'm not sure if that is the entire problem, but you can start by changing that.

Nate



I know where is the problem now! The fact is that VS 2005 is unable to load type converters that are defined in the same project of the form you are adding the control. The workaround is: create a new project and put the code for your control there. That's it! Thanks for the response anyway!
danskcarvalho  Thursday, September 07, 2006 1:14 AM

In your property, you're resetting the value of _v everytime Get is called:

get { return _v = 0; }

Instead, you want to just return _v:

get { return _v; }

I'm not sure if that is the entire problem, but you can start by changing that.

Nate

NateRickard  Wednesday, September 06, 2006 2:02 PM
 NateRickard wrote:

In your property, you're resetting the value of _v everytime Get is called:

get { return _v = 0; }

Instead, you want to just return _v:

get { return _v; }

I'm not sure if that is the entire problem, but you can start by changing that.

Nate



I know where is the problem now! The fact is that VS 2005 is unable to load type converters that are defined in the same project of the form you are adding the control. The workaround is: create a new project and put the code for your control there. That's it! Thanks for the response anyway!
danskcarvalho  Thursday, September 07, 2006 1:14 AM
good to see that you've found the root of this odd problem. It's an ideal setup by the way for controls to be located on a separate assembly from the form using them so you can easily debug them
joeycalisay  Friday, September 08, 2006 11:44 AM

I have been trying to solve the same problem lately and I founda similarsolution. In fact, you don't have to createyour control in a entirely new project.It is sufficient to change onlythe namespace of you custom control class.

The schema is as follows:

namespace MyProjectNamespace

{

public class Form1: System.Windows.Forms

{

//project code

}

}

(for convenience put this in a separate code file)

namespace CustomControlNamespace

{

public class CustomClassName

{

//all your classcode

}

[TypeConverter(typeof(DerivedTypeConverter))]

public class CustomComplexPropertyClass

{

}

}

/*If the namespace of the custom control is "MyProjectNamespace" you will see a properly formated property valuein the Property Window only after the launch of th VS 2005 IDE. As soon as you Debug the project, the value will misteriously disappear and you would no be able to edit it. Even if you type in a correct string for conversion, it will throw an exception.

Surprisingly, if you are usingTypeConverter derived from ExpandableObjectConverter, you can expand the property and commit changes through the subproperties, but the parent property value will still be blank.

*/

Nevertheless, this solution is at least awkward and I hope, it will be fixed in the next versions.

Grallu  Friday, September 21, 2007 1:21 AM

You can use google to search for other answers

Custom Search

More Threads

• Displaymember by valuemember
• Windows Form Design Time Error
• Designer auto assigning bogus string resource values
• Custom User Control
• The variable 'BranchStyle' is either undelcared or was never assigned
• VS2005 Designer fails on inherited user control
• Controls appear attractive in design but not runtime
• Windows application user interface design
• Forcing a control to appear in the Component Tray...
• Drawing at design time.