I have a Form that contains a few textboxes. when loading the form, I want those textboxes to show a default grayed out text that disappears when the user clicks the textbox. I have used this code:
private void Form1_Load(object sender, EventArgs e)
{
//textbox1 default text
this.tBox1.Enter += new EventHandler(tBox1_Enter);
this.textbox1.Leave += new EventHandler(tBox1_Leave);
tBox1_SetText();
/*...
same for other textboxes
*/
}
/*textbox1 default text*/
// default greyed out text in tBox1
public void tBox1_SetText()
{
tBox1.Text = "some default text";
tBox1.ForeColor = Color.Gray;
}
// remove default text from tBox1 once cruser is set in
public void tBox1_Enter(object sender, EventArgs e)
{
if (tBox1.ForeColor == Color.Black)
return;
tBox1.Text = "";
tBox1.ForeColor = Color.Black;
}
// sets the default text back to tBox1 once cruser is gone
public void tBox1_Leave(object sender, EventArgs e)
{
if (tBox1.Text.Trim() == "")
tBox1_SetText();
}
/*...
same for other textboxes
*/
since I have a lot of textboxes I would like to move all this code from my form to a separate class (something like DefaultText.cs), create a method in that class ("ShowDefaultText()") and just call this method in form_load so my code would be more understandable. I tried creating this class but there is no #include in C# and I cant use static class/methods because of the instances. how can I do this otherwise?