Clone Winform control

2020-03-24 02:51发布

问题:

I have a groupox in a VB.NET winform app. The groupbox contains a few Labels, Textboxes and Checkboxes. This was created by simply dragging the controls out of VS toolbox.

What I need to do is take the 1 Groupbox and at runtime create multiple Groupboxes to display based on user selection. Instead of dynamically creating the Groupboxes and other controls nested inside, is there a way to clone or copy the original one.

Then I'd just change the properties. Label text, Textbox text, etc. And the location of the Groupbox in the layout.

回答1:

What you could do is create a user control based upon your groupbox which would allow its reuse. You could then create the instances you require at runtime and add them to the form.

Maybe suggest that you look into creating user controls for winforms.



回答2:

Could this be a help to you?
How to Clone/Serialize/Copy & Paste a Windows Forms Control