Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

For each in Me.Controls Part 2

Posted on 2006-07-17
10
Medium Priority
?
582 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
Technology Partners: 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 24

Accepted Solution

by:
Jeff Certain earned 1500 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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

721 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