先看下效果圖,初次進來是界面左邊的效果,點擊請輸入帳號,清除內容,能夠直接輸入,右邊圖ide
如下代碼是失去焦點的,必定要把控件的屬性TabStop 改成false。否則是不行的。工具
#region 失去焦點
protected override bool ProcessTabKey(bool forward)
{
foreach (Control c in this.Controls) { if (c is TextBox) { c.TabStop = true; } } return base.ProcessTabKey(forward); } #endregion
水印類this
把水印類添加在項目中。直接調用方法便可ComLogin_Loadspa
/// <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設置水印文字 /// </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); } }
private void ComLogin_Load(object sender, EventArgs e) { this.WindowState = FormWindowState.Maximized; TextBoxToolV2.SetWatermark(txtUserID, "請輸入帳號"); TextBoxToolV2.SetWatermark(txtEntryPwd, "請輸入密碼"); }