Solved

For each in Me.Controls Part 2

Posted on 2006-07-17
10
566 Views
Last Modified: 2012-08-13
Chaosian, shall I, in my form_Load() do this:

        For Each ctrl As Control In Me.Controls
            If TypeOf (ctrl) Is Button Then
                myButtons.Add(ctrl.Name, Nothing)
            ElseIf TypeOf (ctrl) Is Label Then
                myLabels.Add(ctrl.Name, Nothing)
            End If
        Next

Something like that, to put all my controls in my Dictionnary tables
Not sure what to put in the value parameter also
0
Comment
Question by:PhilippeRenaud
  • 6
  • 2
  • 2
10 Comments
 
LVL 23

Expert Comment

by:apresto
ID: 17123179
Hi PhilippeRenaud,

the value parameter is what is displayed to the user.  Are myButtons and myLabels Hashtable?

Apresto
0
 
LVL 1

Author Comment

by:PhilippeRenaud
ID: 17123193
It was a Dictionnary
0
 
LVL 1

Author Comment

by:PhilippeRenaud
ID: 17123219
Im noob with HashTable and Dicotnnary actually.
I just gave points in part 1 for the Dictonnary example, i found it great but, I dont know in fact wich one is the Best for wich situation. anyway.
0
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 
LVL 23

Expert Comment

by:apresto
ID: 17123268
could you give a link to the first question :o)
0
 
LVL 1

Author Comment

by:PhilippeRenaud
ID: 17123296
0
 
LVL 24

Accepted Solution

by:
Jeff Certain earned 500 total points
ID: 17123343
Dictionary is the new type-safe implementation of the collections. You can only use Dictionary and the other generic classes in VS2005.

Your posted code is close... but you need to add the actual control.

For Each ctrl As Control In Me.Controls
            If TypeOf (ctrl) Is Button Then
                myButtons.Add(ctrl.Name, ctrl)
            ElseIf TypeOf (ctrl) Is Label Then
                myLabels.Add(ctrl.Name, ctrl)
            End If
        Next

This way, you'll get the control back as the value from the Dictionary.

Note: trying to put anything other than a button in a Dictionary(of string, button) won't work... you were right to use a separate dictionary for labels.
0
 
LVL 1

Author Comment

by:PhilippeRenaud
ID: 17123439
Yes

I did :

    Dim myButtons As Dictionary(Of String, Button)
    Dim myLabels As Dictionary(Of String, Label)

Also, i am getting an error here:
myLabels.Add(ctrl.Name, ctrl)

not sure why, let me try again
0
 
LVL 24

Expert Comment

by:Jeff Certain
ID: 17123456
What is the error?
0
 
LVL 1

Author Comment

by:PhilippeRenaud
ID: 17123615
ok it focus on the lign :  myLabels.Add(ctrl.Name, ctrl)

saying:

System.NullReferenceException was unhandled
  Object reference not set to an instance of an object.
0
 
LVL 1

Author Comment

by:PhilippeRenaud
ID: 17124592
dont understand why
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Suggested Solutions

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

821 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