关键代码:
class="csharpcode"> public static void SetWatermark(this TextEdit textEdit, string watermark) { textEdit.Properties.NullValuePromptShowForEmptyValue = true; textEdit.Properties.NullValuePrompt = watermark; } public static void ClearWatermark(this TextEdit textEdit) { if (textEdit.Properties.NullValuePromptShowForEmptyValue) textEdit.Properties.NullValuePrompt = string.Empty; }
测试代码:
private void Form1_Load(object sender, EventArgs e) { textEdit1.SetWatermark("请输入用户名称...."); textEdit2.SetWatermark("请输入用户密码...."); } private void simpleButton1_Click(object sender, EventArgs e) { textEdit1.ClearWatermark(); textEdit2.ClearWatermark(); }
代码效果: