Export (0) Print
Expand All
Expand Minimize

Control.OnControlAdded Method

Raises the ControlAdded event.

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)

protected:
virtual void OnControlAdded (
	ControlEventArgs^ e
)
protected void OnControlAdded (
	ControlEventArgs e
)
protected function OnControlAdded (
	e : ControlEventArgs
)

Parameters

e

A ControlEventArgs that contains the event data.

Called when a child control is added to the control.

Raising an event invokes the event handler through a delegate. For more information, see Raising an Event.

The OnControlAdded 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.

Notes to Inheritors When overriding OnControlAdded in a derived class, be sure to call the base class's OnControlAdded method so that registered delegates receive the event.

The following code example uses ControlAdded to add a control to a form and displays the name of the added control in a 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 += gcnew System::Windows::Forms::ControlEventHandler( this, &Form1::Control_Removed );
      this->ControlAdded += gcnew System::Windows::Forms::ControlEventHandler( this, &Form1::Control_Added );
   }

   void Control_Added( Object^ /*sender*/, System::Windows::Forms::ControlEventArgs^ e )
   {
      MessageBox::Show( String::Format( "The control named {0} has been added to the form.", e->Control->Name ) );
   }

   void Control_Removed( Object^ /*sender*/, System::Windows::Forms::ControlEventArgs^ e )
   {
      MessageBox::Show( String::Format( "The control named {0} has been removed from the form.", e->Control->Name ) );
   }

   // Click event handler for a Button control. Adds a TextBox to the form.
   void addControl_Click( Object^ /*sender*/, System::EventArgs^ /*e*/ )
   {
      // Create a new TextBox control and add it to the form.
      TextBox^ textBox1 = gcnew TextBox;
      textBox1->Size = System::Drawing::Size( 100, 10 );
      textBox1->Location = 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.
   void removeControl_Click( Object^ /*sender*/, System::EventArgs^ /*e*/ )
   {
      // Loop through all controls in the form's control collection.
      IEnumerator^ myEnum = this->Controls->GetEnumerator();
      while ( myEnum->MoveNext() )
      {
         Control^ tempCtrl = safe_cast<Control^>(myEnum->Current);
         
         // Determine whether the control is textBox1,
         // and if it is, remove it.
         if ( tempCtrl->Name->Equals( "textBox1" ) )
         {
            this->Controls->Remove( tempCtrl );
         }
      }
   }

// 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.add_ControlRemoved(new System.Windows.Forms.
        ControlEventHandler(this.Control_Removed));
    this.add_ControlAdded(new System.Windows.Forms.
        ControlEventHandler(this.Control_Added));
} //Form1_Load

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

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

// 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.set_Size(new Size(100, 10));
    textBox1.set_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.set_Name("textBox1");

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

// 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.
    for (int iCtr = 0; iCtr < this.get_Controls().get_Count(); iCtr++) {
        Control tempCtrl = this.get_Controls().get_Item(iCtr);
        
        // Determine whether the control is textBox1,
        // and if it is, remove it.
        if (tempCtrl.get_Name().Equals("textBox1")) {
            this.get_Controls().Remove(tempCtrl);
        }
    }
} //removeControl_Click

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

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

.NET Framework

Supported in: 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft