複数のコントロールまとめてプロパティを設定


<方法1>
Tagプロパティを使用する方法

1.色を変更したいコントロールのTagプロパティに予め値をセットする
2.全コントロールFor文で検索し、Tagの値が上記のコントロールのみ色を変更する
※TableLayoutPanel等に格納されているコントロールは<方法2>でなければ
 変更できない

例:Tagプロパティが"1"のコントロールのみBackColorをChocolateに変更する
       Dim oControl As Control

       For Each oControl In Me.Controls
           If oControl.Tag = "1" Then
               oControl.BackColor = Color.Chocolate
           End If
       Next



<方法2>
1.子コントロールを含めた、TextBoxのEnabledプロパティを設定する

   '子コントロールを含めた全てのコントロールを取得
   Public Function GetAllControls(ByVal top As Control) As Control()
       Dim buf As ArrayList = New ArrayList
       For Each c As Control In top.Controls
           buf.Add(c)
           buf.AddRange(GetAllControls(c))
       Next
       Return CType(buf.ToArray(GetType(Control)), Control())
   End Function
   
   Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
   
       Dim c As Control
       Dim Allc As Control() = GetAllControls(Me)  'コントロール取得
       For Each c In Allc
          If TypeOf c Is TextBox Then
               CType(c, TextBox).Enabled = False
           End If
       Next
   End Sub



























.
最終更新:2008年10月31日 15:38