Search code examples
c#vb.netdynamiccomposite

vb.net dynamic accesing dynamic controls


I have placed a FlowLayoutPanel in a WinForm and created a composite control named ContactW

With a loop I have filled the FlowLayoutPanel with several ContactW objects

For index = 0 To 99
    Dim Contact As New ContactW.BaseContactW()
    Contact.Tag = index
    'Here I could set properties
    '....
    AddHandler Contact.ContactClickEvt, AddressOf GenericClick
    flp_ContaninerContacts.Controls.Add(Contact)
Next

GenericClick gets the ContactW object, and I can modify it's properties

Public Sub GenericClick(ByVal sender As System.Object, ByVal e As System.EventArgs)
    Dim cwa As ContactW.BaseContactW = CType(sender, ContactW.BaseContactW)
    MessageBox.Show("you have clicked " & CType(CType(sender, System.Object).Tag, String))
    cwa.ContactName = "Robert"
End Sub

But I would like to modify properties of a ContactW using index or Tag for example like this:

Contact(21).ContactName = "Julia"
Contact(21).ContactAge = 35
Contact(21).ContactStatus = "Married"

How could I achieve that?


Solution

  • The Controls collection that you added the controls to in the first place is a collection, so you just index it like you would any other collection.