ContainerControl.ActiveControl 屬性

定義

取得或設定容器控制項上的作用中控制項。

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

屬性值

Control

目前在 Control 上作用中的 ContainerControl

實作

屬性

例外狀況

所指派的 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

另請參閱