WinForm實現(xiàn)為TextBox設(shè)置水印文字功能
更新時間:2014年08月19日 17:15:12 投稿:shichen2014
這篇文章主要介紹了WinForm實現(xiàn)為TextBox設(shè)置水印文字功能,很實用的一個技巧,需要的朋友可以參考下
本文實例展示了WinForm實現(xiàn)為TextBox設(shè)置水印文字功能,非常實用的技巧,分享給大家供大家參考。
關(guān)鍵代碼如下:
using System; using System.Runtime.InteropServices; using System.Windows.Forms; namespace WinFormUtilHelpV2 { /// <summary> /// 基于.NET 2.0的TextBox工具類 /// </summary> public static class TextBoxToolV2 { private const int EM_SETCUEBANNER = 0x1501; [DllImport("user32.dll", CharSet = CharSet.Auto)] private static extern Int32 SendMessage (IntPtr hWnd, int msg, int wParam, [MarshalAs(UnmanagedType.LPWStr)] string lParam); /// <summary> /// 為TextBox設(shè)置水印文字 /// </summary> /// <param name="textBox">TextBox</param> /// <param name="watermark">水印文字</param> public static void SetWatermark(this TextBox textBox, string watermark) { SendMessage(textBox.Handle, EM_SETCUEBANNER, 0, watermark); } /// <summary> /// 清除水印文字 /// </summary> /// <param name="textBox">TextBox</param> public static void ClearWatermark(this TextBox textBox) { SendMessage(textBox.Handle, EM_SETCUEBANNER, 0, string.Empty); } } }
測試代碼如下:
using System; using System.Windows.Forms; using WinFormUtilHelpV2; namespace WinFormUtilHelpV2Test { public partial class WinTextBoxToolV2Test : Form { public WinTextBoxToolV2Test() { InitializeComponent(); } private void WinTextBoxToolV2Test_Load(object sender, EventArgs e) { textBox1.SetWatermark("請輸入用戶名稱...."); textBox2.SetWatermark("請輸入用戶密碼...."); } private void button1_Click(object sender, EventArgs e) { textBox1.ClearWatermark(); textBox2.ClearWatermark(); } } }
測試效果如下圖所示:
希望本文所述的為TextBox設(shè)置水印文字功能示例對大家C#程序設(shè)計有所幫助!
相關(guān)文章
Unity3D實現(xiàn)NavMesh導(dǎo)航網(wǎng)格尋路
這篇文章主要為大家詳細(xì)介紹了Unity3D實現(xiàn)NavMesh導(dǎo)航網(wǎng)格尋路,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-05-05c#獲得目標(biāo)服務(wù)器中所有數(shù)據(jù)庫名、表名、列名的實現(xiàn)代碼
這篇文章主要介紹了c#獲得目標(biāo)服務(wù)器中所有數(shù)據(jù)庫名、表名、列名的方法,需要的朋友可以參考下2014-05-05在c#中使用servicestackredis操作redis的實例代碼
本篇文章主要介紹了在c#中使用servicestackredis操作redis的實例代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-06-06