先看下效果图,初次进来是界面左边的效果,点击请输入账号,清除内容,可以直接输入,右边图
以下代码是失去焦点的,一定要把控件的属性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
水印类
把水印类添加在项目中。直接调用方法即可ComLogin_Load
/// <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, "请输入密码"); }