Change custom control default Text property at des

2019-07-16 00:52发布

问题:

I created a user control. It's basically a button with some custom properties.

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

Everytime I add this CustomButton on a form, its default Text value is set to "customButtonX", where X is 1, 2, 3, ...

How can I change this value? I would like it to be "buttonX" (X = 1, 2, 3...).

EDIT : I would like the trick (or whatever it is I have to do) to be active when I add a button on a form via the design view also. Meaning when I drag-drop a CustomButton from my toolbox to a form, its Text value should be "buttonX".

回答1:

When you drag a control from the Toolbox to a form there are some events that are triggered. In your case you have to subscribe to the one that is fired when the text property of your control is changed from String.Empty to the default name and change it. To do this you have to get the service that exposes these events (an implementation of IComponentChangeService) before the control is added to the form. This can be done overriding the Site property of your control. Modifying the example that you can find here, this kind of code should work:

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


回答2:

The default is "yourControlNameX" thats right. But you can replace the name in the constructor.

Note that this only will work at Runtime (not at design time)

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

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


回答3:

Just override the text and set whatever you want into it.

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


回答4:

May be you could use a ControlDesigner and set the Text property after 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();
    }
}

decorate your control with..

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