是否可以在 WinForms .Net 的组件设计器中更改组件名称

发布于 08-17 04:48 字数 117 浏览 10 评论 0原文

我创建了一个组件,我希望在组件托盘中编辑时能够更改其名称。 我已经为名称属性添加了设计器操作,但现在我陷入困境。

查看属性网格,我可以看到名称属性带有括号,表明它不是常规属性。

这可能吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

千笙结2024-08-24 04:48:42

您可以在设计时使用 Component.Site.Name 更改Component 的名称。您应该将代码放在 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;
        }
    }
}

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;
        }
    }
}
千仐2024-08-24 04:48:42

有些属性在设计环境中是特殊的,您只能通过类型描述符真正设置它们。名称可能是这种情况,但“可见”、“锁定”和“启用”等内容确实是这种情况。也许这会给你一些目前值得关注的东西。

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

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);
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文