Missing UserControl in My User Controls

  • Thread starter Thread starter Mark Olbert
  • Start date Start date
M

Mark Olbert

Could someone please explain to me why the f**k my Windows.Forms application only allows >>one<<
user control to appear in the My User Controls section of the toolbar???

I ran into this problem when I created my second user control. It compiled fine, no messages, but it
refused to show up in the My User Controls section.

Thinking that it might be a problem with the structure of the second user control, I deleted it and
then made a copy of the first user control (which DOES appear in My User Controls), changed the
copy's class name, compiled it (again with no errors)... and still nothing. There should've been two
different user controls in My User Controls. And since the source code files are identical, except
for the class name, why the first one should appear but the second one not is...bizarre. To say the
least!

Any thoughts on how to solve this problem would be greatly appreciated.

- Mark
 
For a user control to appear in the toolbar, two conditions must hold
1. The control must have been compiled successfully
2. The code for the control (designer or code view, I don't remember exactly) must be open in your Visual Studio application

Probably you developed your first control, tested it, and then closed all files. As the files are closed, the control does no longer appear in the toolbox. Open the files again and the control will reappear

Alain
 
Hi Mark,

You may try Alain's suggestion, opening the designer view of the 2nd user
control.
The toolbox item was actually added to the ToolBox list when initializing
the document designer of the user control.
Note, you need change the name of the usercontrol in the propertygrid to
let the designer handle it and update the toolbox, changing the class name
directly in code will break the toolboxitem.

Does it resolve your problem?
Feel free to reply this thread, if you still have problem on it.

Best regards,

Ying-Shen Yu [MSFT]
Microsoft community Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
This mail should not be replied directly, please remove the word "online"
before sending mail.
 
In addition, you may refer to the IToolBox Service in MSDN Documentation
for more informatin about the toolbox.

Thanks!


Best regards,

Ying-Shen Yu [MSFT]
Microsoft community Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
This mail should not be replied directly, please remove the word "online"
before sending mail.
 
Alain,

Thanx for the tip. However, the code files for both controls were open.

- Mark
 
Hi mark,

Have you resolved this problem?

If this problem still persists, I'd like to know if you overrided the
designer of the usercontrol? If you provided a customized root designer for
this user control (or the "usercontrol" is not actually derived from
Usercontrol class), you need add the toolbox item by yourself, take a look
at the IToolboxService interface, maybe AddLinkedToolBoxItem will help you.


If it is the default usercontrol document designer, I'd like you try the
following steps to see if it will be helpful:
1. create a new user control (using "Add"->"Add User Control")...
2. build the project, you should see a toolbox item in "My User Controls"
tab.
3. in the designer view of the new usercontrol, rename the usercontrol in
the PropertyGrid to the name of your own usercontrol.
4. close the designer view ,then replace the code of the usercontrol with
your own code. Note your own usercontrol should be use the same name and
the same namespace as the auto-generated usercontrol. Then re-build and
open the designer view again.

Then open the designer view you would like to add your usercontrol and see
if you could add the usercontrol from the toolbox.


Best regards,

Ying-Shen Yu [MSFT]
Microsoft community Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
This mail should not be replied directly, please remove the word "online"
before sending mail.
 
Ying-Shen,

Thanx for the toolbox tip, I'll try it.

The workaround I'm currently using is essentially what you suggested (i.e., create a plain
usercontrol and then manually change the inheritance chain so that it derives from my custom control
class).

FYI, my custom control does have a root document designer associated with it. Based on your comments
I gather this somehow inhibits the automagic addition of descendants of the custom control to the
toolbox? Can you point out to me where this is documented? I want to see what I overlooked.

- Mark
 
Okay, I spoke too soon; I'm unclear as to when/how to execute the AddToolBoxItem() method. Is this
something the root designer needs to do when it gets created?

Sorry to bug you about this, but the docs are very unclear as to how to use the IToolBoxService
stuff.

- Mark
 
Ying-Shen,

I did some experimenting with trying to call AddToolboxItem() from within the Initialize() method of
the DocumentDesigner.

What I wanted to do was remove any existing ToolboxItem in the My User Controls tab with the same
name as the control the designer is associated with. I was trying to do that by matching
ToolboxItem.DisplayName to Control.GetType().ToString(). I was then planning on calling
IToolboxService.AddToolboxItem(new ToolboxItem(Control.GetType()), Control.GetType().ToString()) to
add an item to the toolbox that would create an instance of the control being designed.

Unfortunately, I ran into the following problem: so far as the designer is concerned, the control
it's designing is an instance of the "base class" of the user control, not the derived class. This
may be clearer if I spell out some definitions first:

// this is the control that the designer is associated with. It's part of the control library dll.
// I create inherited versions of this control in a project
public class RecordBase : UserControl
{
.....
}

// for example, the project contains the following inherited UserControl
public class giver_rec : RecordBase
{
....
}

When, in the RecordBase's DocumentDesigner I query the Control property's Type, I get back
RecordBase, not giver_rec. Of course, giver_rec is derived from RecordBase, but what I want to add
to the toolbox is a ToolboxItem that will create an instance of giver_rec, not RecordBase.

Any thoughts on how to accomplish what I want to do?

- Mark
 
Ying-Shen,

Please disregard my earlier post. I discovered that I need to wait for the Control to be
fully-loaded before its Type will resolve "properly". Adding an event handler for
IDesignerHost.LoadComplete and stuffing the toolbox logic into the handler solved the problem.

- Mark
 
Just one of many VS.NET bugs I've run into! Here's the work-around for
the problem that I have found:

First, open your user control in the designer. Often that will restore
it to the toolbar. If all your controls got removed from the toolbar,
you will need to open every one of them.

Not all the controls will get added to the toolbar this way. When that
happens, open the control in the designer, then view the code. Change
the inherits statement (I'm assuming VB.net) to something else then
return to the designer (for example, change inherit from textbox to
inherit from usercontrol). The control should be back in the toolbar
now. Return to the code and restore the inherits statement.

Hope this helps!

Steve
 
Back
Top