关键代码:

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设置水印文字
        /// </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();
        }
    }
}

测试效果:

希望有所帮助!

[WinForm]为TextBox设置水印文字的更多相关文章

  1. DevExpress实现为TextEdit设置水印文字

    本文实例展示了DevExpress实现为TextEdit设置水印文字的方法,是一个很实用的技巧.分享给大家供大家参考. 转自 http://blog.csdn.net/yh0503/article/d ...

  2. winform的Textbox设置只读之后ForeColor无效的解决方法

    winform的Textbox设置只读之后ForeColor无效. 通过以下方法就可以解决: 设置为只读之后,把BackColor改一下,然后运行一下窗口,再设置ForeColor就没问题了. tbT ...

  3. Winform给TextBox设置默认值

    Winform给TextBox设置默认值(获取焦点后默认值消失) 主要是通过TextBox的获取焦点Enter和失去焦点Leave两个事件来实现的, 思路如下: 1.设置一个字符串常量,作为TextB ...

  4. winform的Textbox设置只读之后使用ForeColor更改颜色

    winform的Textbox设置只读之后设置ForeColor更改颜色无效.这是 TextBox 默认的行为. 解决方法:设置为只读之后,修改控件的BackColor,再设置ForeColor就可以 ...

  5. DevExpress 为TextEdit设置水印文字

    设置水印代码: //设置水印值public static void SetWatermark(this TextEdit textEdit, string watermark) { textEdit. ...

  6. Winform给TextBox设置默认值(获取焦点后默认值消失)

    主要是通过TextBox的获取焦点Enter和失去焦点Leave两个事件来实现的, 思路如下: 1.设置一个字符串常量,作为TextBox的默认值: 2.在界面的构造方法中将默认值赋值给TextBox ...

  7. DevExpress实现为TextEdit设置水印文字的方法

    设置水印与消除水印 public static void SetWatermark(TextEdit textEdit, string watermark) { textEdit.Properties ...

  8. TextBox 文本框水印文字

    #region TextBox 文本框水印文字 /// <summary> /// 基于.NET 2.0的TextBox工具类 /// </summary> public st ...

  9. WinForm自定义控件–TextBox扩展

      一.简单回顾 在前两节中,对Panel和GroupBox控件进行了相关的扩展应用,主要都是设置控件的边框以及边框颜色等.本节,继续对WinForm现有的控件TextBox进行扩展,来满足实际开发中 ...

随机推荐

  1. 【ZZ】Java : 一个帝国的诞生 & 假如时光能够倒流, 我会这么学习Java

    Java : 一个帝国的诞生 http://dy.qq.com/article.htm?id=20160523A06XFS00 写的很有意思,一下子了解了JAVA的历史. 假如时光能够倒流, 我会这么 ...

  2. 高级I/O之STREAMS

    http://en.wikipedia.org/wiki/STREAMS STREAMS(流)是系统V提供的构造内核设备驱动程序和网络协议包的一种通用方法,对STREAMS进行讨论的目的是为了理解系统 ...

  3. textarea 中的换行符问题

    下面是我对这个问题的解决过程,最后算是完全搞懂了,真是阴沟里险些翻船 1.必须知道textarea中的换行符是 \n  (个人检测发现按回车键是\n,好像在linux下是\r\n) 2.用nl2br之 ...

  4. Objective-C ,ios,iphone开发基础:http网络编程

    - (IBAction)loadData:(id)sender { NSURL* url = [NSURL URLWithString:@"http://162.105.65.251:808 ...

  5. 【线性结构上的动态规划】UVa 11400 - Lighting System Design

    Problem F Lighting System Design Input: Standard Input Output: Standard Output You are given the tas ...

  6. 2012蓝桥杯C组本科决赛答案

    题目: 脱氧核糖核酸即常说的DNA,是一类带有遗传信息的生物大分子.它由4种主要的脱氧核苷酸(dAMP.dGMP.dCMT和dTMP)通过磷酸二酯键连接而成.这4种核苷酸可以分别记为:A.G.C.T. ...

  7. webkit,HTML5头部标签

    大家都知道在移动前端开发中添加一些webkit专属的HTML5头部标签,帮助浏览器更好解析html代码,更好地将移动web前端页面表现出来.本文整理一些HTML5头部<meta>标签常用的 ...

  8. Java Concurrency - Phaser, Controlling phase change in concurrent phased tasks

    The Phaser class provides a method that is executed each time the phaser changes the phase. It's the ...

  9. Android学习之——图形图像处理(Bitmap、BitmapFactory)(一)

    转载自http://blog.csdn.net/csxwc/article/details/10345235 Bitmap是Android系统中的图像处理的最重要的类之一.用它可以获取图像文件信息,对 ...

  10. crackme_zapline分析

    [破文标题]crackme_zapline 分析 [破文作者]CloAk [作者邮箱]@qq.com [作者主页] [破解工具]OD,... [破解平台]Windows --------------- ...