Is it possible to change the component name in the component designer in WinForms.Net

I created a component whose name I would like to change when editing in the component tray. I added a Designer action for the name property, but now I'm stuck.

Looking at the property grid, I see that the name property in brackets indicates that this is not a regular property.

Is it possible?

+3
source share
2 answers

, . , , , , Visible, Locked Enabled. , -, .

SetHiddenValue(control, "Visible", false);
SetHiddenValue(control, "Locked", true);
SetHiddenValue(control, "Enabled", false);

    /// <summary>
    /// Sets the hidden value - these are held in the type descriptor properties.
    /// </summary>
    /// <param name="control">The control.</param>
    /// <param name="name">The name.</param>
    /// <param name="val">The val.</param>
    private static void SetHiddenValue(Control control, string name, object val)
    {
        PropertyDescriptor descriptor = TypeDescriptor.GetProperties(control)[name];
        if (descriptor != null)
        {
            descriptor.SetValue(control, val);
        }
    }
+1

Component Component.Site.Name. try/catch , .

:

, :

this.Component.Site.Name = "SomeName";

​​ . , , . Rename, SomeName. , . ActionLists, .

using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Windows.Forms.Design;

[Designer(typeof(MyComponentDesigner))]
public class MyComponent : Component
{
    public string SomeProperty { get; set; }
}

public class MyComponentDesigner : ComponentDesigner
{
    DesignerVerbCollection verbs;
    public MyComponentDesigner() : base() { }
    public override DesignerVerbCollection Verbs
    {
        get
        {
            if (verbs == null)
            {
                verbs = new DesignerVerbCollection();
                verbs.Add(new DesignerVerb("Rename", (s, e) =>
                {
                    try
                    {
                        this.Component.Site.Name = "SomeName";
                        this.RaiseComponentChanged(null, null, null);
                    }
                    catch (Exception ex)
                    {
                        var svc = ((IUIService)this.GetService(typeof(IUIService)));
                        svc.ShowError(ex);
                    }
                }));
            }
            return verbs;
        }
    }
}
+1

Source: https://habr.com/ru/post/1727772/


All Articles