Solved

How to reference a dynamically created object in vb.net

Posted on 2012-03-15
4
219 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

828 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