Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Referecing a listbox created "on the fly"

Status
Not open for further replies.

vonehle

Programmer
May 16, 2006
35
US
I'm trying to add data to some listboxes that were created on the fly. Each listbox was named "lstPL" & intPlayers in a loop. I can't figure out how to reference these listboxes in a loop later on.

The portion "lstPL(intPlayers)" is incorrect. How do I do this?

Code:
While intDealCard <= 51
lstPL(intPlayers).Items.Add(DealCards.ShuffleCards(intDealCard).numbers _
& DealCards.ShuffleCards(intDealCard).suit)
intDealCard += 1
EndWhile
 
Perhaps you need to loop through each control in your class, then get it when it meets your criterias:
Code:
'Declare a listbox type control
Dim lst As ListBox
'Loop through each control
For Each ctr As Control In Me.Controls
   If TypeOf ctr Is ListBox Then
      If ctr.Name = "lstPL" & intPlayers Then
         lst = ctr
         Exit For
      End If
   End If
Next

If lst Is Nothing Then Exit Sub

While intDealCard <= 51
   lst.Items.Add(DealCards.ShuffleCards(intDealCard).numbers _
   & DealCards.ShuffleCards(intDealCard).suit)
   intDealCard += 1
EndWhile

Good luck
 
An alternative is to add each listbox to an array of listboxes as you create them. You can then use sort of syntax that you originally tried.

Code:
Dim lstPL(intPlayers) as ListBox

' create each list box and add it to the array
For i as integer = 1 to intPlayers
    lstPL(i) = New ListBox
    ' add any other initialisation required here
    With lstPL(i)
    .
    .
    .
    End With  
Next

Bob Boffin
 
Hello,

Check out this FAQ: Get control by name using reflection faq796-5698
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top