MFC app with Windows Forms and User Control - what am I missing?

Background:

We have a large legacy app written in native C++ (VC6) and MFC. We are doing some major enhancements that require a bunch of new and revamped UI's, and have decided to do the new UI work with Windows Forms. The app has been moved to VS2005 and updated to build with /clr - so far, so good. I have created a new Windows Forms dialog, and am able to launch it and interact with it just fine - looking real good.

Here's the problem:

I've created a User Control (one of many) that I want hosted in a panel so the controls can be selectively enabled/disabled to replace each other based on other user actions. (These controls are created directly in the MFC project - not in a seperate assembly, as they won't be reused elsewhere.) I've done exactly this kind of implementation in C# tons of times, and it works well for me. However, when I try this in my app, I can't seem to use the user control.

It appears to be something related to the fact that this app started life as an MFC app. I tried two experiments to verify this:

1) create a new C++ windows forms app with one form, add a user control that has one button and one label, build the project, then add the user control to the form by dragging it from the toolbox. This works just fine.

2) create a new C++ MFC app - I just clicked through the MFC designer using all defaults. Add a Windows Form. Now add a User Control just like we did in the Windows Form test. Build the project. The user control appears in the toolbox as I expect it to. However, when I try to drag it onto the form I get a message box that states: "Failed to load toolbox item 'MyControl'. It will be removed from the toolbox."

If I build the project again I get no errors or warnings and the control goes back into the toolbox. But I get the same results trying to use it.

There is one noticable clue that must mean something, but I can't figure out how to resolve it: when hovering over my user control in the toolbox I get a valid version number in the Windows Forms case, but just 0.0.0.0 in the MFC case. Does this mean the project is not properly building an assembly so that the designer can use reflection or something like that If so, how do I get over this in an MFC app

I could certainly use techniques other than User Controls to do the job I need to get done, but none of them are quite as clean, IMO, so that's not the nature of this question.

Thanks in advance,

 

Alan



Answer this question

MFC app with Windows Forms and User Control - what am I missing?

  • BJ Subsets

    Alan4s wrote:

    Thanks for the response Nishant - at least I know it's not just me.

    Has this been filed as a bug with MS

    Not to my knowledge. And I seriously doubt if it will be addressed - since this seems to be more of an issue with the designer - it seems to be unable to handle mixed-mode assemblies properly. But you may want to report this :-)



  • Marcela Moreno

    Well, if the feature doesn't work, then it should be either fixed, or prevented in the first place. As it is now, when working in an MFC app Visual Studio allows you to add a new user control, design it, compile it, and it even puts it into the toolbox. So everything works until you want to actually use the control. Don't let me get so far into it before it throws up - again, either make it work, or block the actions up front.

    I'll probably submit this as a bug at some point, but for now I'm working around it by creating the user controls in a seperate assembly - this seems to work much better at this stage, but adds some minor complexity when I need to deploy.


  • Emil Damian

    Yep, if your user control is in an MFC project, chances are pretty bad that you'll be able to do drag/drop stuff with it. I've been through this pretty much every time I tried it.

    But if you hand code it, it will compile and run correctly. Drag/Drop some other control, and then do a search replace - or manually replace since it's only in 3-4 places.

    Alan4s wrote:

    Background:

    We have a large legacy app written in native C++ (VC6) and MFC. We are doing some major enhancements that require a bunch of new and revamped UI's, and have decided to do the new UI work with Windows Forms. The app has been moved to VS2005 and updated to build with /clr - so far, so good. I have created a new Windows Forms dialog, and am able to launch it and interact with it just fine - looking real good.

    Here's the problem:

    I've created a User Control (one of many) that I want hosted in a panel so the controls can be selectively enabled/disabled to replace each other based on other user actions. (These controls are created directly in the MFC project - not in a seperate assembly, as they won't be reused elsewhere.) I've done exactly this kind of implementation in C# tons of times, and it works well for me. However, when I try this in my app, I can't seem to use the user control.

    It appears to be something related to the fact that this app started life as an MFC app. I tried two experiments to verify this:

    1) create a new C++ windows forms app with one form, add a user control that has one button and one label, build the project, then add the user control to the form by dragging it from the toolbox. This works just fine.

    2) create a new C++ MFC app - I just clicked through the MFC designer using all defaults. Add a Windows Form. Now add a User Control just like we did in the Windows Form test. Build the project. The user control appears in the toolbox as I expect it to. However, when I try to drag it onto the form I get a message box that states: "Failed to load toolbox item 'MyControl'. It will be removed from the toolbox."

    If I build the project again I get no errors or warnings and the control goes back into the toolbox. But I get the same results trying to use it.

    There is one noticable clue that must mean something, but I can't figure out how to resolve it: when hovering over my user control in the toolbox I get a valid version number in the Windows Forms case, but just 0.0.0.0 in the MFC case. Does this mean the project is not properly building an assembly so that the designer can use reflection or something like that If so, how do I get over this in an MFC app

    I could certainly use techniques other than User Controls to do the job I need to get done, but none of them are quite as clean, IMO, so that's not the nature of this question.

    Thanks in advance,

    Alan



  • Earle

    Thanks for the response Nishant - at least I know it's not just me.

    Has this been filed as a bug with MS


  • MFC app with Windows Forms and User Control - what am I missing?