Results 1 to 13 of 13

Thread: [RESOLVED] How to Duplicate a Panel with it's all controls?

  1. #1

    Thread Starter
    Hyperactive Member Joye's Avatar
    Join Date
    Jul 2009
    Posts
    256

    Resolved [RESOLVED] How to Duplicate a Panel with it's all controls?

    So my project is something like Trello but program but with some special editing made for my work needs.

    I made one panel with all needed controls and codes and I also changed the names of controls to be sorted if needed.
    this video shows me moving this panel as a (To Do card) from location to other location on form.

    https://streamable.com/s/f4dz8/hhgwvc

    this yellow card is a Panel that I want to duplicate or copy past or whatever gives me another same Panel or as it should be named (Card).
    Is there a way to generate another copy of this panel in run or design time and it does the same things where it should get the same properties and behave as the first panel coded?
    any good idea for this situation as I need to make 50 same Panels at least?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: How to Duplicate a Panel with it's all controls?

    Don't use a Panel in the first place. What you should have done is created a user control. You add a user control to your project, design it and add appropriate code in pretty much the same way as you do a form but, once you build your project, that user control is added to the Toolbox and you can then use it like any other control. You can add multiple instances to your form from the designer like you would any other control and you can create instances at run time like you would any other control. It's just like any other control.

    That said, if you really want to stick with the Panel, which I recommend against, then you simply Ctrl+drag in the designer, the same way you copy a file in Windows Explorer or copy anything else that supports similar drag and drop operations.

  3. #3

    Thread Starter
    Hyperactive Member Joye's Avatar
    Join Date
    Jul 2009
    Posts
    256

    Re: How to Duplicate a Panel with it's all controls?

    Quote Originally Posted by jmcilhinney View Post
    Don't use a Panel in the first place. What you should have done is created a user control. You add a user control to your project, design it and add appropriate code in pretty much the same way as you do a form but, once you build your project, that user control is added to the Toolbox and you can then use it like any other control. You can add multiple instances to your form from the designer like you would any other control and you can create instances at run time like you would any other control. It's just like any other control.

    That said, if you really want to stick with the Panel, which I recommend against, then you simply Ctrl+drag in the designer, the same way you copy a file in Windows Explorer or copy anything else that supports similar drag and drop operations.
    Fully understood.
    I will try this and see
    Thank you very much

  4. #4

    Thread Starter
    Hyperactive Member Joye's Avatar
    Join Date
    Jul 2009
    Posts
    256

    Re: How to Duplicate a Panel with it's all controls?

    Okay that worked fine

    I Added the a UserControl that user control has no panel and I manged to move it by mouse in run time then I added this control twice on the same form and worked fine each usercontrol was moving separately with no interference.
    The only problem is when I bounded the textbox in that user control to the (Application Sittings) all other controls from the original one have the same string and are behaving the same as the original one.
    How I can bound those text boxes to a diffident string?

    This video might help understanding the problem
    https://streamable.com/s/xrl4f/ccjvwk

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: How to Duplicate a Panel with it's all controls?

    Where did you do the binding, in the user control or in the form? If you did it in the user control then of course every instance uses the same value, just like if you do it in the form then every instance of that form will use the same value. The proper way to approach this is to make all the child controls Private in the user control and then add a property to the user control for each child control property you want access from outside. For instance, you have a TextBox in the user control and you want to access its Text property from outside then you should add a property something like this:
    vb.net Code:
    1. Public Event TextBox1TextChanged As EventHandler
    2.  
    3. Public Property TextBox1Text As String
    4.     Get
    5.         Return TextBox1.Text
    6.     End Get
    7.     Set
    8.         If TextBox1.Text <> Value Then
    9.             TextBox1.Text = Value
    10.             OnTextBox1TextChanged(EventArgs.Empty)
    11.         End If
    12.     End Set
    13. End Property
    14.  
    15. Protected Sub OnTextBox1TextChanged(e As EventArgs)
    16.     RaiseEvent TextBox1TextChanged(Me, e)
    17. End Sub
    In the form, you can now bind that property to a setting for a specific instance of the user control, just like any other property. The event is necessary because, otherwise, the Binding won't know that the property has changed and that it needs to update the setting.

  6. #6

    Thread Starter
    Hyperactive Member Joye's Avatar
    Join Date
    Jul 2009
    Posts
    256

    Re: How to Duplicate a Panel with it's all controls?

    Quote Originally Posted by jmcilhinney View Post
    Where did you do the binding, in the user control or in the form? If you did it in the user control then of course every instance uses the same value, just like if you do it in the form then every instance of that form will use the same value. The proper way to approach this is to make all the child controls Private in the user control and then add a property to the user control for each child control property you want access from outside. For instance, you have a TextBox in the user control and you want to access its Text property from outside then you should add a property something like this:
    vb.net Code:
    1. Public Event TextBox1TextChanged As EventHandler
    2.  
    3. Public Property TextBox1Text As String
    4.     Get
    5.         Return TextBox1.Text
    6.     End Get
    7.     Set
    8.         If TextBox1.Text <> Value Then
    9.             TextBox1.Text = Value
    10.             OnTextBox1TextChanged(EventArgs.Empty)
    11.         End If
    12.     End Set
    13. End Property
    14.  
    15. Protected Sub OnTextBox1TextChanged(e As EventArgs)
    16.     RaiseEvent TextBox1TextChanged(Me, e)
    17. End Sub
    In the form, you can now bind that property to a setting for a specific instance of the user control, just like any other property. The event is necessary because, otherwise, the Binding won't know that the property has changed and that it needs to update the setting.
    Great it Amazed me how easy it was
    but there is something is not going very well
    when running the application with three instances and start changing the text inside textboxes it seems like letters get deleted very fast or not written at all.

    and here is a video of what is going on and what I did:
    https://streamable.com/s/lkmto/dsfzor

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: How to Duplicate a Panel with it's all controls?

    I just ran a quick test and I had no such issue, but it didn't seem like the entered values were being saved. I don't have time to test more thoroughly right now but I'll have a closer look when I do.

  8. #8

    Thread Starter
    Hyperactive Member Joye's Avatar
    Join Date
    Jul 2009
    Posts
    256

    Re: How to Duplicate a Panel with it's all controls?

    I really do appreciate that.
    Thank you very much.🙏🏼
    One more thing I noticed:
    While trying to solve the problem I deleted all instances from that UserControl on the main form and kept only one instance.
    I noticed it worked fine alone.
    So adding more instances is making the problem in my case.

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: How to Duplicate a Panel with it's all controls?

    I realised why my settings weren't being changed. The code I provided would update the setting if the user control property was set in code but the TextBox1TextChanged event was not being raised if the user entered text into the TextBox via the UI. Without the event, the setting cannot be updated. My code would have to be modified like so:
    vb.net Code:
    1. Public Event TextBox1TextChanged As EventHandler
    2.  
    3. Public Property TextBox1Text As String
    4.     Get
    5.         Return TextBox1.Text
    6.     End Get
    7.     Set
    8.         If TextBox1.Text <> Value Then
    9.             TextBox1.Text = Value
    10.             OnTextBox1TextChanged(EventArgs.Empty)
    11.         End If
    12.     End Set
    13. End Property
    14.  
    15. Protected Sub OnTextBox1TextChanged(e As EventArgs)
    16.     RaiseEvent TextBox1TextChanged(Me, e)
    17. End Sub
    18.  
    19. Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
    20.     OnTextBox1TextChanged(EventArgs.Empty)
    21. End Sub

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: How to Duplicate a Panel with it's all controls?

    FYI, I just tested my new code on a form with one TextBox and three instances of a user control containing a TextBox and all four controls were bound to settings and I could successfully enter text in each one, close the app, run the app again and see the text restored.

  11. #11

    Thread Starter
    Hyperactive Member Joye's Avatar
    Join Date
    Jul 2009
    Posts
    256

    Re: How to Duplicate a Panel with it's all controls?

    Quote Originally Posted by jmcilhinney View Post
    FYI, I just tested my new code on a form with one TextBox and three instances of a user control containing a TextBox and all four controls were bound to settings and I could successfully enter text in each one, close the app, run the app again and see the text restored.
    That worked Just fine
    But guess what?
    I needed to make a new blank project other than the one I was testing on and it worked.
    So i'm wondering if there are some properties that I might changed on the old test form??
    What are the necessary properties that I should take care of ??

    And Thank You From my Deep Heart

  12. #12
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: [RESOLVED] How to Duplicate a Panel with it's all controls?

    I can't think of anything that would cause the symptoms you described off the top of my head. I can only suggest that you make changes to your test project to make it more and more like your existing project and see if and when it breaks.

  13. #13

    Thread Starter
    Hyperactive Member Joye's Avatar
    Join Date
    Jul 2009
    Posts
    256

    Re: [RESOLVED] How to Duplicate a Panel with it's all controls?

    Quote Originally Posted by jmcilhinney View Post
    I can't think of anything that would cause the symptoms you described off the top of my head. I can only suggest that you make changes to your test project to make it more and more like your existing project and see if and when it breaks.
    Doing now
    Really Happy to solve this and many problems on this great Forum.
    Thank you very much

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