2011-12-19 108 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#]的
相關問題