Solved

For each in Me.Controls Part 2

Posted on 2006-07-17
10
557 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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 …
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…

947 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now