1. 定义提示文字内容
private readonly string RemarkText = "最多输入100字"; // 提示文字
2. 添加textbox 焦点事件, 初始化textbox提示文字和字体颜色
public UserControl(){InitializeComponent();tb_Remark.Text = RemarkText;tb_Remark.ForeColor = Color.Gray;//Lostfocus当一个控件失去焦点时触发,定义控件失去焦点时的方法this.tb_Remark.LostFocus += new System.EventHandler(this.tb_Remark_LostFocus);//GotFocus是在一个对象得到焦点时发生,定义控件得到焦点时的方法this.tb_Remark.GotFocus += new System.EventHandler(this.tb_Remark_GotFocus);}
3. 事件处理
private void tb_Remark_GotFocus(object sender, EventArgs e){if (tb_Remark.Text.Trim() == RemarkText)tb_Remark.Text = ""; tb_Remark.ForeColor = Color.Black;}private void tb_Remark_LostFocus(object sender, EventArgs e){if (tb_Remark.Text.Trim() == ""){tb_Remark.Text = RemarkText;tb_Remark.ForeColor = Color.Gray;}}
4. 获取textbox 输入内容需做判断
if (this.tb_Remark.Text.Trim() == RemarkText)friendInformation.Remark = "";elsefriendInformation.Remark = this.tb_Remark.Text.Trim();
效果