Results 1 to 3 of 3

Thread: Copying control from one form to another

  1. #1

    Thread Starter
    Member
    Join Date
    Jan 2005
    Posts
    40

    Copying control from one form to another

    I want to (programatically) copy a panel control from one form to a new form, then open the new form. Here's what I've got so far:

    Dim frm As New frmOnTop
    Dim ctl As Panel
    ctl = pnlComments
    With frm
    .Controls.Add(ctl)
    .Text = "Comments"
    .Show()
    End With

    This works, but it removes the panel (pnlComments) from the original form. I just want a copy of it on the new form.

    Thanks!

  2. #2
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177

    Re: Copying control from one form to another

    Because controls are classes and classes are managed by reference, you can't create a copy of a control by simply assigning it to another variable.

    Your only options are to either copy each individual property "value" from one control to another, or the recommended way would be to create a user control which contains all the components you want to reuse and then use that whereever you need it.

  3. #3
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949

    Re: Copying control from one form to another

    Why not use the code you posted and just reverse it back to form1 when you close form2?
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width