为什么在向自定义控件添加新属性时,属性不显示
本文关键字:属性 显示 自定义控件 添加 新属性 为什么 | 更新日期: 2023-09-27 18:30:17
我有一个库类,我添加了一个新的用户控件并添加了代码:
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 窗体项目时,我将控件拖到设计器中,但在控件属性下的解决方案资源管理器中,我看不到"数据",看不到"扩展属性",也看不到"设置文本框边框颜色"。
我想添加一个属性,当您单击它时,它会为您提供一个子属性,然后单击该属性将打开颜色模式,以便您可以更改/设置油漆事件的新颜色。
现在在油漆事件中,它设置为 Red,但我希望有一个属性,以便用户可以设置任何颜色。
不太了解你尝试过的内容。但是在您的代码中需要进行一些更改之后,对我来说效果很好。
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.
}
}
存储文本框边框颜色的属性在哪里?我只看到public string Texts
财产。您应该为文本框边框添加新属性:
[PropertyTab("Data")]
[Browsable(true)]
[Category("Extended Properties")]
[Description("Set TextBox border Color")]
public Color BorderColor { get; set; }
你在哪里定义继承PropertyTab
的Data
类?如果您不是自己定义它,您希望设计器使用哪个Data
类?
其他属性 - Category
和Description
- 对我来说效果很好。自定义属性显示在 PropertyGrid
控件的"属性"选项卡中,在其自己的"扩展属性"类别中(当然,您必须按类别而不是按字母顺序对属性进行分组),并具有正确的说明文本(在PropertyGrid
中选择属性时显示)。
PropertyTab
属性必须指定有效的PropertyTab
类。如果没有可供其使用的 Data
类,则显然无法在Data
类的属性选项卡中显示该属性。