Dynamic Controls

N

Nathan Carroll

I'd like to use the below inherited control that is incorporated into a
CollectionBase to Create controls on a usercontrol.
My question is it possible to copy/clone from the collection of the user
control. When i try to add the control from the collection I am only able
to add one control. See Below BuiltIt. My Solution is to loop through the
collection and set the properties of a new inherited control. This seems
the long way am I missing something?

<System.ComponentModel.DesignTimeVisible(False)> _
Public Class Columns
Inherits System.Windows.Forms.TextBox
End Class


Sub BuildIt()
Dim l As Columns
Dim j As Integer
Static rowtop As Integer
For j = 0 To 2
Dim r As New Panel
With r
..Width = Me.Width
..Top = rowtop
..Height = 24
..Left = 0
rowtop += .Height
End With
Static left As Integer
left = 0
For Each l In Me.m_ColumnHeaders
' Me.Controls.Add(l)
Dim tb As New Columns
With tb
'this is fine but what about databinding
Dim s As String
s = l.DataBindings.Item(0).BindingMemberInfo.BindingField().ToString
..Left = left
left = left + .Width + 2
..Text = l.Text
Try
..DataBindings.Add("Text", Datasource, s)
Catch ex As Exception
End Try
End With
r.Controls.Add(tb)
Next
Me.Controls.Add(r)
Next
End Sub
 
H

Herfried K. Wagner [MVP]

Hello,

Nathan Carroll said:
My question is it possible to copy/clone from the collection of the user
control. When i try to add the control from the collection I am only able
to add one control. See Below BuiltIt. [...]
Me.Controls.Add(r)

Maybe you can use 'Me.Controls.AddRange' to add an array of controls.
 
N

Nathan Carroll

Not sure that I used it right but when I tried suggestion ended up with only
one control. Trying to get three panels each with controls that corresponds
to the controls in collection.

Dim NewCol As Columns = New Columns
Sub BuildIt()
Dim l As Columns
Dim j As Integer
Static rowtop As Integer
For j = 0 To 2
Dim r As New Panel
With r
..Width = Me.Width
..Top = rowtop
..Height = 24
..Left = 0
rowtop += .Height
End With
Static left As Integer
left = 0
For Each l In Me.m_ColumnHeaders
'if i place
'Dim NewCol As Columns = New Columns
'here, then the control seems to be added once and for each panel and
subtracted on subsequent additions

' Me.Controls.Add(l)
Dim tb As New Columns
With tb
'this is fine but what about databinding
Dim s As String
s = l.DataBindings.Item(0).BindingMemberInfo.BindingField().ToString
..Left = left
left = left + .Width + 2
..Text = l.Text
Try
..DataBindings.Add("Text", Datasource, s)
Catch ex As Exception
End Try
End With
'allows only one addition
NewCol = l
r.Controls.AddRange(New Control() {NewCol})

'will allow multiples
'r.Controls.Add(tb)
Next



'have no problem with this step
Me.Controls.Add(r)
Next
End Sub



Herfried K. Wagner said:
Hello,

Nathan Carroll said:
My question is it possible to copy/clone from the collection of the user
control. When i try to add the control from the collection I am only able
to add one control. See Below BuiltIt. [...]
Me.Controls.Add(r)

Maybe you can use 'Me.Controls.AddRange' to add an array of controls.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top