locked
User Controls in same project RRS feed

  • Question

  • I need three identical component consisting of different Forms stuff like labels, button, texboxes, and a few custom structures (colored status images).

    Instead of making three of everything, I would like to make a user control that I can drag in my designer three times.

    However, it seems as if I only can make user controls in a different project in order to use it in my main project. Is that so?

    Wednesday, March 31, 2010 6:38 AM

Answers

  • No that isn't true but the IDE can't always work well with controls in the same project. The problem is that the designer has to instantiate a control in order to display it.  That means it has to run the code you're writing.  If that code resides in the current project then complications set in.  It is even worse if the code changes or doesn't compile.  The designer is actually really good at getting this to work but there are still cases where it won't.  For example the Toolbox (with auto population enabled) won't generally add controls until you compile your project.  Even then it can occassionally refuse to recognize them.  At that point your only option is to manually add the control to the designer file.  Then you can switch back to the designer (after closing the designer file - it's a bug) and graphically muck with your control.

    If you intend to use the controls only in one project then keep the controls in that project.  If you want to reuse them across projects though you'll need to put them into a class library.  Many folks put their controls into a separate library anyway for maintenance purposes but for small projects it's overkill.

    Are you having a specific problem that we might be able to help you with?

    Michael Taylor - 3/31/2010
    http://msmvps.com/blogs/p3net

    • Marked as answer by YiChun Chen Thursday, April 1, 2010 10:54 AM
    Wednesday, March 31, 2010 1:59 PM
    Moderator

All replies

  • No that isn't true but the IDE can't always work well with controls in the same project. The problem is that the designer has to instantiate a control in order to display it.  That means it has to run the code you're writing.  If that code resides in the current project then complications set in.  It is even worse if the code changes or doesn't compile.  The designer is actually really good at getting this to work but there are still cases where it won't.  For example the Toolbox (with auto population enabled) won't generally add controls until you compile your project.  Even then it can occassionally refuse to recognize them.  At that point your only option is to manually add the control to the designer file.  Then you can switch back to the designer (after closing the designer file - it's a bug) and graphically muck with your control.

    If you intend to use the controls only in one project then keep the controls in that project.  If you want to reuse them across projects though you'll need to put them into a class library.  Many folks put their controls into a separate library anyway for maintenance purposes but for small projects it's overkill.

    Are you having a specific problem that we might be able to help you with?

    Michael Taylor - 3/31/2010
    http://msmvps.com/blogs/p3net

    • Marked as answer by YiChun Chen Thursday, April 1, 2010 10:54 AM
    Wednesday, March 31, 2010 1:59 PM
    Moderator
  • Thanks for the info. I will give it a try, and return if I am having trouble.

    (Sorry for the late reply, I have been abroad during the Easter.)

    Wednesday, April 7, 2010 8:11 AM
  • Thanks, Michael
    Saturday, June 5, 2010 3:28 PM