文章目录
方法一
实现效果:
代码:
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;
}