2012-04-03 15 views
2

我正在使用winforms,並且我試圖在設計器中顯示form \ component \ usercontrol的任何時間在某些事件中鏈接,我是試圖鏈接一些從app.config文件加載首選項的靜態事件。Winforms - 當任何表單被加載到設計器中時執行一個函數

是否可以在我的項目中定義代碼,說「任何時候控件加載,執行此事件?」

編輯這是嚴格的設計時間的事情。

我有一個基本形式,在DLL「A」。這有很多屬性,「ColorLocation,SizeLocation」和這種性質的東西。

在DLL「B」中我有一個派生形式。當B被加載到設計器中時,我有iEditorComponents(不記得確切的名字),它允許用戶從當前項目的app.config設置文件中指定的大量項目中選擇ColorLocation。

問題是,編輯組件位於Dll「A」,它是基礎,它無法訪問「B」中的app.config。

我需要一些辦法來告訴編輯組件「嘿,使用這個字符串列表填充您的編輯控件」。設計師正在盡其所能,似乎不想在派生類中執行任何代碼。

+2

這聽起來不太好。設計時間遠離運行時間。首先,沒有好方法來訪問項目的app.config文件。另一方面,有可能有人聽到這個事件應該是零。 – 2012-04-03 16:46:58

+0

你能解釋一下你的用例嗎?你的意思是你想在包含表單加載時在你的組件中執行一些代碼嗎? – 2012-04-03 18:09:47

+0

另外,這是一個設計時間或運行時間的事情? – 2012-04-03 18:14:25

回答

3

是的,這是可能的,但要確保你在做什麼,因爲聽起來很奇怪。

使用下面的代碼在你Form'sControl's構造:

public void Form1() 
    { 
     InitializeComponent(); 

     if (IsInWinFormsDesignMode()) 
     { 
      // your code stuff here 
     } 
    } 

    public static bool IsInWinFormsDesignMode() 
    { 
     bool returnValue = false; 

     #if DEBUG 

     if (System.ComponentModel.LicenseManager.UsageMode == 
      System.ComponentModel.LicenseUsageMode.Designtime) 
     { 
      returnValue = true; 
     } 

     #endif 

     return returnValue; 
    } 

希望它能幫助。

+0

加載事件不會從設計器中被解僱。 – 2012-04-03 17:04:25

+0

@Boo:是的,你是對的,我編輯了我的答案。謝謝! – 2012-04-03 17:07:12

1

我不知道這是否適用於您的特定用例,但在某個時間點,我需要與Component的容器(即表單)進行交互,所以我最終從ErrorProvider中「竊取」了代碼:

//code goes in your Component/Control 

private ContainerControl _containerControl = null; 

//Will contain a reference to the Form hosting this Component 
     public ContainerControl ContainerControl 
     { 
      get { return _containerControl; } 
      set { _containerControl = value; 
       //In here setup what you need from the Form 
       //for example add a handler 
       } 
     } 

     //Allows the VS.NET designer to set the ContainerControl 
     //in the designer code. Stolen from ErrorProvider. 
     public override ISite Site 
     { 
      set 
      { 
       base.Site = value; 
       if (value != null) 
       { 
        IDesignerHost host = value.GetService(typeof(IDesignerHost)) as IDesignerHost; 
        if (host != null) 
        { 
         IComponent rootComponent = host.RootComponent; 
         if (rootComponent is ContainerControl) 
         { 
          this.ContainerControl = (ContainerControl)rootComponent; 
         } 
        } 
       } 
      } 
     } 

我也用字符串轉換一個派生類從設計外部資源進行交互:

public class OpcDotNetTagConverter : StringConverter 
    { 
     #region Make It A ComboBox 
     public override bool GetStandardValuesSupported(ITypeDescriptorContext context) 
     { 
      return true; 
     } 
     public override bool GetStandardValuesExclusive(ITypeDescriptorContext context) 
     { 
      return false; 
     } 
     #endregion 

     #region Display Tags In List 
     public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context) 
     { 
      if ((context == null) || (context.Container == null)) 
      { 
       return null; 
      } 

      Object[] Tags = this.GetTagsFromServer(context); 
      if (Tags != null) 
      { 
       return new StandardValuesCollection(Tags); 
      } 
      return null; 
     } 

     private object[] GetTagsFromServer(ITypeDescriptorContext context) 
     { 
      List<string> availableTags = new List<string>(); 

      if (context.Instance == null) 
      { 
       availableTags.Add("ITypeDescriptorContext.Instance is null"); 
       return availableTags.ToArray(); 
      } 


      Interfaces.IOpcDotNetServerEnabled inst = context.Instance as Interfaces.IOpcDotNetServerEnabled; 
      if (inst == null) 
      { 
       availableTags.Add(context.Instance.ToString()); 
       return availableTags.ToArray(); 
      } 

      if (inst.OpcDotNetServer == null) 
      { 
       availableTags.Add("No server selected"); 
       return availableTags.ToArray(); 
      } 

      availableTags = inst.OpcDotNetServer.GetTagList(string.Empty); 
      availableTags.Sort(Comparer<string>.Default); 
      return availableTags.ToArray(); 
     } 

     #endregion 

     public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) 
     { 
      if(sourceType == typeof(string)) 
       return true; 
      return base.CanConvertFrom(context, sourceType); 
     } 

     public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) 
     { 
      if (value is string) 
       return value.ToString(); 
      return base.ConvertFrom(context, culture, value); 
     } 

    } 

我希望這能有一定的幫助。

乾杯

相關問題