Export (0) Print
Expand All

IContainerControl Interface

Provides the functionality for a control to act as a parent for other controls.

For a list of all members of this type, see IContainerControl Members.

[Visual Basic]
Public Interface IContainerControl
[C#]
public interface IContainerControl
[C++]
public __gc __interface IContainerControl
[JScript]
public interface IContainerControl

Classes that Implement IContainerControl

Class Description
ContainerControl Provides focus management functionality for controls that can function as a container for other controls.

Remarks

Notes to Implementers:  Implement this interface in classes that you want to parent a collection of controls. The members of this interface allow you to activate a child control, or determine which control is currently active. When implemented in a class, ActivateControl takes a Control as a parameter and activates the specified control. The ActiveControl property activates or retrieves the control that is active.

In most common scenarios, you do not need to directly implement this interface. For example, if you create a Windows Control Library project, Visual Studio generates an initial class for you. That class inherits from the UserControl class, and UserControl implements IContainerControl for you.

Example

[Visual Basic, C#, C++] The following example inherits from the ScrollableControl class and implements the IContainerControl interface. Implementation is added to the ActiveControl property and the ActivateControl method.

[Visual Basic] 
Imports System
Imports System.Windows.Forms
Imports System.Drawing

   Public Class MyContainerControl
      Inherits ScrollableControl
      Implements IContainerControl 

      Private myActiveControl As Control
      
      Public Sub New()
         ' Make the container control Blue so it can be distinguished on the form.
         Me.BackColor = Color.Blue
         
         ' Make the container scrollable.
         Me.AutoScroll = True
      End Sub 
      
      ' Add implementation to the IContainerControl.ActiveControl property.
      Public Property ActiveControl() As Control Implements IContainerControl.ActiveControl
         Get
            Return Me.myActiveControl
         End Get
         
         Set
            ' Make sure the control is a member of the ControlCollection.
            If Me.Controls.Contains(value) Then
               Me.myActiveControl = value
            End If
         End Set
      End Property
      
      ' Add implementation to the IContainerControl.ActivateControl(Control) method.
      public Function ActivateControl(active As Control) As Boolean Implements IContainerControl.ActivateControl
         If Me.Controls.Contains(active) Then
            ' Select the control and scroll the control into view if needed.
            active.Select()
            Me.ScrollControlIntoView(active)
            Me.myActiveControl = active
            Return True
         End If
         Return False
      End Function 

   End Class  

[C#] 
using System;
using System.Windows.Forms;
using System.Drawing;

    public class MyContainer : ScrollableControl, IContainerControl
    {
        private Control activeControl;
        public MyContainer() 
        {
            // Make the container control Blue so it can be distinguished on the form.
            this.BackColor = Color.Blue;
            
            // Make the container scrollable.
            this.AutoScroll = true;
        }

        // Add implementation to the IContainerControl.ActiveControl property.
        public Control ActiveControl
        {
            get
            {
                return activeControl;
            }

            set
            {
                // Make sure the control is a member of the ControlCollection.
                if(this.Controls.Contains(value))
                {
                    activeControl = value;
                }
            }
        }

        // Add implementations to the IContainerControl.ActivateControl(Control) method.
        public bool ActivateControl(Control active)
        {
            if(this.Controls.Contains(active))
            {
                // Select the control and scroll the control into view if needed.
                active.Select();
                this.ScrollControlIntoView(active);
                this.activeControl = active;
                return true;
            }
            return false;
        }
    }

[C++] 

using namespace System;
using namespace System::Windows::Forms;
using namespace System::Drawing;

    public __gc class MyContainer : public ScrollableControl, public IContainerControl {
    private:
        Control*  activeControl;

    public:
        MyContainer() {
            // Make the container control Blue so it can be distinguished on the form.
            this->BackColor = Color::Blue;

            // Make the container scrollable.
            this->AutoScroll = true;
        }

        // Add implementation to the IContainerControl.ActiveControl property.
        __property Control* get_ActiveControl() {
            return activeControl;    
        }

        __property void set_ActiveControl(Control* value) {
            // Make sure the control is a member of the ControlCollection.
            if (this->Controls->Contains(value)) {
                activeControl = value;
            }    
        }

        // Add implementations to the IContainerControl.ActivateControl(Control) method.
        bool ActivateControl(Control* active) {
            if (this->Controls->Contains(active)) {
                // Select the control and scroll the control into view if needed.
                active->Select();
                this->ScrollControlIntoView(active);
                this->activeControl = active;
                return true;
            }
            return false;
        }
    };

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.Windows.Forms

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

Assembly: System.Windows.Forms (in System.Windows.Forms.dll)

See Also

IContainerControl Members | System.Windows.Forms Namespace | ContainerControl | Control

Show:
© 2014 Microsoft