2012-08-06 117 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 
{ 
..... 
相关问题