ContainerControl.ActiveControl 属性

定义

获取或设置容器控件上的活动控件。

C#
[System.ComponentModel.Browsable(false)]
public System.Windows.Forms.Control ActiveControl { get; set; }

属性值

Control

ContainerControl 的当前活动 Control

实现

属性

例外

分配的 Control 未能激活。

示例

下面的代码示例继承自 ScrollableControl 类并实现 IContainerControl 接口。 实现将添加到 ActiveControl 属性和 ActivateControl 方法。

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

注解

ActiveControl 属性激活或检索容器控件上的活动控件。

为了从此属性接收有效值,调用它的对象必须包含或包含在它所调用的控件中。 如果一个窗体尝试调用另一个窗体 ActiveControl 的属性,它将接收未定义的值。 在这种情况下,需要在表单之间定义自己的通信机制才能传递此数据。

适用于

产品 版本
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8
Windows Desktop 3.0, 3.1, 5, 6, 7

另请参阅