我如何在vb.net中复制TextBox

时间:2017-05-29 13:04:46

标签: vb.net

我想知道如何复制textbox(不是它里面的文字,而是它自己的对象)? Textbox.copy只是复制Textbox内的文字 我正在使用

1 个答案:

答案 0 :(得分:0)

请尝试此功能:)

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))

我很抱歉我的英语不好。我是土耳其人:D