Copy / Paste in Visual Studio (VB.NET) looses the 'connected' evetns

I am trying to move some of my objects into a tab.  When I cut and then paste the object - the object pastes just fine BUT any associated EVENTS with that objects don't get copied over.

This is a simple test I did.  One button on the form.   Added a click event test to it.
When I copy the button, then paste it, VB wants to create a new even for it with _X (where x={1...n},
like this:

  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    End Sub

    Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs)
    End Sub

    Private Sub Button1_Click_2(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

All I'm after is a simple copy/paste where the whole object gets moved over along with all the events, etc.

Any ideas how this is done?

Who is Participating?
SanclerConnect With a Mentor Commented:
When you code an event of a control on your form it adds to the Sub's declaration, at the end  Handles.<controlName>.<Event>

If you try to COPY a control - even if you are copying it into a container such as a tab page - if it retained the same name the Handles.<controlName>.<Event> would be ambiguous.  Which is why the designer requires a different controlName when you try to paste it.

As soon as you CUT a control from a form there are no events to handle for that control, so the designer gets rid of the Handles.<controlName>.<Event> from any relevant sub/s.  When you paste it, the link is not re-created.  So far as the Form is concerned it is a new control.

But if you DRAG a control from one place to another on the same form - including into a containter control such as a tab page - the link between any sub/s and the Handles.<controlName>.<Event> part of the declaration is preserved.  The Form doesn't lose sight of the control so it leaves any event handlers for it in place.

Or that's how it works for me.


Bob LearnedCommented:
It ain't gonna happen, unfortunately.  Are you copying/pasting from one form to another?

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.