Reputation: 99
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:
More radical solutions I found:
Add [ToolboxItem(true)] to the UserControl.
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
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
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...
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
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
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:
In the Constructor of that Form initialize the "hidden" CustomUserControl
CustomUserControl con = new CustomUserControl();
Controls.Add(con);
con.Dock = DockStyle.Fill;
Rebuild Solution (I started it once)
Hope it helps someone.
Upvotes: 4
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
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