VSTO之带PlaceHolder的TextBox

4 篇文章 0 订阅
2 篇文章 0 订阅

方法一

实现效果:

在这里插入图片描述

代码:

PlaceHolderTextBox.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

/// <summary>
/// 带有PlaceHolder的 TextBox
/// </summary>
/// <creator>Alice</creator>
public partial class PlaceHolderTextBox : TextBox 
{
    private bool _isPlaceHolder = true;
    private string _placeHolderText;
    /// <summary>
    /// 提示文本
    /// </summary>
    public string PlaceHolderText
    {
        get { return _placeHolderText; }
        set
        {
            _placeHolderText = value;
            SetPlaceholder();
        }
    }

    /// <summary>
    /// 文本
    /// </summary>
    public new string Text
    {
        get
        {
            return _isPlaceHolder ? string.Empty : base.Text;
        }
        set
        {
            base.Text = value;
        }
    }

    /// <summary>
    /// 构造函数
    /// </summary>
    public PlaceHolderTextBox()
    {
        GotFocus += RemovePlaceHolder;
        LostFocus += SetPlaceholder;
    }

    /// <summary>
    /// 当焦点失去的时候,将清空提示文本
    /// </summary>
    private void SetPlaceholder()
    {
        if (string.IsNullOrEmpty(base.Text))
        {
            base.Text = PlaceHolderText;
            this.ForeColor = Color.Gray;
            this.Font = new Font(this.Font, FontStyle.Italic);
            _isPlaceHolder = true;
        }
    }

    /// <summary>
    /// 当焦点获得的时候,将显示提示文本
    /// </summary>
    private void RemovePlaceHolder()
    {
        if (_isPlaceHolder)
        {
            base.Text = "";
            this.ForeColor = SystemColors.WindowText;
            this.Font = new Font(this.Font, FontStyle.Regular);
            _isPlaceHolder = false;
        }
    }

    /// <summary>
    /// 失去焦点
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void SetPlaceholder(object sender, EventArgs e)
    {
        SetPlaceholder();
    }

    /// <summary>
    /// 获得焦点
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void RemovePlaceHolder(object sender, EventArgs e)
    {
        RemovePlaceHolder();
    }
}

PlaceHolderTextBox.Designer.cs

partial class PlaceHolderTextBox
{
    /// <summary> 
    /// 必需的设计器变量。
    /// </summary>
    private System.ComponentModel.IContainer components = null;

    /// <summary> 
    /// 清理所有正在使用的资源。
    /// </summary>
    /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

    #region 组件设计器生成的代码

    /// <summary> 
    /// 设计器支持所需的方法 - 不要修改
    /// 使用代码编辑器修改此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
        this.textBox1 = new System.Windows.Forms.TextBox();
        this.SuspendLayout();
        // 
        // textBox1
        // 
        this.textBox1.Location = new System.Drawing.Point(0, 0);
        this.textBox1.Name = "textBox1";
        this.textBox1.Size = new System.Drawing.Size(100, 96);
        this.textBox1.TabIndex = 0;
        this.textBox1.Text = "";
        this.ResumeLayout(false);

    }

    #endregion

    private System.Windows.Forms.TextBox textBox1;
}

使用

在工具箱中找到如下组件,同TextBox一样,拖入窗体就可以用啦。
在这里插入图片描述
使用时只需要在如下属性中添加提示。
在这里插入图片描述

提示

以上这种方法有个问题,就是设置this.XXX.Text的时候会变成PlaceHolderText;

方法二

代码

PlaceHolderRichTextBox.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
public partial class PlaceHolderRichTextBox : RichTextBox
{
    public String PlaceHolderStr { get; set; }
    protected override void WndProc(ref Message m)
    {
        base.WndProc(ref m);
        if (m.Msg == 0xF || m.Msg == 0x133)
        {
            WmPaint(ref m);
        }
    }
    private void WmPaint(ref Message m)
    {
        Graphics g = Graphics.FromHwnd(base.Handle);
        if (!String.IsNullOrEmpty(this.PlaceHolderStr) && string.IsNullOrEmpty(this.Text))
            g.DrawString(this.PlaceHolderStr, this.Font, new SolidBrush(Color.LightGray), 0, 0);
    }
}

PlaceHolderRichTextBox.Designer.cs

partial class PlaceHolderRichTextBox
{
    /// <summary> 
    /// 必需的设计器变量。
    /// </summary>
    private System.ComponentModel.IContainer components = null;

    /// <summary> 
    /// 清理所有正在使用的资源。
    /// </summary>
    /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

    #region 组件设计器生成的代码

    /// <summary> 
    /// 设计器支持所需的方法 - 不要修改
    /// 使用代码编辑器修改此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
        this.richTextBox1 = new System.Windows.Forms.RichTextBox();
        this.SuspendLayout();
        // 
        // richTextBox1
        // 
        this.richTextBox1.Location = new System.Drawing.Point(27, 96);
        this.richTextBox1.Name = "richTextBox1";
        this.richTextBox1.Size = new System.Drawing.Size(100, 21);
        this.richTextBox1.TabIndex = 0;
        this.ResumeLayout(false);
        this.PerformLayout();

    }

    #endregion

    private System.Windows.Forms.RichTextBox richTextBox1;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值