2012-08-06 51 views
2

我創建了一個用戶控件。它基本上是一個具有一些自定義屬性的按鈕。更改自定義控件在設計時的默認文本屬性

public partial class CustomButton : Button { 
    // My custom properties, constructor and events 
} 

每次我添加此CustomButton一個形式,它的默認Text值設置爲 「customButtonX」,其中X是1,2,3,...

我怎樣才能改變這種值?我希望它是「buttonX」(X = 1,2,3 ...)。

編輯:當我通過設計視圖在表單上添加一個按鈕時,我還想使用這個技巧(或者其他任何我必須做的)。當我從工具箱中將CustomButton拖放到表單時,其含義應該是「buttonX」。

回答

2

將控件從工具箱拖動到窗體時,會觸發一些事件。在你的情況下,當你的控件的text屬性從String.Empty改變爲默認名稱並更改它時,你必須訂閱被觸發的那個。爲此,必須在控件添加到表單之前獲得公開這些事件的服務(IComponentChangeService的實現)。這可以覆蓋控件的Site屬性。修改,你可以找到here的例子,這種代碼應該工作:

private IComponentChangeService _changeService; 

    public override System.ComponentModel.ISite Site 
    { 
     get 
     { 
      return base.Site; 
     } 
     set 
     { 
      _changeService = (IComponentChangeService)GetService(typeof(IComponentChangeService)); 
      if (_changeService != null) 
       _changeService.ComponentChanged -= new ComponentChangedEventHandler(OnComponentChanged); 
      base.Site = value; 
      if (!DesignMode) 
       return; 
      _changeService = (IComponentChangeService)GetService(typeof(IComponentChangeService)); 
      if (_changeService != null) 
       _changeService.ComponentChanged += new ComponentChangedEventHandler(OnComponentChanged); 
     } 
    } 

    private void OnComponentChanged(object sender, ComponentChangedEventArgs ce) 
    { 
     CustomButton aBtn = ce.Component as CustomButton; 
     if (aBtn == null || !aBtn.DesignMode) 
      return; 
     if (((IComponent)ce.Component).Site == null || ce.Member == null || ce.Member.Name != "Text") 
      return; 
     if (aBtn.Text == aBtn.Name) 
      aBtn.Text = aBtn.Name.Replace("customButton", "button"); 
    } 
+0

這似乎是正確的路要走。儘管如此,我無法使用OnComponentAdded事件。當我有'aBtn.Text = aBtn.Name.Replace(「customButton」,「button」);'事件中的代碼時,什麼都不會發生。 – Otiel 2012-08-06 12:18:03

+0

我使用了ComponentChanged,因爲組件是第一次添加的,它的Text屬性只有在執行OnComponentAdded之後纔會改變。你可以看到發生了什麼,在附加到不同事件的方法中添加一些MessageBoxes。 – 2012-08-06 12:37:25

+0

你知道是否可以改變'Name'屬性嗎? – Otiel 2012-08-06 12:57:42

4

默認值是「yourControlNameX」,這是正確的。但是你可以在構造函數中替換名稱。

注意,這隻會在運行時的工作(在設計時)

public partial class CustomButton : Button { 
    // My custom properties, constructor and events 

    public CustomButton() 
    { 
     this.Text = this.Text.Replace("customButton ", "button"); 
    } 
} 
+0

這並不工作裝點你的控制,'Text'仍然是* customButtonX *。至少當通過Design視圖在窗體上添加一個CustomButton時。 – Otiel 2012-08-06 07:27:08

+2

它不起作用,因爲在設計模式下,當您將控件添加到窗體時,它首先被創建(並且構造函數被執行),然後文本從空字符串更改爲默認名稱。 – 2012-08-06 10:22:02

+0

順便說一句,即使在運行時它也沒用,因爲你在設計時設置的值(如果你不改變它,這是默認值)寫在InitializeComponent中。這意味着即使在運行時,您在構造函數中設置的文本也會被覆蓋。 – 2012-08-08 07:19:20

1

只是覆蓋文本,並設置任何你想要進去。

public partial class CustomButton : Button { 

    public override string Text 
    { 
     get 
     { 
      //return custom text 
      return base.Text; 
     } 
     set 
     { 
      //modify value to suit your needs 
      base.Text = value; 
     } 
    } 
} 
+0

但是我怎樣才能使用它來保持* buttonX *,X = 1,2,3 ... scheme? – Otiel 2012-08-06 07:32:37

+0

如果您在該集合中放置了一個調試,您將會看到'customButtonX'正在通過'value'設置。你可以從這裏的值中刪除它。像'base.Text = value.Replace(「customButton」,「button」)'; – nunespascal 2012-08-06 07:46:25

+2

我看,它的工作原理。但是這並不能真正改變默認值。它每次都會更改值*。現在,如果開發者希望將CustomButton文本值設置爲* customButtonToDoSmthg *,那麼將該按鈕重命名爲* buttonToDoSmthg *。我知道,那很愚蠢,但是...... – Otiel 2012-08-06 07:54:05

0

可能是你可以使用一個ControlDesigner和Text屬性設置inizialized後。

public class MultiDesigner : ControlDesigner 
{ 
    public MultiDesigner() 
    { 

    } 

    public override void InitializeNewComponent(IDictionary defaultValues) 
    { 
     base.InitializeNewComponent(defaultValues); 

     ICommonControl control = this.Component as ICommonControl; 
     control.Text = control.Tag.ToString(); 
    } 
} 

與..

[Designer("MultiPuntoDeVenta.Controls.Tickets.Editors.Designer.MultiDesigner, MultiPuntoDeVenta.Controls")] 
public class LabelBase<T> : KryptonLabel, ICommonControl where T : ICommonControl 
{ 
..... 
相關問題