IContainerControl IContainerControl IContainerControl IContainerControl Interface

定义

为某控件提供功能,使其用作其他控件的父级。Provides the functionality for a control to act as a parent for other controls.

public interface class IContainerControl
public interface IContainerControl
type IContainerControl = interface
Public Interface IContainerControl
派生

示例

下面的示例从继承ScrollableControl类并实现IContainerControl接口。The following example inherits from the ScrollableControl class and implements the IContainerControl interface. 实现添加到ActiveControl属性和ActivateControl方法。Implementation is added to the ActiveControl property and the ActivateControl method.

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

public ref 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;
   }

   property Control^ ActiveControl 
   {
      // Add implementation to the IContainerControl.ActiveControl property.
      virtual Control^ get()
      {
         return activeControl;
      }

      virtual void set( 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.
   virtual 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;
   }
};
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;
		}
	}
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  

实施者说明

在你想要父控件的集合的类中实现此接口。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. 类中实现时ActivateControl(Control)采用Control作为参数,并激活指定的控件。When implemented in a class, ActivateControl(Control) takes a Control as a parameter and activates the specified control. ActiveControl属性激活或检索处于活动状态的控件。The ActiveControl property activates or retrieves the control that is active.

在最常见的情况下,不需要直接实现此接口。In most common scenarios, you do not need to directly implement this interface. 例如,如果您创建一个 Windows 控件库项目,Visual Studio 为您生成初始类。For example, if you create a Windows Control Library project, Visual Studio generates an initial class for you. 类继承自UserControl类,并UserControl实现IContainerControl为您。That class inherits from the UserControl class, and UserControl implements IContainerControl for you.

属性

ActiveControl ActiveControl ActiveControl ActiveControl

获取或设置该容器控件上的活动控件。Gets or sets the control that is active on the container control.

方法

ActivateControl(Control) ActivateControl(Control) ActivateControl(Control) ActivateControl(Control)

激活指定的控件。Activates a specified control.

适用于

另请参阅