繁体   English   中英

在设计时更改自定义控件默认Text属性

[英]Change custom control default Text property at design time

我创建了一个用户控件。 它基本上是一个带有一些自定义属性的按钮。

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

每次我在表单上添加此CustomButton时,其默认Text值设置为“ customButtonX ”,其中X为1,2,3,...

我该如何更改此值? 我希望它是“ buttonX ”(X = 1,2,3 ......)。

编辑:当我通过设计视图在表单上添加按钮时,我想要激活(或者我必须做的任何事情)。 这意味着当我将CustomButton从我的工具箱拖放到表单时,其Text值应为“ buttonX ”。

默认为“yourControlNameX”,这是正确的。 但是您可以在构造函数中替换名称。

请注意,这仅适用于运行时(不是在设计时)

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

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

将控件从“工具箱”拖动到表单时,会触发一些事件。 在您的情况下,您必须订阅当控件的text属性从String.Empty更改为默认名称并更改它时触发的那个。 为此,您必须在将控件添加到表单之前获取公开这些事件的服务(IComponentChangeService的实现)。 这可以覆盖控件的Site属性。 修改你可以在这里找到的例子,这种代码应该工作:

    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");
    }

只需覆盖文本并设置您想要的任何内容。

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;
        }
    }
 }

也许你可以使用ControlDesigner并在inizialized之后设置Text属性。

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
{
.....

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM