【发布时间】:2018-12-02 17:00:08
【问题描述】:
我创建了具有一些属性的自定义控件 PlaceHolderTextBox。
这是我的代码:
class PlaceHolderTextBox:TextBox
{
public string PlaceHolderText { get; set; }
public Color PlaceHolderColor { get; set; }
public Font PlaceHolderFont { get; set; }
public Color StandardColor { get; set; }
public PlaceHolderTextBox()
{
GotFocus += OnGetFocus;
LostFocus += OnLostFocus;
TextChanged += OnTextChanged;
Text = PlaceHolderText;
ForeColor = PlaceHolderColor;
Font = PlaceHolderFont;
}
private void OnGetFocus(object sender,EventArgs e)
{
if (this.Text == this.PlaceHolderText)
{
ForeColor = StandardColor;
Text = "";
}
}
private void OnLostFocus(object sender, EventArgs e)
{
if (this.Text == "")
{
ForeColor = PlaceHolderColor;
Text = PlaceHolderText;
}
}
}
当我启动程序时,我得到空文本框。
我认为这种行为的原因是构造函数按时执行,属性为空,但我不确定。
另外,我想在更改这些自定义属性时进行事件。
这可能吗?
【问题讨论】:
-
可以在构造函数中设置你在设计器中设置的属性吗?
-
@jaredbaszler 我不能这样做,因为我需要将此控件用于不同的用途
标签: winforms events properties custom-controls