【问题标题】:Why when adding new properties to custom control the properties not show?为什么向自定义控件添加新属性时属性不显示?
【发布时间】:2014-11-12 18:12:13
【问题描述】:

我有一个库类,我添加了一个新的用户控件并添加了一个代码:

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

当我将 dll 文件添加到另一个 windows 窗体项目时,我将控件拖到设计器中,但在解决方案资源管理器中的控件属性下,我看不到数据,也看不到扩展属性,也没有设置文本框边框颜色。

我想添加一个属性,当您单击它时会为您提供一个子属性,单击该属性将打开颜色模式,以便您可以更改/设置绘画事件的新颜色。

现在在绘制事件中它设置为红色,但我希望有一个属性,以便用户可以设置任何颜色。

【问题讨论】:

    标签: c# .net winforms


    【解决方案1】:

    不太明白你尝试了什么。但在您的代码需要进行一些更改之后,对我来说效果很好。

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

    编辑:立即应用更改 您需要在设置的时刻刷新控件BorderColor 属性,这不是自动属性,而是完整属性。所以:

    //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.
        }
    }
    

    【讨论】:

    • lomed 它正在工作,但我有两个问题: 1. 当我选择新的颜色属性时,我怎样才能做到这一点,以便在选择后立即更改边框颜色?现在的方式只有当我调整文本框的大小时,我才能看到所选颜色的变化。 2. 属性 Texts 正在工作,但我怎样才能做到这一点,我也可以直接在 textBox 区域中输入文本?现在我只能从属性 Texts 在文本框中添加/键入文本。
    • @DanielShpigel 请接受答案。对于问题: 1. 我编辑了这个 2. 我不认为这是可能的答案。你知道这样的例子吗?
    • 关于第二个问题,我以为您的意思是在 Desgin 时间的 Desginer 中。如果你的意思是像原始文本框一样在运行时,对我来说它可以工作并且是允许的。
    【解决方案2】:

    存储文本框边框颜色的属性在哪里?我只看到public string Texts 属性。您应该为 textBox 边框添加新属性:

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

    【讨论】:

      【解决方案3】:

      您在哪里定义继承PropertyTabData 类?如果您不自己定义它,您希望设计师使用什么Data 类?

      其他属性——CategoryDescription——对我来说很好用。自定义属性显示在 PropertyGrid 控件的“属性”选项卡中,在其自己的“扩展属性”类别中(当然,您必须按类别对属性进行分组,而不是按字母顺序),并带有正确的描述文本(在该属性在PropertyGrid中被选中)。

      PropertyTab 属性必须指定一个有效的PropertyTab 类。如果您没有Data 类可供它使用,那么显然该属性无法显示在Data 类的属性选项卡中。

      【讨论】:

        猜你喜欢
        • 2011-03-12
        • 2012-04-13
        • 1970-01-01
        • 2018-05-29
        • 2011-04-16
        • 2012-04-28
        • 2013-07-25
        • 1970-01-01
        • 2021-05-03
        相关资源
        最近更新 更多