Partager via


ControlCollection.GetEnumerator Méthode

Définition

Récupère un énumérateur qui peut itérer au sein de l’objet ControlCollection .

public:
 virtual System::Collections::IEnumerator ^ GetEnumerator();
public 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 Function GetEnumerator () As IEnumerator
Public Overridable Function GetEnumerator () As IEnumerator

Retours

Énumérateur à itérer dans la collection.

Implémente

Exemples

L’exemple de code suivant crée une méthode qui énumère la ControlCollection collection d’un Button contrôle. myButton Lorsque l’énumérateur est créé, la IsSynchronized propriété est vérifiée pour voir si l’opération est thread safe et si ce n’est pas le cas, la SyncRoot propriété est utilisée pour obtenir un objet pour rendre le thread d’opération sécurisé. Une fois l’énumération terminée, la valeur de la IsReadOnly propriété est écrite en tant que Text propriété d’un Label contrôle sur la page contenante.

// 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)
   {
       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

S’applique à

Voir aussi