Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. How to access a number of dynamically created user controls

How to access a number of dynamically created user controls

Scheduled Pinned Locked Moved Visual Basic
questiondatabasetutorialwinformshelp
1 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Tino Fourie
    wrote on last edited by
    #1

    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
    Next

    Now 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 = ctrlImagesList

    With Imagelst
       .lvImageListing.Height = 20 'Test to see if I can resize the listview in the user control
    End With
    

    There are probably other sex

    1 Reply Last reply
    0
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups