C# - How To Add Placeholder Text To Textbox In C#
Project Source Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace MyApp
{
public partial class TextBoxPalceHolder : Form
{
public TextBoxPalceHolder()
{
InitializeComponent();
}
private void TextBoxPalceHolder_Load(object sender, EventArgs e)
{
this.ActiveControl = label1;
textBox1.GotFocus += new EventHandler(this.TextGotFocus);
textBox1.LostFocus += new EventHandler(this.TextLostFocus);
}
public void TextGotFocus(object sender , EventArgs e)
{
TextBox tb = (TextBox)sender;
if (tb.Text == "Your Text ..........")
{
tb.Text = "";
tb.ForeColor = Color.Black;
}
}
public void TextLostFocus(object sender , EventArgs e)
{
TextBox tb = (TextBox)sender;
if(tb.Text=="")
{
tb.Text = "Your Text ..........";
tb.ForeColor = Color.LightGray;
}
}
}
}
Download Projects Source Code