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

Posted on 2006-05-30
Last Modified: 2010-05-18
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?

Question by:paullepa
    LVL 96

    Expert Comment

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

    LVL 34

    Accepted Solution

    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

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Join & Write a Comment

    This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
    Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now