Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

replace existing control with custom class .NET Winforms

I have an existing .NET application with tons of controls already created.

Now I have a new custom control class for - say - buttons (which inherits from "Button"), with extra properties, methods etc. Compiled fine, and it shows in my toolbar.

How can I tell VS that I want to REPLACE existing controls with the new one?

I mean, if I open the designer code and manually change the control's type, VS is really unhappy and the design is broken and down with a nasty error message.

Is there a way to change the designer generated code without breaking the design in VS?

Or Is there a way to do it other than manually delete each control, and create a new one? This would be a real hassle since all control's handlers will be erased in the form's code when I delete the control. Like:

handles myControl.Click ... handles myControl.MouseHover ... etc.

Any tip welcome!

like image 626
Didier Levy Avatar asked Feb 16 '11 12:02

Didier Levy


1 Answers

Complete procedure follows.

Make a backup!

Go to solution explorer window (Ctrl Alt L is the shortcut) and click "Show all files" button. Select the form you want to do control replacement. Clic on the small triangle to show the "designer.vb" file.

Open it.

Search for the control instance you want to replace. Check for the occurrences of:

Me.ControlToBeReplaced = New System.Windows.Forms.TypeOfControlToReplace()

Substitute your definition of control. Do the same for every occurrence of:

Friend WithEvents ControlToBeReplaced As System.Windows.Forms.TypeOfControlToReplace

Build your solution

like image 134
Andrea Antonangeli Avatar answered Nov 11 '22 16:11

Andrea Antonangeli