How to access a number of dynamically created user controls
-
I have two User Controls, _ucDateListing and _ucImageListing. In my main form I declare ucDateListing: Private ucDateListing as New _ucDateListing ucDateListing has two controls - a DataGridView (dgvDates) and a Panel (pnlImages). I then dynamically add this control to a panel control on the main form. I populate the DGV with data from a DB. For every date (no duplicates), for every row in other words, I add ucImageListing control to the ucDateListing.pnlImages panel.
'Define a new _ucConsultationImages control
Dim ucImageListing As New _ucImageListing'Set the properties of the control before adding it With ucImageListing .Location = New Point(0, 0) .Dock = DockStyle.Fill .Tag = DBData.Rows(i).Item(0) 'Date used to identify control .Visible = False End With 'Add the ImageListing control to the DateListing control on the main form frmMain1.ucDateListing.pnlImageListing.Controls.Add(ucImagesListing)
My question is, how do I access each of these user controls individually every time I select a date in the DGV. I am currently looping through all the added controls and test the .Tag field to the selected date from the DGV. All that works fine.
'Get ucImageListing that corresponds to selected date in DGV
For Each ctrlImageListing As Control In pnlImageListing.Controls
If TypeOf ctrlImageListing Is _ucImageListing Then
If ctrlImageListing.Tag.ToString = dgvDates.Rows(dgvDates.CurrentRow.Index).Cells(0).Value.ToString Then
ctrlImageListing.Visible = True
Exit For
End If
End If
NextNow that I have found the control, how do I access it when I need to for example load images into the Listview (lvImages) of that specific dynamically added control ? Update: Here is the solution to my own problem: I managed to figure it out, eventually. By adding the following code just before I "Exit" the For Each loop, allows me to access the "found" control like any other predefined control.
Dim Imagelst As _ucImageList
Imagelst = ctrlImagesListWith Imagelst .lvImageListing.Height = 20 'Test to see if I can resize the listview in the user control End With
There are probably other sex