Visual Studio: Copy control WITH all code?

In Visual Studio (2013, if it matters), is there a way to copy a control from one form to another and include all the code attached to that control? (IOW, all code that exists for every event.)
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Hi Kaprice,

Sounds like you need to learn how to enhance controls.

If you inherit from an existing control you then only need one copy of the code and use it as many times as you need.
You can then build on top of it and you can do it with any .net control you use.
Once created you will be able to add from the toolbox or copy from one form to the other.
The thing to keep in mind is that you don't put anything specific to the form in here ie: it should be generic to the usage of the control and use properties, events and/or overrides for those specific pieces of code on the form.

eg.. Below heres a class where I'm adding a hover background to a button which exposes a property for the color that you can use in the designer or code.
You can add this in a new file, and once added you should be able to add in the windows designer of the form from the tool box.

Public Class ButtonHover
	Inherits Button

	''' <summary>
	''' What color you want the Button to have when hovered.
	''' </summary>
	<Description("Hover Color for mouse over events."), DefaultValue(GetType(Color), "Empty")> _
	Public Property HoverColor() As Color
			Return _HoverColor
		End Get
			_HoverColor = value
		End Set
	End Property
	Private _HoverColor As Color = Color.Empty
	Private _WasColor As Color
	Private Sub ButtonEx_MouseEnter(sender As Object, e As EventArgs) Handles Me.MouseEnter
		If Me.HoverColor.ToArgb() <> Color.Empty.ToArgb() Then
			_WasColor = Me.BackColor
			Me.BackColor = Me.HoverColor
		End If
	End Sub
	Private Sub ButtonEx_MouseLeave(sender As Object, e As EventArgs) Handles Me.MouseLeave
		If Me.HoverColor.ToArgb() <> Color.Empty.ToArgb() Then
			Me.BackColor = Color.Transparent
		End If
	End Sub

End Class

Open in new window

KapriceAuthor Commented:
Yeah, there's a LOT I need to learn. I'm an expert programming MS Access but finding the move to Visual Studio quite challenging.

So, you're saying rather than code events directly in the control's event, I should put all the code in a class and then just refer to the class in the control's event? And, doing that means no need to duplicate code if I need it in multiple forms, yes?

Can you share a snippet of how I would use, for example, your ButtonHover class on a button in my WinForm?

And, I assume I'd have one class for each event I needed to use more than once?
Attached is a mini example project showing the use of the HoverButton more than once on the same form using different colors and also re-used on another form.

Make sure you Build it before viewing the forms. Sorry I don't have 2013 installed but should convert with no problems.

>> So, you're saying rather than code events directly in the control's event, ...
If it's to do with the functionality of the control thats to be used in all instances - do it in the control class (ButtonHover)
If it's code is specific to the Form it is on, the events will be added to the form as well where you would place that code.
All events will run.. there is a order to what runs first, which you should do some reading on also.
Hopefully you can see this in the example attached.

>> And, I assume I'd have one class for each event I needed to use more than once?
Not sure what you mean here - it depends what you're trying to do.. in general it's just a control that you handle on that instance of the form.

Heres an MSDN tut regarding this sort of implementation.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Éric MoreauSenior .Net ConsultantCommented:
this is one of the feature of MZ-Tools (an add-in for Visual Studio): With this feature you can copy and paste controls along with related code (event handlers) from one form to another.
KapriceAuthor Commented:
Eric, that's a pretty impressive tool with a ton of features. I'll consider that.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.