Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / Design Time / May 2005

Tip: Looking for answers? Try searching our database.

UserControl.Property cannot convert to target type...

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Michael Palmer - 29 Apr 2005 20:57 GMT
I have a composite UserControl with a public property with a return type of
a custom enumerator. I want this to show up in the PropertyGrid during
design-time... it will allow me to change the enumeration within the grid,
but when I rebuild it defaults back to the first value in the enumeration
rather than the one I selected during run-time. It also displays "Object
type cannot be converted to target type." in the task list during
design-time.

What am I missing? Thanks!
"Jeffrey Tan[MSFT]" - 30 Apr 2005 02:50 GMT
Hi Michael,

Thanks for your post!!

Can you show us some detailed information about "I rebuild it defaults back
to the first value in the enumeration"? What does "rebuid default back to
first value" mean? Currently, I think I still does not understand your
problem very well, I suggest you provide some detailed information to help
us reproduce out your issue. Thanks

I have writen a sample code snippet in usercontrol, like this:
public enum EnumTest
{
    item1,
    item2,
    item3,
    item4
}

private EnumTest _prop=EnumTest.item1;
public EnumTest Prop
{
    get
    {
        return _prop;
    }
    set
    {
        _prop=value;
    }
}
I can manipulate this property in PropertyBrowser without any problem. Do
you write a customized TypeConverter inherits from EnumConverter? If so,
you may refer the below link:
http://groups.google.com/groups?hl=zh-CN&lr=&threadm=OgMyycY3BHA.2328%40tkms
ftngp02&rnum=10&prev=/groups%3Fq%3D%2522Object%2Btype%2Bcannot%2Bbe%2Bconver
ted%2Bto%2Btarget%2Btype%2522%2BEnum%26hl%3Dzh-CN%26lr%3D%26selm%3DOgMyycY3B
HA.2328%2540tkmsftngp02%26rnum%3D10

Also, if you got this error when changing the Enum value at runtime,
normally, we should use Enum.Parse method to parse the object value, for
more information, please refer to the link:
http://groups.google.com/groups?hl=zh-CN&lr=&threadm=89BDFF5E-2C24-4B35-97F5
-9ED3BE3CCC9A%40microsoft.com&rnum=9&prev=/groups%3Fq%3D%2522Object%2Btype%2
Bcannot%2Bbe%2Bconverted%2Bto%2Btarget%2Btype%2522%2BEnum%26hl%3Dzh-CN%26lr%
3D%26selm%3D89BDFF5E-2C24-4B35-97F5-9ED3BE3CCC9A%2540microsoft.com%26rnum%3D
9

Note: take care of the link-break.

Anyway, I will wait for your further feedback. Thanks
========================================================
Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Signature

Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Michael Palmer - 02 May 2005 14:29 GMT
Hi Jeffrey, thanks for your reply. Using your same code, the default value
for EnumTest would be item1. So, item1 would be the selected value in the
property grid for this UserControl on a form. In the forms designer property
grid, if I change the control's EnumTest property to item2, then rebuild my
project, the forms designer automatically resets my value to item1 and
displays in the task list a design-time error message stating "Object type
cannot be converted to target type." When I remove this property from the
UserControl, I'm no longer given that error message, so I know it stems from
that property.

Also... I'm not using a custom TypeConverter for this property.... I figured
the default EnumConverter would work fine, but maybe I'm doing something
wrong.

Thanks, Jeffrey.

-Michael Palmer

> Hi Michael,
>
[quoted text clipped - 58 lines]
> Get Secure! - www.microsoft.com/security
> This posting is provided "as is" with no warranties and confers no rights.
Michael Palmer - 02 May 2005 14:51 GMT
Update: upon further testing, when I add an enumerator referenced from
within the same assembly, the problem does not appear, but when I add my
enumerator which is from another assembly, this problem occurs.

> Hi Jeffrey, thanks for your reply. Using your same code, the default value
> for EnumTest would be item1. So, item1 would be the selected value in the
[quoted text clipped - 79 lines]
>> This posting is provided "as is" with no warranties and confers no
>> rights.
Michael Palmer - 02 May 2005 15:29 GMT
I found the problem. The enumerator that I declared within my UserControl
was located within the Namespace [Global] within another assembly.
Evidently, this namespace name caused the problem with the forms designer
not being able to persist the selected property value. As soon as I changed
the namespace from [Global] to something else, the problem disappeared.
Problem fixed...

So.... I'm assuming that it's probably best to never have a [Global]
namespace declared anywhere.

-Michael

> Update: upon further testing, when I add an enumerator referenced from
> within the same assembly, the problem does not appear, but when I add my
[quoted text clipped - 86 lines]
>>> This posting is provided "as is" with no warranties and confers no
>>> rights.
"Jeffrey Tan[MSFT]" - 03 May 2005 06:39 GMT
Hi Michael,

Cool! I am glad you find the cause to your problem. If you need further
help, please feel free to post. Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Signature

Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.