Solved

How to reference a dynamically created object in vb.net

Posted on 2012-03-15
4
220 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

734 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