在设计时为表单自定义可浏览属性 [英] Custom browsable property for Form at design time

查看:95
本文介绍了在设计时为表单自定义可浏览属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在设计时使Windows窗体的自定义属性可浏览,但是我的努力都没有成功.显而易见的解决方案似乎是将browsable属性设置为true:

I want to make a custom property for a windows form browsable during design-time but none of my efforts have panned out to success. The obvious solution would seem to be to set the browsable attribute to true:

[Browsable(true),
EditorBrowsable(EditorBrowsableState.Always),
Description("Custom Border Colour"),
Category("Custom")]
public Color BorderColour
{
    get
    {
        return bCol;
    }
    set
    {
        bCol = value;
    }
}

但这不起作用.我花了很多时间来进行自定义控件,并且它的工作原理很像魅力,实际上,我不需要添加属性,因为默认值是true.这篇 codeproject 文章似乎可以帮助我想要,这就是我上面描述的. MSDN也是死胡同,或者我不知道要搜索什么.

But this doesn't work. I have done it numerous time for custom controls and it works like a charm, in fact, I don't even need to add the attributes because the default is true. This codeproject article seems to do what I want, which is what I described above. MSDN is also a dead end, or I don't know what to search for.

我试图将代码添加到Form1.csFrom1.Designer.cs,但没有任何效果.

I have tried to add the code to Form1.cs and From1.Designer.cs but nothing works.

我是否缺少某些东西,例如我需要为表单设置的属性以允许这样做,或者这是不可能的吗?

Is there something I am missing, like some property I need to set for the form to allow for this, or is it just impossible?

如果正在以任何方式影响结果,我正在使用Visual Studio Express 2013.

I am using Visual Studio Express 2013, if this would influence the outcomes in any way.

在Reza回答后尝试:

Attempts after Reza's answer: A more detailed question on this topic is asked in this question as per Reza's suggestion.

推荐答案

简短答案

您应该将属性添加到表单的基类,然后在打开子表单时可以在设计器中看到它:

You should add the property to base class of your form, then you can see it in designer when you open the child form:

public class Form1 : BaseForm
{
    public Form1()
    {
        InitializeComponent();
    }
}

public class BaseForm : Form
{
    //The property is not visible in designer of BaseForm
    //But you can see it in designer of Form1

    public string SomeProperty {get;set;}
}

此行为的原因

原因在于设计师的工作方式.当设计人员在设计时显示表单时,实际上会创建该表单的基类的实例并显示其属性.因此,在设计器中使用public class Form1:Form时,在设计器中看到的实际上是Form类的实例,以及使用Form1InitializeComponent方法设置了属性值的控件的实例,以及添加了控件的实例.使用Form1InitializeComponent方法.

The reason is in the way that designer works. When the designer shows a form at design time, in fact it creates an instance of the base class of the form and shows its properties. So having public class Form1:Form in designer, what you see in designer is actually an instance of Form class and the instances of controls which values of properties has been set using using InitializeComponent method of Form1 and also controls which are added using InitializeComponent method of Form1.

对于用户控件,您也无法在用户控件的设计器中看到自定义属性,因为在用户控件的设计器中可以看到的属性是其基类的属性.但是,当您将用户控件的实例放在表单上时,您将看到该实例的属性,即UserControl1的属性.

Also for user controls, you can not see your custom properties in the designer of your user control, because the properties that you can see in designer of your user control, is properties of it's base class. But when you put an instance of your user control on a form, you will see properties of that instance which is properties of your UserControl1.

设计器的根元素的属性是根元素的基类的属性.但是这些值正是在InitializeComponent中设置的值.

Properties of the root element of your designer are properties of base class of the root element. But the values are exactly those which are set in InitializeComponent.

要查找更多信息并查看有关设计师如何工作的有趣示例,请查看此帖子这一个.

To find more information and see an interesting example of how the designer works, you can take a look at this post or this one.

这篇关于在设计时为表单自定义可浏览属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆