How to put a UserControl into Visual Studio toolBox

前端 未结 10 2156
逝去的感伤
逝去的感伤 2020-11-29 01:02

I made a usercontrol in my project, and after building project, I need to put it in my toolbox, and use it as a common control. but i can\'t. the UserControl is

相关标签:
10条回答
  • 2020-11-29 01:25

    Basic qustion if you are using generics in your base control. If yes:

    lets say we have control:

    public class MyComboDropDown : ComboDropDownComon<MyType>
    {
        public MyComboDropDown() { }
    }
    

    MyComboDropDown will not allow to open designer on it and will be not shown in Toolbox. Why? Because base control is not already compiled - when MyComboDropDown is complied. You can modify to this:

    public class MyComboDropDown : MyComboDropDownBase
    {
        public MyComboDropDown() { }
    }
    
    public class MyComboDropDownBase : ComboDropDownComon<MyType>
    {
    
    }
    

    Than after rebuild, and reset toolbox it should be able to see MyComboDropDown in designer and also in Toolbox

    0 讨论(0)
  • 2020-11-29 01:32

    Right-click on toolbar then click on "choose item" in context menu. A dialog with registered components pops up. in this dialog click "Browse" to select your assembly with the usercontrol you want to use.

    PS. This assembly should be registered before.

    0 讨论(0)
  • 2020-11-29 01:34

    I found that the user control must have a parameterless constructor or it won't show up in the list. at least that was true in vs2005.

    0 讨论(0)
  • 2020-11-29 01:35

    I'm assuming you're using VS2010 (that's what you've tagged the question as) I had problems getting them to add automatically to the toolbox as in VS2008/2005. There's actually an option to stop the toolbox auto populating!

    Go to Tools > Options > Windows Forms Designer > General

    At the bottom of the list you'll find Toolbox > AutoToolboxPopulate which on a fresh install defaults to False. Set it true and then rebuild your solution.

    Hey presto they user controls in you solution should be automatically added to the toolbox. You might have to reload the solution as well.

    0 讨论(0)
  • 2020-11-29 01:35

    Recompiling did the trick for me!

    0 讨论(0)
  • 2020-11-29 01:37

    Using VS 2010:

    Let's say you have a Windows.Forms project. You add a UserControl (say MyControl) to the project, and design it all up. Now you want to add it to your toolbox.

    As soon as the project is successfully built once, it will appear in your Framework Components. Right click the Toolbox to get the context menu, select "Choose Items...", and browse to the name of your control (MyControl) under the ".NET Framework Components" tab.

    Advantage over using dlls: you can edit the controls in the same project as your form, and the form will build with the new controls. However, the control will only be avilable to this project.

    Note: If the control has build errors, resolve them before moving on to the containing forms, or the designer has a heart attack.

    0 讨论(0)
提交回复
热议问题