Matt
Matt

Reputation: 1570

VB.NET Controls Not Visible

I am trying to see all the labels in Me.Controls and when I use:

For Each Control As Label In Me.Controls.OfType(Of Label)()
    MsgBox(Control.Name.ToString)
Next

it only shows the labels that have NOT been renamed. Am I doing something wrong here?

Upvotes: 1

Views: 1365

Answers (1)

LarsTech
LarsTech

Reputation: 81610

For the most part, your code looks right, unless you have labels inside other container controls like Panels and GroupBoxes. In which case, you would need to loop through those containers, too.

Here is an example:

Dim allContainers As New Stack(Of Control)
allContainers.Push(Me)
While allContainers.Count > 0
  For Each item As Control In allContainers.Pop.Controls
    If item.Controls.Count > 0 Then
      allContainers.Push(item)
    End If
    If TypeOf item Is Label Then
      MessageBox.Show("Label.Name = " + item.Name)
    End If
  Next
End While

Upvotes: 3

Related Questions