Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

ControlEventArgs Class

Provides data for the ControlAdded and ControlRemoved events.

System.Object
  System.EventArgs
    System.Windows.Forms.ControlEventArgs

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)
public class ControlEventArgs : EventArgs

The ControlEventArgs type exposes the following members.

  NameDescription
Public methodControlEventArgsInitializes a new instance of the ControlEventArgs class for the specified control.
Top
  NameDescription
Public propertyControlGets the control object used by this event.
Top
  NameDescription
Public methodEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Top

For more information about handling events, see Consuming Events.

The following code example demonstrates the use of the ControlAdded and ControlRemoved events. The example requires that two Button controls are added to the form and connected to the addControl_Click and removeControl_Click event handling methods.

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

Supported in: 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Show:
© 2014 Microsoft. All rights reserved.