Adding Custom Controls to Windows Form

pizzaboy

Well-known member
Joined
Jan 5, 2008
Messages
57
Programming Experience
1-3
I've created a "Windows Form Application" using VS 2008.

In the project I've added a custom control. The problem I'm having is how to create an instance of the custom control to a Windows Form?

I've tried dragging directly to a form in the same project from the solution explorer, but a "can't do that" icon appears. When I try to add the control to the toolbox, it fails.

What am I doing wrong? Do I have to create a separate "Windows Forms Control Library" project then compile it into a DLL before I can use the custom control? The thing with that is the control is only really needed in this current project and not really any use to any other.

I've only ever used to VS 2003 & 2005 for ASP.NET purposes so aren't really that familiar with using Custom controls in Windows projects. Having looked back at previous .NET books on 2005, they appear to be pretty vague in this area also. On MSDN, there appears to be some sort of error relating to 2003 having problems showing custom controls in the toolbox, is this still applicable to 2005 and 2008?

Any help would be greatly appreciated.

Thanks.
 
You can add UserControl, it goes automatically to Toolbox (project components tab). You can also add Class and declare it to inherit a specific control (for example Button), generic Control, or Component. When rebuilt it goes automatically to Toolbox. You can also add a class at namespace level in the same file as other classes (for example a form) and same happens, but you can't write a class nested within other class to make it go to Toolbox. What you have attempted doing when you say "In the project I've added a custom control" I don't know.
 
When I said "In the project I've added a custom control", I mean I created a windows project, then in the solution explorer added a new user control then designed it's appearance and added methods and properties to it as opposed to creating a Windows Control Library first.

With regards to the ToolBox, there doesn't appear to be any "Project Components" tab in the list??? Do I have to play with the IDE to get it to show?

Thanks.
 
No, it's just there, first tab in the toolbox. If your application is named "WindowsApplication1" the tab reads "WindowsApplication1 Components". Below that is the "All Windows Forms" tab, below that the "Common Controls" tab etc.
 
Apologies, I was having an build error due having the "Option Strict On", so VS wouldn't allow me to compile and create the New Custom control and as a result show the controls in the ToolBox.

All fixed now and seems to be working fine. My sincere apologies for being a pain, and thanks for the help.
 
Back
Top