?
Solved

How to reference a dynamically created object in vb.net

Posted on 2012-03-15
4
Medium Priority
?
227 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 86

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 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses
Course of the Month16 days, 5 hours left to enroll

850 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