C# 为什么在向自定义控件添加新属性时,属性不显示?
我有一个库类,我添加了一个新的用户控件并添加了一个代码:C# 为什么在向自定义控件添加新属性时,属性不显示?,c#,.net,winforms,C#,.net,Winforms,我有一个库类,我添加了一个新的用户控件并添加了一个代码: 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; namesp
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窗体项目时,我将控件拖到设计器中,但在解决方案资源管理器中的控件属性下,我看不到数据,也看不到扩展属性,也没有设置文本框边框颜色
我想添加一个属性,当您单击它时,它将为您提供一个子属性,单击它将打开颜色图案,以便您可以更改/设置绘制事件的新颜色
现在在paint事件中,它被设置为红色,但我希望有一个属性,以便用户可以设置任何颜色。存储文本框边框颜色的属性在哪里?我只看到
公共字符串文本
属性。您应该为文本框边框添加新属性:
[PropertyTab("Data")]
[Browsable(true)]
[Category("Extended Properties")]
[Description("Set TextBox border Color")]
public Color BorderColor { get; set; }
您在哪里定义继承属性选项卡的
数据
类?如果您没有自己定义它,那么您希望设计者使用什么数据类
其他属性-类别
和描述
-对我来说很好。自定义属性显示在PropertyGrid
控件的“属性”选项卡中,在其自己的“扩展属性”类别中(当然,您必须按类别而不是字母顺序对属性进行分组),并带有正确的描述文本(在PropertyGrid
中选择属性时显示)
PropertyTab
属性必须指定有效的PropertyTab
类。如果没有可供使用的数据
类,则该属性显然无法显示在数据
类的属性选项卡中。不太了解您尝试了什么。但是,在代码中需要进行一些更改之后,它对我来说非常有用
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.
}
}
洛米德:它正在工作,但我有两个问题:1。当我选择“新颜色”属性时,如何使其在选择后立即更改边框颜色?现在的情况是,只有调整文本框的大小,我才能看到所选颜色的变化。2.属性文本正在工作,但我如何才能使其也能够直接在文本框区域中输入文本?现在,我只能从属性文本在文本框中添加/键入文本。@DanielShpigel请接受答案。对于问题:1。我编辑了这个2的答案。我认为这是不可能的。你知道这样一个例子吗?关于第二个问题,我想你指的是在设计工程师时代。如果你的意思是像原始文本框一样在运行时运行,那么对我来说,它可以工作并且是允许的。