2011-12-19 98 views
2

我在窗体中有一个文本框。在表单加载事件上,我想我的textbox.text =“输入名称”,但它应该显示为如果单击文本框,文本框的文本会消失。 附加的图像将帮助你理解我的问题:)c#窗口中的文本框形式

First Image is what i actually want to do.

+0

可能重复(http://stackoverflow.com/questions/578193/watermark-system-windows-forms-textbox-using-c-sharp) – Caleb 2011-12-20 04:38:54

回答

4
+1

要么你在谷歌搜索找到相同的网站,或者你正在复制一个[先前的答案](http://stackoverflow.com/questions/578193/watermark-system-windows-forms-textbox-using- C-尖锐)。如果是后者,请确保在答案中引用作者的帖子。 – 2011-12-19 20:00:40

+0

@BradChristie好点。我用这个帖子标记了这个q,不妨把它张贴在这里。 – 2011-12-19 20:02:56

+0

此外,我在这里是另一个[使用WndProc](http://stackoverflow.com/questions/2539903/sendmessage-vs-wndproc)(如果作者不介意)。 – 2011-12-19 20:04:26

2

使用TextBox.ForeColor并将其更改为灰色和进入/离开文本框的事件来改变颜色和删除文字

1

这里是一个解决方案,没有DllImport用法

/// <summary> 
/// inspired by this forum entry: http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/10f75954-6d14-4926-a02d-98649653b9c8/ 
/// Watermark TextBox in winform 
/// </summary> 
public class WatermarkTextBox : TextBox 
{ 
    private string watermarkText; 
    private Color watermarkColor; 
    private Color foreColor; 
    private bool isTextBoxEmpty; 

    public WatermarkTextBox() 
    { 
    this.WatermarkText = "Watermark Text..."; 
    this.WatermarkColor = Color.FromKnownColor(KnownColor.Silver); 
    this.Enter += onEnter; 
    this.Leave += onLeave; 
    } 

    [Browsable(true)] 
    public new Color ForeColor 
    { 
    get { return this.foreColor; } 
    set 
    { 
     if (value == this.foreColor) 
     { 
     return; 
     } 
     this.foreColor = value; 
     if (!this.isTextBoxEmpty) 
     { 
     base.ForeColor = value; 
     } 
    } 
    } 

    [Browsable(true)] 
    public Color WatermarkColor 
    { 
    get { return this.watermarkColor; } 
    set 
    { 
     if (value == this.watermarkColor) 
     { 
     return; 
     } 
     this.watermarkColor = value; 
     if (this.isTextBoxEmpty) 
     { 
     base.ForeColor = this.watermarkColor; 
     } 
    } 
    } 

    [Browsable(true)] 
    public string WatermarkText 
    { 
    get { return this.watermarkText; } 
    set 
    { 
     if (value == this.watermarkText) 
     { 
     return; 
     } 
     this.watermarkText = value; 
     if (base.Text.Length == 0) 
     { 
     this.isTextBoxEmpty = true; 
     base.Text = this.watermarkText; 
     base.ForeColor = this.watermarkColor; 
     } 
     this.Invalidate(); 
    } 
    } 

    public override string Text 
    { 
    get { return this.isTextBoxEmpty ? string.Empty : base.Text; } 
    set 
    { 
     if (string.IsNullOrEmpty(value)) 
     { 
     this.isTextBoxEmpty = true; 
     base.ForeColor = this.watermarkColor; 
     base.Text = this.watermarkText; 
     } 
     else 
     { 
     this.isTextBoxEmpty = false; 
     base.ForeColor = this.foreColor; 
     base.Text = value; 
     } 
    } 
    } 

    private void onEnter(object sender, EventArgs e) 
    { 
    if (this.isTextBoxEmpty) 
    { 
     this.isTextBoxEmpty = false; 
     base.ForeColor = this.foreColor; 
     base.Text = string.Empty; 
    } 
    } 

    private void onLeave(object sender, EventArgs e) 
    { 
    if (string.IsNullOrEmpty(base.Text)) 
    { 
     this.isTextBoxEmpty = true; 
     base.ForeColor = this.watermarkColor; 
     base.Text = this.watermarkText; 
    } 
    else 
    { 
     this.isTextBoxEmpty = false; 
    } 
    } 
} 
[水印System.Windows.Forms.TextBox使用C#]的