1

I was wondering how I would duplicate a TextBox, not just the text inside of it. TextBox.copy copies the text selected inside the TextBox, which is not quite what I need. I am using .

  • 1
    Just instantiate a new Textbox and initialize it with the same properties as the one that you want to duplicate. Dim textBox As New TextBox – F0r3v3r-A-N00b May 29 '17 at 13:09
  • did you try this? [How to Clone/Serialize/Copy & Paste a Windows Forms Control](https://www.codeproject.com/kb/miscctrl/controlclonetst.aspx) – Mederic May 29 '17 at 13:13
  • @Mederic No, i didn't i will now :) –  May 29 '17 at 13:16
  • There's [a question](https://stackoverflow.com/questions/3473597/it-is-possible-to-copy-all-the-properties-of-a-certain-control-c-window-forms) about copying properties. – the_lotus May 29 '17 at 13:50
  • You can create extension to clone any control like in here https://stackoverflow.com/a/10267292/1662459 – G J Jun 01 '17 at 07:54

1 Answers1

2

Please try this function :)

Private Function CopyControl(ByVal obj As Object, Optional ByVal locationX As Integer = 0, Optional ByVal locationY As Integer = 0) As Object
    Dim objnew As Object = Activator.CreateInstance(obj.GetType()) 'Create new control
    Dim oldprops As PropertyDescriptorCollection = TypeDescriptor.GetProperties(obj) 'Control properties
    Dim newprops As PropertyDescriptorCollection = TypeDescriptor.GetProperties(objnew) 'New control properties
    For i As Integer = 0 To oldprops.Count - 1 '
        newprops(i).SetValue(objnew, oldprops(i).GetValue(obj)) 'New control properties = Old control properties
    Next
    objnew.location = New Point(locationX, locationY) 'Set location
    Return objnew 'New control is copied
    'Im sorry my english is bad, I hope you understand..
End Function

Me.Controls.Add(CopyControl(TextBox1))

I'm sorry for my bad english. I'm turkish :D

Berken Usar
  • 23
  • 1
  • 2