2016-03-04 54 views
2

假設我有一個自定義的WinForms控制:確定一個自定義的設計時上下文的WinForms控制

public class MyBaseControl : Control 
{ 
    ... 
} 

其擴展,如:

public class MyControl : MyBaseControl 
{ 
    ... 
} 

通過檢查this.DesignMode屬性標誌是相當微不足道以確定控件是否在視覺上設計,然而有沒有一種方法可以確定MyControl本身是否正在設計,而不是在設計時被操縱?


爲了提供額外的澄清,該MyControl類中,我試圖設計時進行區分,當組件本身被設計:

"Control" designer

,並在添加組件以設計時從工具箱中形成:

enter image description here

+0

有人要求澄清,添加屏幕截圖以更清楚地表明我正在嘗試完成的是什麼。 – Lemonseed

+0

謝謝你的澄清:) –

回答

3

您可以檢查控件是否是設計者的根。
您可以獲得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; 
     } 
    } 
} 
+0

這正是我之後的事情。謝謝。 – Lemonseed

相關問題