JoshS
JoshS

Reputation: 99

User control missing from Toolbox

Background: C# WinForms program using Visual Studio 2015.

I have a User Control class in my project. My user control class is called 'FocusControl'. My project contains the default 'Form1'. The user control is not being auto populated into my toolbox.

My history of research and unworking solutions:

  1. Set autopopulate on:
    • Auto populate is on and I have closed and reopened the solution and program.
  2. Rebuild solution:
    • Done plenty of times. I've tried to clean and build as well.
  3. Check the namespace of the user control:
    • The namespaces are the same for the UserControl and the form and the project.
  4. Go into toolbox and right click 'Choose Items...'
    • I am not looking to add a DLL, nor wish to create one. I would like to keep design control over the UserControl in my project.
    • Note: When I do make a new 'Windows Forms Control Library' with the same code as my UserControl, I can build it to create the DLL. With this DLL I can successfully add the UserControl to my toolbox. But again, I do not wish to do this.
  5. I tried adding a new user control through 'Add User Control...' through projects and making that UserControl simple with just a button. That didn't show up either.

More radical solutions I found:

  1. Add [ToolboxItem(true)] to the UserControl.

    • Tried it, didn't work.
  2. Delete the old toolbox files from the App Data because they can go corrupt: See here: https://weblogs.asp.net/javiervillarreal/visual-studio-not-showing-properly-the-toolbox-items

    • Tried it, didn't work.

I am out of ideas on what to search Google for to solve this problem.

Research websites:

Edit adding code:

User Control FocusControl.cs

namespace ProjectRaven
{
    [ToolboxItem(true)]

    public partial class FocusControl : UserControl
    {
        private const NumberStyles StyleDouble = NumberStyles.AllowDecimalPoint | NumberStyles.AllowThousands | NumberStyles.AllowLeadingSign;

        private ThermalCamera _camera;
        public FocusControl()
        {
            InitializeComponent();
        }

User Control Designer FocusControl.designer.cs

namespace ProjectRaven
{
    partial class FocusControl
    {
        /// <summary> 
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary> 
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Component Designer generated code

        /// <summary> 
        /// Required method for Designer support - do not modify 
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {

Form Form1.cs

namespace ProjectRaven
{
    public partial class Form1 : Form
    {

        string ipAddressCamera;

        public Form1()
        {
            InitializeComponent();

Form designer Form1.Designer.cs

namespace ProjectRaven
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {

Upvotes: 7

Views: 15610

Answers (5)

osirisgothra
osirisgothra

Reputation: 2237

In VS2019, you will find it is hostile to in-project user controls and populating them in the toolbox but I'm pretty sure I found the fix. After searching around a bit, I finally figured out what it is that is causing it:

From the main window in Visual Studio, go to...

  • Main Menu
    • Tools
      • Options...

Find the pane on the right and locate the subheading General and look beneath it to locate the Windows Form Designer category and click it.

Find the setting:

Automatically Populate Toolbox Items

Which is set by default to FALSE in new installs of VS2019....

Description

Toggles whether the toolbox is automatically populated with components built by this project. The current solution must be closed and reopened for changes to take effect`

I was pretty happy when I found this setting because it described exactly what I was agonizing over. I didn't even have to reload my solution -- the changes took place right away and my controls showed up in the control box as soon as I built my project (not even a clean build, just clicked 'build').

I commented this above but thought I should probably answer this proper since the OP never figured out their problem. Hope someone is helped, and hope that was it for them (if they are still around or ever reads this again, that is).

Upvotes: -1

smirkingman
smirkingman

Reputation: 6358

Make sure your usercontrol is Public and that both in the designer-generated code and your code-behind, it is declared "Public Partial Class MyControl"

Upvotes: 2

Jan
Jan

Reputation: 333

Some of the listed solutions helped me in the past, but they wont today (with a third party child control class). BUT I managed to get the plain UserControl inheritence back to the Toolbox by using it from code first, like so:

  1. Create a new Windows Form
  2. In the Constructor of that Form initialize the "hidden" CustomUserControl

    CustomUserControl con = new CustomUserControl();
    Controls.Add(con);
    con.Dock = DockStyle.Fill;

  3. Rebuild Solution (I started it once)

  4. See the CustomUserControl in the Toolbox

Hope it helps someone.

Upvotes: 4

JoshS
JoshS

Reputation: 99

No fix found.

I started a new project and copied the User Control in first and built the solution. After that I was able to see FocusControl under 'ProjectRaven Components' at the top of my toolbox. I simply copy/pasted the controls and code back from the old project to the new one. It seems to be working.

Remember to change the namespaces if you made a new project with a new project name.

Upvotes: 1

Joseph Wu
Joseph Wu

Reputation: 5008

you can put this user control into its own project and output it as a DLL. Then reference this dll from your project.

Upvotes: 0

Related Questions