vb.netwinformsvisual-studiouser-interfacevisual-studio-designer

How do you change the type of a WinForms control to a new type that is derived from the original type when using Visual Studio Designer?


I built several user controls that used a number of GroupBox objects using Visual Studio Designer and later decided I wanted to be able to control the color of the GroupBox border(a feature you don't seem to be able to do with a standard GroupBox). So I created a new custom GroupBoxPlus control that inherits from GroupBox and overrides OnPaint to draw my border. Is there a way to convert all my GroupBoxs to GroupBoxPlus controls using visual studio designer? If I was building the ui by hand from code I would just do a find and replace GroupBox with GroupBoxPlus but since I used the designer I believe doing so would cause Visual Studio to error when trying to compile. Is there anyway to convert them other then deleting the old groupboxes and dragging new ones in and having to reconfigure all properties? I am using vb.net and winforms.


Solution

  • You just need to replace a the lines of code that you initialize your group boxes.

    Instead of:

    Dim groupBox as new GroupBox();
    

    You would change that to:

    Dim groupBox as new GroupBoxPlus();
    

    You could simply search for 'as new GroupBox();' and replace with 'as new GroupBoxPlus()'. Modifying the generated code of VS designer will not cause problems in this instance. If you do a Find-Replace All and things don't work, pressing Ctrl-Z will undo all the changes the find-replace feature changed.

    If you are really worried about doing a mass find-replace all then just make a copy of your code files or utilize source control before you replace.