2012-03-26 78 views
1

我想看到Me.Controls所有的標籤,當我用:VB.NET控件不可見

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

那隻能說明沒有被重命名的標籤。我在這裏做錯了什麼?

+1

你是什麼意思 '更名爲'? – Steve 2012-03-26 22:28:47

+0

現在重新閱讀你的問題。你的問題沒有提到你的標題「控制不可見」的任何內容。 – LarsTech 2012-03-26 22:41:01

回答

3

大部分情況下,您的代碼看起來不錯,除非您在其他容器控件(如Panels和GroupBoxes)內部有標籤。在這種情況下,您也需要遍歷這些容器。

下面是一個例子:

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 
+0

好的,這正是問題所在!謝謝! – Matt 2012-03-27 02:58:19