tags:

views:

852

answers:

4

I plan to add functionalities to TextBox with the following:

   public class TextBoxExt : TextBox  
    {
        protected override void OnKeyPress(KeyPressEventArgs e)
        {
            base.OnKeyPress(e);
        }

    }

The question is how can we use this TextBoxExt? Is there anyway to get this class onto the ToolBox so that we can just drag and drop it onto the form? If not, what is the best way to use the TextBoxExt?

+3  A: 

1) Build you project with TextBoxExt, make sure it compiles ok.

2) With the form that you want TextBoxExt on, open the toolbox, right click and select "choose items"

3) Browse to you .exe or dll that you compiled in 1)

4) make sure that TextBoxExt has a tick next to it, press ok

5) TextBoxExt should appear in the toolbox, drag it onto your form

(There is another way of doing this, opening the designer file and renaming the instances of TextBox to TextBoxExt but manual editing of designer files can be considered hazardous by some)

Calanus
i have made many extended controls and this is the way to add them to the form. More over you can also add that project to your current solution, and also add the reference to the dll of your project (TextBoxExt) to the project in which you plan to use the TextBoxExt.
Anirudh Goel
A: 

Any custom control in your project should show up in the Toolbox automatically. I have found that sometimes the controls won't show until you close a re-open Visual Studio. I assume the issue has something to do with caching of the contents of the Toolbox.

Rob Windsor
A: 

You need to add a constructor to your derived class.

public class TextBoxExt : TextBox      
{        
    public TextBoxExt()
    {
    }

    protected override void OnKeyPress(KeyPressEventArgs e)        
    {            
         base.OnKeyPress(e);        
    }    
}
James
Not really. Without constructor works as well.
david.healed
Apologies, I just tried it without adding constructor and mines worked fine. Not quite sure how its not automatically picking up on the control.
James
A: 

Your control should appear in the toolbox for your solution automatically. To have it appear for other projects, you have to do Choose Toolbox items, as others have said.

If you want to provide special design-time functionality, then you will also need to provide some additional designer related attributes and probably your own class derived from ControlDesigner.

Eric