Este artigo foi traduzido por máquina. Para visualizar o arquivo em inglês, marque a caixa de seleção Inglês. Você também pode exibir o texto Em inglês em uma janela pop-up, movendo o ponteiro do mouse sobre o texto.
Tradução
Inglês

Método Control.OnControlAdded (ControlEventArgs)

 

Aciona o evento ControlAdded.

Namespace:   System.Windows.Forms
Assembly:  System.Windows.Forms (em System.Windows.Forms.dll)

protected virtual void OnControlAdded(
	ControlEventArgs e
)

Parâmetros

e
Type: System.Windows.Forms.ControlEventArgs

Um ControlEventArgs que contém os dados do evento.

Called when a child control is added to the control.

Raising an event invokes the event handler through a delegate. For more information, see Handling and Raising Events.

The M:System.Windows.Forms.Control.OnControlAdded(System.Windows.Forms.ControlEventArgs) method also allows derived classes to handle the event without attaching a delegate. This is the preferred technique for handling the event in a derived class.

Observações para os Herdeiros:

When overriding M:System.Windows.Forms.Control.OnControlAdded(System.Windows.Forms.ControlEventArgs) in a derived class, be sure to call the base class's M:System.Windows.Forms.Control.OnControlAdded(System.Windows.Forms.ControlEventArgs) method so that registered delegates receive the event.

The following code example uses E:System.Windows.Forms.Control.ControlAdded to add a control to a form and displays the name of the added control in a T:System.Windows.Forms.MessageBox.

// This example demonstrates the use of the ControlAdded and
// ControlRemoved events. This example assumes that two Button controls
// are added to the form and connected to the addControl_Click and
// removeControl_Click event-handler methods.
private void Form1_Load(object sender, System.EventArgs e)
{
    // Connect the ControlRemoved and ControlAdded event handlers
    // to the event-handler methods.
    // ControlRemoved and ControlAdded are not available at design time.
    this.ControlRemoved += new System.Windows.Forms.ControlEventHandler(this.Control_Removed);
    this.ControlAdded += new System.Windows.Forms.ControlEventHandler(this.Control_Added);
}

private void Control_Added(object sender, System.Windows.Forms.ControlEventArgs e)
{
    MessageBox.Show("The control named " + e.Control.Name + " has been added to the form.");
}

private void Control_Removed(object sender, System.Windows.Forms.ControlEventArgs e)
{
    MessageBox.Show("The control named " + e.Control.Name + " has been removed from the form.");
}

// Click event handler for a Button control. Adds a TextBox to the form.
private void addControl_Click(object sender, System.EventArgs e)
{
    // Create a new TextBox control and add it to the form.
    TextBox textBox1 = new TextBox();
    textBox1.Size = new Size(100,10);
    textBox1.Location = new Point(10,10);
    // Name the control in order to remove it later. The name must be specified
    // if a control is added at run time.
    textBox1.Name = "textBox1";

    // Add the control to the form's control collection.
    this.Controls.Add(textBox1);
}

// Click event handler for a Button control.
// Removes the previously added TextBox from the form.
private void removeControl_Click(object sender, System.EventArgs e)
{
    // Loop through all controls in the form's control collection.
    foreach (Control tempCtrl in this.Controls)
    {
        // Determine whether the control is textBox1,
        // and if it is, remove it.
        if (tempCtrl.Name == "textBox1")
        {
            this.Controls.Remove(tempCtrl);
        }
    }
}

.NET Framework
Disponível desde 1.1
Retornar ao início
Mostrar: