Is there a way to force a tooltip to show?

29,304

Solution 1

"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);
    }
}

Solution 2

Try this:
Basically,

private void button1_Click(object sender, EventArgs e)
{
    ToolTip toolTip1 = new ToolTip();
    toolTip1.Title = "Invalid entry"; // Title to display.
    toolTip1.Show("Please enter a number.", textBox1); // Message of the toolTip and to what control to appear.
}

But there are 5 other overloads for ToolTip. You can see it here.

Share:
29,304
Sinaesthetic
Author by

Sinaesthetic

Updated on August 01, 2022

Comments

  • Sinaesthetic
    Sinaesthetic almost 2 years

    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?

  • Sinaesthetic
    Sinaesthetic over 13 years
    ok so i got it to set the properties. but im missing how to get it to show automatically rather than only on mouse hover