C# 自定义文本框控件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9247756/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Custom textbox control
提问by gjjansen97
Is it possible to create a textbox in Visual Studio like this:
是否可以像这样在 Visual Studio 中创建一个文本框:


采纳答案by Owais Qureshi
You would just need to handle three TextBox events, in the Designer set the text of the TextBox to "username" and make it Font Italics then set TextBox BackColor to LightYellow,the rest is handled by Event handlers...
您只需要处理三个 TextBox 事件,在设计器中将 TextBox 的文本设置为“用户名”并使其字体为斜体,然后将 TextBox BackColor 设置为 LightYellow,其余由事件处理程序处理...
private void textBox1_TextChanged(object sender, EventArgs e)
{
if (textBox1.Text == "")
ChangeTextBoxtoWatermark();
}
private void textBox1_MouseEnter(object sender, EventArgs e)
{
if (textBox1.Text == "username")
{
textBox1.Text = "";
textBox1.Font = new Font(this.Font, FontStyle.Regular);
textBox1.BackColor = Color.White;
}
}
private void textBox1_MouseLeave(object sender, EventArgs e)
{
if (textBox1.Text == "")
ChangeTextBoxtoWatermark();
}
private void ChangeTextBoxtoWatermark()
{
textBox1.Font = new Font(this.Font, FontStyle.Italic);
textBox1.BackColor = Color.LightYellow;
textBox1.Text = "username";
}
I have checked it and it works fine :)
我已经检查过了,它工作正常:)
回答by Nissim
Actually. A better solution will be simply use the Paint event of the text box to draw the string.
实际上。更好的解决方案是简单地使用文本框的 Paint 事件来绘制字符串。
Here's the code:
这是代码:
class CueTextBox : TextBox
{
public event EventHandler CueTextChanged;
private string _cueText;
public string CueText
{
get { return _cueText; }
set
{
value = value ?? string.Empty;
if (value != _cueText)
{
_cueText = value;
OnCueTextChanged(EventArgs.Empty);
}
}
}
public CueTextBox()
: base()
{
_cueText = string.Empty;
}
protected virtual void OnCueTextChanged(EventArgs e)
{
this.Invalidate(true);
if (this.CueTextChanged != null)
this.CueTextChanged(this, e);
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
if (string.IsNullOrEmpty(this.Text.Trim()) && !string.IsNullOrEmpty(this.CueText) && !this.Focused)
{
Point startingPoint = new Point(0, 0);
StringFormat format = new StringFormat();
Font font = new Font(this.Font.FontFamily.Name, this.Font.Size, FontStyle.Italic);
if (this.RightToLeft == RightToLeft.Yes)
{
format.LineAlignment = StringAlignment.Far;
format.FormatFlags = StringFormatFlags.DirectionRightToLeft;
}
e.Graphics.DrawString(CueText, font, Brushes.Gray, this.ClientRectangle, format);
}
}
const int WM_PAINT = 0x000F;
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
if (m.Msg == WM_PAINT)
{
this.OnPaint(new PaintEventArgs(Graphics.FromHwnd(m.HWnd), this.ClientRectangle));
}
}
}
Now, all you need is the set the 'CueText' property to the initial value you want and you're done!
现在,您只需要将 'CueText' 属性设置为您想要的初始值,就大功告成了!

