(HW) Adding custom controls within a project

H

Helmut Wagensonner

Hi,

I have created a custom textBox within a C# project. I want to put that
custom textBox on a form now. I heard about a way, to place that user
control onto a form within the same project, without having to compile it
seperately before.

An example:

1. In Visual Studio create a new C# Windows Application
2. Add a User Control to the Project
3. Modify the code of the control so that it inherits from
System.Windows.Forms.TextBox
4. Overwrite some function (i. e. OnDragEnter)

protected override void OnDragEnter(DragEventArgs drgevent)
{
MessageBox.Show("Test");
base.OnDragEnter (drgevent);
}

5. ...and this is where I stuck: Add this modified textBox to the existing
Form1

It's not possible, to simply drag it on the Form (in Design View), like you
do with controls from the Toolbox. I know, that there's a way. I saw this a
couple of months before in some presentation, but cannot remember how the
guy did this.

Anybody out there knows?
 
M

Mick Doherty

The common way
Right Click the Toolbox and select "Add/Remove Items..."
Browse to your projects Bin directory and select the exe.
Click the OK button and the control will be added to the toolbox.
This will be on the toolbox of every project you now open.

The way I do it
Comment out the code for your control.
Change Inheritance from TextBox to UserControl.
Show the controls designer and rebuild the project.
The textbox control is now in your "My User Controls" tab.
Reset inheritance back to TextBox, uncomment the code and rebuild.
This will only be on the toolbox for your current project.
 
H

Helmut Wagensonner

Thanks a lot! I will try immediately. I remember, that the guy in the
presentation used a different way - but anyway...

Thx, Helmut


"Mick Doherty"
 
S

Sean Hederman

There is a My UserControls group in the toolbox that will show
UserControl-derived classes that were added in VS.NET to a referenced
project. They do sometimes appear in other circumstances, but I never
figured out why.

So, add Project1, add Project2, Reference Project1 in Project2, add a
UserControl to Project1, switch to Project2, and it should appear in the
MyUserControls group.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top