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

笑着哭i 提交于 2019-12-07 17:33:45

问题


I've created a component whose name I'd like to be able to change while editing in the component tray. I've added a Designer action for a name property, but now I'm stuck.

Looking at the property grid, I can see that the name property is parenthesised, indicating that it's not a regular property.

Is this possible?


回答1:


You can change the name of a Component at design-time using Component.Site.Name. You should put the code in a try/catch block to handle exception when the name is duplicate.

Code:

When you implement a designer for your component, the man code for change the name of component at design time is:

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

Here is a full implementation of a component and a component designer. The component designer has a verb that is accessible when you right click on the component, also it's accessible from property grid in commands tray. When you click on Rename command, it sets the name of component to SomeName. It also shows an error message if there is a component with the same name. In a more realistic sample, you can override ActionLists to let the user enter a new name itself.

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



回答2:


Some of the properties are special in the Design Environment and you can only really set them via the Type Descriptor. This may be the case for the name, but it certainly is the case for things like Visible, Locked and Enabled. Perhaps this will give you something to look at for now.

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


来源:https://stackoverflow.com/questions/2027802/is-it-possible-to-change-a-component-name-in-a-component-designer-in-winforms-n

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!