在Visual Studio中是否可能创建一个像这样的文本框:
![](https://istack.dev59.com/J45jp.webp)
实际上,更好的解决方案是简单地使用文本框的Paint事件来绘制字符串。
以下是代码:
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));
}
}
}
现在,你需要做的就是将“CueText”属性设置为你想要的初始值,然后就完成了!
Dispose
方法来释放由FromHwnd
方法创建的Graphics和相关资源。因此,我建议使用以下代码:using (Graphics g = Graphics.FromHwnd(m.HWnd)) { this.OnPaint(new PaintEventArgs(g, this.ClientRectangle)); }
- Ricardo González您只需要处理三个文本框事件,在设计器中将文本框的文本设置为“用户名”,并使其字体倾斜,然后将文本框背景颜色设置为浅黄色,其余由事件处理程序处理...
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";
}