# [.net] Duplicating controls in VB.NET

This topic is 5089 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm working with VB.NET to create a simple Z80 assembly tool. What it has is a series of tab pages, each one with a text box on it. What I want to be able to do is have a Sub called "addTab" where I can pass 2 parameters - the filename, which appears on the tab button and the text for the source file. What it would then do is add a new tab (I can do that) and a new text box onto that tab with the source file text in it, so you can switch between the source files easily. To create a new textbox, I've tried
Dim x as New TextBox()
x = txtEditBox
...where txtEditBox is the existing text box which has all the properties set up. However, if I now add 'x' to a tab page, it'll actually move the txtEditBox to the tab page - I'm guessing because when I go x = txtEditBox it's copying a handle to the existing text box, rather than all its properties. Any hints, or ideas?

##### Share on other sites
Once you do:

Dim x as New TextBox()

you have a new textbox. Just add it to the tab page's control collection.

Dim tp As New TabPage()Dim txt As New TextBox()txt.Text = "Textbox " & TabControl1.Controls.Count + 1tp.Controls.Add(txt)tp.Text = "Page " & TabControl1.Controls.Count + 1TabControl1.TabPages.Add(tp)

##### Share on other sites
Unfortunately that just creates it with the default properties - ideally I'd like to create it based on the properties of an existing one.
In VB6 you could just do Dim x as New txtSample, where txtSample was the text box with the properties you wanted to duplicate.

##### Share on other sites
Unfortunately, it doesn't look like the Control class implements the ICloneable interface, which I would have expected it to. You'll have to copy the relevant properties yourself.

1. 1
2. 2
3. 3
4. 4
JoeJ
12
5. 5
Rutin
11

• 12
• 16
• 13
• 20
• 12
• ### Forum Statistics

• Total Topics
632178
• Total Posts
3004598

×