Solved

For each in Me.Controls Part 2

Posted on 2006-07-17
10
570 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
[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
  • 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
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!

 
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

Independent Software Vendors: 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

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
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…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

739 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