Windows用户自定义表单属性可浏览

Windows用户自定义表单属性可浏览

问题描述:

我想使一个窗口自定义属性形成在设计时可浏览,但没有我的努力都摇出来的成功。显而易见的解决方案似乎是设置可浏览属性设置为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;
    }
}

但是,这并不正常工作。我已经做了很多的时间自定义控件和它的工作原理就像一个魅力,事实上,我甚至都不需要添加的属性,因为默认是真实的。这 $ C $的CProject 文章似乎做我想做的,这是上述我。 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.

我曾尝试到code添加到 Form1.cs的 From1.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防爆preSS 2013年,这是否会以任何方式影响的结果。

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

编辑:雷扎的回答后尝试:有关这个主题的更详细的问题是问在this问题按照礼的建议。

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

这种行为背后的原因

原因是在方式,设计师作品。当设计者表示在设计时的形式,事实上,它创建的基类的形式的一个实例,并示出了它的性质。因此,有公共Form1类:表格的设计师,你的设计师看到什么是真正的表的实例类和其中values​​属性已经使用 Form1中的InitializeComponent 方法设置使用的,也该是使用添加的控件的控件实例的InitializeComponent Form1中的方法。

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.