2014-03-01 76 views
1

我在VB10中用50個按鈕創建了一個窗體。我如何使用for循環管理其可見性?
例如,我想要做這樣的事情:VB 10如何控制多個按鈕

For i As Integer = 1 To 50  
     Button(i).Visible = False  
    Next 

我怎麼能映射我目前有多少?我想避免寫50次。
預先感謝您的幫助。

+0

你應該能夠遍歷控件的集合。如果他們的類型是按鈕,然後將其可見變爲false。 – user3358344

回答

0

如果名稱是Button1的,Button2的,等那麼這將工作:

For i As Integer = 1 To 50  
    Me.Controls("Button" & i.ToString).Visible = False  
Next 
+0

只有當窗體本身直接包含所有按鈕時才能使用。如果他們在不同的容器中呢?如果它們分佈在多個容器上呢?... –

1

下面是如何獲得的按鈕,不管它們是什麼容器,甚至多者:

Dim matches() As Control 
    For i As Integer = 1 To 50 
     matches = Me.Controls.Find("Button" & i, True) 
     If matches.Length > 0 AndAlso TypeOf matches(0) Is Button Then 
      Dim btn As Button = DirectCast(matches(0), Button) 
      btn.Visible = False 
     End If 
    Next