简体   繁体   English

为什么在向自定义控件添加新属性时不显示属性?

[英]Why when adding new properties to custom control the properties not show?

I have a library class and i added a new User Control and added a code: 我有一个库类,并添加了新的用户控件并添加了代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CustomControl
{
    public partial class ExtendedTextBox : UserControl
    {
        [PropertyTab("Data")]
        [Browsable(true)]
        [Category("Extended Properties")]
        [Description("Set TextBox border Color")]

        public string Texts
        {
            get { return textBox.Text; }
            set { textBox.Text = value; }
        }

        private TextBox textBox;

        public ExtendedTextBox()
        {
            InitializeComponent();

            textBox = new TextBox();
            textBox.Multiline = true;
            textBox.BorderStyle = BorderStyle.None;
            this.Controls.Add(textBox);
        }

        private void ExtendedTextBox_Load(object sender, EventArgs e)
        {

        }

        private void ExtendedTextBox_Paint(object sender, PaintEventArgs e)
        {
            ControlPaint.DrawBorder(e.Graphics, this.ClientRectangle, Color.Red, ButtonBorderStyle.Solid);
        }

        private void ExtendedTextBox_Resize(object sender, EventArgs e)
        {
            textBox.Size = new Size(this.Width - 3, this.Height - 2);
            textBox.Location = new Point(2, 1);
        }
    }
}

When i add the dll file to another windows forms project i drag the control to the designer but in the solution explorer under the control properties i don't see Data and not Extended Properties and not Set TextBox border Color. 当我将dll文件添加到另一个Windows窗体项目中时,我将控件拖到设计器中,但是在解决方案资源管理器中,控件属性下我看不到“数据”,“扩展属性”和“未设置文本框边框颜色”。

I wanted to add a property that when you click on it will give you a sub property and click on that will open the colors pattern so you can change/set a new color to the paint event. 我想添加一个属性,当您单击它时,将为您提供一个子属性,然后单击该属性将打开颜色模式,以便您可以为paint事件更改/设置新颜色。

Now in the paint event it's set to Red but i want that there will be a property so the user can set any color. 现在在绘画事件中将其设置为红色,但我希望有一个属性,以便用户可以设置任何颜色。

Where is property that stores textBox border color? 存储textBox边框颜色的属性在哪里? I see only public string Texts property. 我只看到public string Texts属性。 You should add new property for textBox border: 您应该为textBox边框添加新属性:

[PropertyTab("Data")]
[Browsable(true)]
[Category("Extended Properties")]
[Description("Set TextBox border Color")]
public Color BorderColor { get; set; }

Where are you defining the Data class that inherits PropertyTab ? 您在哪里定义继承PropertyTabData类? If you are not defining it yourself, what Data class did you expect for the designer to use? 如果您不是自己定义的,那么您希望设计人员使用哪种Data类?

The other attributes — Category and Description — work fine for me. 其他属性( CategoryDescription对我来说很好用。 The custom property is shown in the "Properties" tab of the PropertyGrid control, in its own "Extended Properties" category (naturally, you have to be grouping properties by category, not alphabetically), with the correct description text (shown when the property is selected in the PropertyGrid ). 自定义属性显示在PropertyGrid控件的“属性”选项卡中,位于其自己的“扩展属性”类别中(自然,您必须按类别(而不是按字母顺序)对属性进行分组),并带有正确的描述文本(在属性中显示在PropertyGrid被选中)。

The PropertyTab attribute has to specify a valid PropertyTab class. PropertyTab属性必须指定一个有效的PropertyTab类。 If you don't have a Data class for it to use, then obviously the property can't be displayed in the Data class's property tab. 如果没有要使用的Data类,则显然该属性无法显示在Data类的属性选项卡中。

Not so understand what you tried. 不太了解您尝试了什么。 But works great for me, after some changes are needed in your code. 但是,在您的代码中需要进行一些更改之后,对我而言非常有用。

public partial class ExtendedTextBox : UserControl
{
    [PropertyTab("Data")]
    [Browsable(true)]
    [Category("Extended Properties")]
    [Description("Set TextBox border Color")]
    public Color BorderColor { get; set; }

    [PropertyTab("Data")]
    [Browsable(true)]
    [Category("Extended Properties")]
    [Description("Set TextBox Text")]
    public string Texts
    {
        get { return textBox.Text; }
        set { textBox.Text = value; }
    }

    private TextBox textBox;

    public ExtendedTextBox()
    {
        textBox = new TextBox();
        textBox.Multiline = true;
        textBox.BorderStyle = BorderStyle.None;
        this.Controls.Add(textBox);
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);
        ControlPaint.DrawBorder(e.Graphics, this.ClientRectangle, BorderColor, ButtonBorderStyle.Solid);
    }

    protected override void OnResize(EventArgs e)
    {
        base.OnResize(e);
        textBox.Size = new Size(this.Width - 3, this.Height - 2);
        textBox.Location = new Point(2, 1);
    }
}

EDIT: For apply changes immediately you need refresh the control BorderColor property on catch the moment of set, which is not possible automatic property, but only full property. 编辑:要立即应用更改,您需要在设置时立即刷新控件BorderColor属性,这不可能是自动属性,而只能是完整属性。 so: 所以:

//private field needy in full property.
private Color _BorderColor = Color.Red;  //= Color.Red; for default color...

[PropertyTab("Data")]
[Browsable(true)]
[Category("Extended Properties")]
[Description("Set TextBox border Color")]
public Color BorderColor
{
    get {return _BorderColor ;}
    set
    {
        _BorderColor = value;
        Invalidate(); //refresh, trigger new paint.
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM