簡體   English   中英

確定自定義Winforms控件的設計時上下文

[英]Determine design-time context of a custom winforms control

假設我有一個自定義WinForms控件:

public class MyBaseControl : Control
{
     ...
}

擴展如下:

public class MyControl : MyBaseControl
{
     ...
}

通過檢查this.DesignMode屬性標志,可以很this.DesignMode地確定控件是否在視覺上進行設計,但是,有沒有一種方法可以確定MyControl本身是否在設計中,而不是在設計時在from上進行操作?


為了提供更多的說明,在MyControl類中,我試圖在設計組件本身時區分設計時:

“控制”設計師

在設計時將組件從工具箱添加到表單中時:

在此處輸入圖片說明

您可以檢查控件是否位於設計器的根目錄中。
您可以獲取IDesignerHost服務,然后檢查RootComponent屬性,以查看您的控件是否是當前設計器的根組件。

using System.Windows.Forms;
using System.ComponentModel.Design;
public partial class MyBaseControl : UserControl
{
    public MyBaseControl()
    {
        InitializeComponent();
    }

    public bool IsRootDesigner
    {
        get
        {
            var host = (IDesignerHost)this.GetService(typeof(IDesignerHost));
            if (host != null)
                return host.RootComponent == this;

            return false;
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM