14

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?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
david.healed
  • 1,289
  • 7
  • 16
  • 31

8 Answers8

17
  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)

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Calanus
  • 25,619
  • 25
  • 85
  • 120
  • 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 Jul 13 '09 at 06:10
  • Adding a reference to the dll(control library) did not automatically add the control to the toolbox in visual studio, doing the second step "Choose items" >> browse >> select dll did the trick in my case. – Martin Nov 11 '18 at 02:56
  • 1
    Hmm that doesn't seem to work, it's saying the exe is invalid when I click on Browse and it doesn't show up automatically as an option to select from the list. Am I missing something here? – rboy Apr 12 '22 at 16:23
9

I know this is super old question, but maybe still useful for someone else that has same problem like me - as it's still on the top Google :)

You might interest to use ToolboxItemAttribute (http://msdn.microsoft.com/en-us/library/system.componentmodel.toolboxitemattribute(v=vs.110).aspx).

I did this at my end to resolve the problem.

[ToolboxItem(true)]
public class PanelTitle : LabelControl  {
   // Whatever code to override LabelControl here...
}

Rebuild the solution and the extended control should be shown in the toolbox.

Adiono
  • 1,034
  • 11
  • 19
3

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
  • 6,744
  • 1
  • 21
  • 26
0

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
  • 80,725
  • 18
  • 167
  • 237
  • 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 Jul 13 '09 at 08:22
  • It's because VS automatically adds a default constructor to the class during build if no constructor is explicitly defined in the source. BUT it does not do that if you define a constructor with parameters, you will then need to add an explicit default constructor too. – JonP Jul 15 '20 at 09:53
0

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
  • 6,364
  • 1
  • 32
  • 49
0

I fell into this trap just a couple of hours ago.
I've got a .NET 2.0 Windows Application project with some custom UserControls; it worked fine. So I decided to order my files in subfolders, to make my project a little bit cleaner.
After that, Visual Studio 2010 designer stopped loading my forms, and ToolBox won't show my controls anymore.
I freaked out, moving back source files in project root, resetting ToolBox, but nothing seemed to work. After that, I remembered I used ReSharper "Remove Unused References", so I tried to put back unused reference, in particular System.Data: problem solved! :O
I can't say you why, but this worked for me.
Hope my experience can help someone else. :)
Bye, Nando

Ferdinando Santacroce
  • 1,047
  • 3
  • 12
  • 31
0

I created an empty constructor for my custom implementation of UltraGridBagLayoutPanel. Although david.healed is right it isn't necessary, it is quite useful to put a breakpoint in to check that when the form initialises it is using your class to implement your custom control.

It would have been a lot easier to edit the designer file, but I tried it and changed both the field type for the control and also changed the assignment of the field to a new instance of my custom control.

private Infragistics.Win.Misc.UltraGridBagLayoutPanel ultraGridBagLayoutPanel1;
this.ultraGridBagLayoutPanel1 = new Infragistics.Win.Misc.UltraGridBagLayoutPanel();

to

private Athia.Reports.ultraGridBagLayoutPanel1 ultraGridBagLayoutPanel1;
this.ultraGridBagLayoutPanel1 = new Athia.Reports.ultraGridBagLayoutPanel1();

Doing this destroys Visual Studio every time, and to fix it requires using a text editor to put it back again. Therefore unless anyone can describe what is wrong with my implementation of this approach, perhaps calling the class the same as the control name isn't a great idea, I think the only safe and reliable way to achieve this is as Calanus describes in steps 1 to 5 or as an small deviation from that as Rob Windsor rightly points out restarting VS will bring the control into the Toolbox automatically. Unfortunately for me I then have to change all of the child controls over from the original class to my customised class :-(.

Matthew R
  • 1,038
  • 11
  • 15
0

Within the same Solution this should work automatically. However, I have found that if the Target Framework aren't matching the Toolbox does not populate. ( I'm assuming really Reference needs to be of version same or lower than target of Reference. ) ( I did get a warning about non-matching Frameworks ) By making these the same Target Framework, Recompile, Restart VS. the control populated correctly. ( I also added the ToolboxItem(true) Attribute)

anon
  • 1