Solved

How to reference a dynamically created object in vb.net

Posted on 2012-03-15
4
216 Views
Last Modified: 2012-04-06
HI..

I have created various panels using :

 Dim panel1 As New Panel()
        Dim height = 50
        Dim parent_width = staff_panel_list2.Width
        staff_panel_list2.Height = height * staff_list_counter
        panel1.Name = "staff" & staff_list_counter
        panel1.Size = New Size(parent_width, height)
        panel1.Location = New Point(0, 0 + (height * (staff_list_counter - 1)))
        panel1.Controls.Add(label1)
        staff_panel_list2.Controls.Add(panel1)

Which works fine.. panels stacked within the staff_panel_list2 panel.

I now need to be able to reference the stacked panels eg:

staff1.visible=false
but I need to be able to do it programatically like :
("staff" & counter).visible=false

Is there a way to do this?
thanks
0
Comment
Question by:jellydeal
  • 2
4 Comments
 
LVL 17

Expert Comment

by:nepaluz
ID: 37727304
You can not use variables like that. Give your panels names, e.g
Panel1.Name = "Panel1"

Open in new window

, then you can iterate through the controls like:
For Each x As Control In Me.Controls
   If x.Name = "staff" & counter Then x.Visible = False
End If

Open in new window

0
 

Author Comment

by:jellydeal
ID: 37727332
Is there a way I can reference the panel directly using a variable for its name, similar to javascript though, like :

cnt=1
panel & cnt.visible=false

I see how youw way can work but I dont want to have to loop through all controls every time.

thanks
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 37727353
You can use a Dictionary with the Panel name as the key:

    Private pnls as New Dictionary(Of String, Panel)
   
    ...
        pnls.Add(panel1.Name, panel1)

Later, you can do:

    pnls("panel1").xxx = yyy

See Dictionary:
http://msdn.microsoft.com/en-us/library/xfhwa508.aspx
0
 
LVL 17

Accepted Solution

by:
nepaluz earned 500 total points
ID: 37727375
The only way I can think of without overtly looping through is by LINQ (which actually loops in the background!)
Dim gf = From x As Control In Me.Controls Where TypeOf x Is Panel AndAlso x.Name = "staff" & counter Select x.Visible = False

Open in new window

where you have counter defined as a variable
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
This video discusses moving either the default database or any database to a new volume.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now