Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to force a tooltip to show?

I have a method that validates a field. If it cannot be validated the field gets cleared and marked red.

I also would like a ToolTip to pop up over the box with a message for the user that the value is invalid. Is there a way to do this and maybe control how long the ToolTip shows? How can I make it pop up on it's own and not on mouse hover?

like image 336
Sinaesthetic Avatar asked Nov 10 '10 10:11

Sinaesthetic


1 Answers

"If the field cannot be validated, i have it clearing the box and marking it red. I would also like it to pop up a tooltip over the box saying that the value is invalid."

From the description of the behavior that you want, it sounds like you would be best served by the ErrorProvider component rather than a tooltip. The ErrorProvider component will automatically place an icon you specify next to the control that failed validation and display a tooltip to the user describing the validation error and/or the steps they need to take to correct it:

ErrorProvider component in action

There is a sample available on C# Corner, but it's very simple to implement. Simply add an ErrorProvider component to your form (it's available by default in the toolbox), then in your validation method, write the following code:

private void ValidateName()
{
 if (string.IsNullOrEmpty(NameTextBox.Text))
    {
            //Validation failed, so set an appropriate error message
            errorProvider.SetError(NameTextBox, "You must enter your name");
    }
    else
    {
            //Clear previous error message
            errorProvider.SetError(NameTextBox, string.Empty);
    }
}
like image 68
Cody Gray Avatar answered Oct 11 '22 12:10

Cody Gray