none
How to add UserControl to toolbox?

    Question

  • Hi,

    Sorry, I know there are existing "How to add UserControl to toolbox", but I still cannot get it to work. I also have a few more questions.

    I create 3 user controls in C#:

    class ItemTable : SelectionTable

    class PatientTable : SelectionTable

    class SelectionTable : UserControl
    {
          DataGridView _mDataGridView
    }

    class StaffTable : SelectionTable

    The order presented above is the order of listing in the solution explorer under the project.

    I created these controls from Empty Project > Blank Code File.

    After typing in all the code, The Blank Code File icon turns into the UserControl Icon in the Solution Explorer. Then when I double click the Icon, it always tries to open in designer view and creates the .resx files which I keep deleting. Furthermore, MSVS inserts the function void InitializeComponent() together with some other DataGridView variables into my UserControl classes.

    Removing and ignoring all these oddities with MSVS 2010, I can still build and see my UserControls appear in the toolbox (when my currently selected project is the same namespace as my UserControl). I can even create another windows form and add my custom control without problems. However, when I toolbox > choose items > browse > mycontrol.dll it says there are not usercontrols found that can be added.

    Can someone explain why MSVS is doing this to me? I think I am not properly creating the UserControls.

    Regarding the order of the UserControl list, ItemTable always gets added to PatientTable in design view whenever I double click on the PatientTable icon in Solution Explorer... hmmmmm.... why??! =( and SelectionTable never has .resx generated.

    Thanks in advance!


    Jake See
    Saturday, April 03, 2010 10:39 PM

Answers

  • Hi JakeSee,

    The root cause is that the access modifier of the user controls are not set, so they will be set with the default modifier: internal. In this case, the control can only be visible in the same project, not in other projects. So when you add them to the ToolBox via choosing items, it does not work.

    To solve the issue, you need to set the modifier to public, like following:
       public class MyUserControl : UserControl
       { }
    You can get more about access modifiers from:
    http://msdn.microsoft.com/en-us/library/ms173121.aspx

    By the way, I have two suggestions:
    1. If you do not want to show a user control on the Toolbox, just add an attribute [ToolboxItem(false)] to the class.
    2. Place different UserControls in different classes.

    Let me know if this does not help.
    Aland Li


    Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
    Saturday, April 10, 2010 11:40 AM
    Moderator