本文實例展示了DevExpress實現為TextEdit設置水印文字的方法,是一個很實用的技巧。分享給大家供大家參考。
轉自 http://blog.csdn.net/yh0503/article/details/51487726
關鍵代碼如下:
1 public static void SetWatermark(this TextEdit textEdit, string watermark) 2 { 3 textEdit.Properties.NullValuePromptShowForEmptyValue =true; 4 textEdit.Properties.NullValuePrompt = watermark; 5 } 6 public static void ClearWatermark(this TextEdit textEdit) 7 { 8 if(textEdit.Properties.NullValuePromptShowForEmptyValue) 9 textEdit.Properties.NullValuePrompt =string.Empty; 10 }
測試代碼如下:
1 privatevoid Form1_Load(objectsender, EventArgs e) 2 { 3 textEdit1.SetWatermark("請輸入用戶名稱...."); 4 textEdit2.SetWatermark("請輸入用戶密碼...."); 5 } 6 7 privatevoid simpleButton1_Click(objectsender, EventArgs e) 8 { 9 textEdit1.ClearWatermark(); 10 textEdit2.ClearWatermark(); 11 }
代碼效果如下圖所示: