I've got a standard TextBox control which I'm trying to have mimic the "soft descriptions" like those found in the title and tags boxes on StackOverflow. Essentially, when the user's focus enters the control, it hides the description ("Username") in this case, and sets alignment and color to be that of a standard text control. When the user leaves the textbox, I want to check if the user actually entered anything, and put the username display back up otherwise.
For example:
private void tbUsername_Enter(object sender, EventArgs e)
{
if (tbUsername.TextAlign == HorizontalAlignment.Center)
{
tbUsername.TextAlign = HorizontalAlignment.Left;
tbUsername.ForeColor = SystemColors.ControlText;
tbUsername.Text = String.Empty;
}
}
private void tbUsername_Leave(object sender, EventArgs e)
{
if (tbUsername.Text == String.Empty)
{
tbUsername.TextAlign = HorizontalAlignment.Center;
tbUsername.ForeColor = SystemColors.InactiveCaption;
tbUsername.Text = "Username";
}
}
Unfortunately, when I setup these events, the user cannot tab out of the username control. The control simply flickers and control returns to the textbox control itself until the user has entered something, skipping over the event body.
If I call this.SelectNextControl()
in the event, then the event enters an infinite loop.
Does anybody see what I'm doing wrong?