[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 182
  • Last Modified:

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?

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

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.



Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now