Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

User control missing from Toolbox

Tags:

c#

.net

winforms

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:

  • Most relevant: Can I use a UserControl from the same project without making a DLL?
  • Link3: http:// www. vbforums. com/showthread.php?660017-Custom-controls-not-showing-up-in-toolbox
  • Link4: https:// social.msdn. microsoft. com/Forums/en-US/887c0adc-0c26-4789-b563-46d294177eb6/cant-see-custom-control-in-toolbox?forum=csharpide
  • Plus dozens of others. The three above were the most relevant and didn't help.

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()
        {
like image 843
JoshS Avatar asked Nov 24 '16 01:11

JoshS


3 Answers

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.

like image 155
Jan Avatar answered Oct 13 '22 14:10

Jan


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"

like image 42
smirkingman Avatar answered Oct 13 '22 14:10

smirkingman


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.

like image 40
JoshS Avatar answered Oct 13 '22 15:10

JoshS