ControlCollection.GetEnumerator ControlCollection.GetEnumerator ControlCollection.GetEnumerator ControlCollection.GetEnumerator Method

定義

ControlCollection オブジェクトを反復処理できる列挙子を取得します。Retrieves an enumerator that can iterate through the ControlCollection object.

public:
 virtual System::Collections::IEnumerator ^ GetEnumerator();
public virtual System.Collections.IEnumerator GetEnumerator ();
abstract member GetEnumerator : unit -> System.Collections.IEnumerator
override this.GetEnumerator : unit -> System.Collections.IEnumerator
Public Overridable Function GetEnumerator () As IEnumerator

戻り値

コレクションを反復処理する列挙子。The enumerator to iterate through the collection.

実装

次のコード例は、列挙するメソッドを作成、ControlCollectionのコレクションをButtonコントロール、myButtonします。The following code example creates a method that enumerates through the ControlCollection collection of a Button control, myButton. 列挙子が作成されたときに、IsSynchronizedプロパティをチェックして、操作はスレッド セーフである場合とそうでない場合、SyncRoot操作スレッドを安全にオブジェクトを取得するプロパティを使用します。When the enumerator is created, the IsSynchronized property is checked to see if the operation is thread safe, and if it is not, the SyncRoot property is used to obtain an object to make the operation thread safe. 列挙が完了すると、値のIsReadOnlyとしてプロパティの書き込み、TextのプロパティをLabel格納されているページ上のコントロール。When the enumeration is completed, the value of the IsReadOnly property is written as the Text property of a Label control on the containing page.

// Create a method that enuberates through a 
// button//s ControlCollection in a thread-safe manner.  
public void ListControlCollection(object sender, EventArgs e)
{
   IEnumerator myEnumerator = myButton.Controls.GetEnumerator();

   // Check the IsSynchronized property. If False,
   // use the SyncRoot method to get an object that 
   // allows the enumeration of all controls to be 
   // thread safe.
   if (myButton.Controls.IsSynchronized == false)
   {
       lock (myButton.Controls.SyncRoot)
       {
           while (myEnumerator.MoveNext())
           {

               Object myObject = myEnumerator.Current;

               LiteralControl childControl = (LiteralControl)myEnumerator.Current;
               Response.Write("<b><br /> This is the  text of the child Control  </b>: " +
                              childControl.Text);
           }
           msgReadOnly.Text = myButton.Controls.IsReadOnly.ToString();
       }
   }       
}
' Create a method that enuberates through a 
' button's ControlCollection in a thread-safe manner.  
Public Sub ListControlCollection(sender As Object, e As EventArgs)
   Dim myEnumerator As IEnumerator = myButton.Controls.GetEnumerator()

   ' Check the IsSynchronized property. If False,
   ' use the SyncRoot method to get an object that 
   ' allows the enumeration of all controls to be 
   ' thread safe.
   If myButton.Controls.IsSynchronized = False Then
     SyncLock myButton.Controls.SyncRoot
       While (myEnumerator.MoveNext())

       Dim myObject As Object  = myEnumerator.Current
           
         Dim childControl As LiteralControl = CType(myEnumerator.Current, LiteralControl)
         Response.Write("<b><br /> This is the  text of the child Control  </b>: " & _
                        childControl.Text)
       End While
      msgReadOnly.Text = myButton.Controls.IsReadOnly.ToString()
      
      End SyncLock
   End If       
 End Sub

適用対象

こちらもご覧ください