9

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?

J. Schiller
  • 125
  • 2
  • 11
Sinaesthetic
  • 11,426
  • 28
  • 107
  • 176
  • 2
    WinForms? WPF? Something else? – Mark Byers Nov 10 '10 at 10:27
  • where do u want the pop up to come? Tooltips appear on hover...if u want it to always be present its likely u want another element to appear on screen. Cause u will also have to set its position on screen. – Whimsical Nov 10 '10 at 10:51
  • 1
    i just want it to pop in its normal location, right next to the control. pop up and show for a couple seconds – Sinaesthetic Nov 10 '10 at 10:55

4 Answers4

22
"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);
    }
}
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
4

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.

yonan2236
  • 13,371
  • 33
  • 95
  • 141
0

Here you have an example. Play with properties of the tooltip and the tooltip will show as you want.

You have the Show method on the tooltip.

Note: .net 2.0 version

Liviu Mandras
  • 6,540
  • 2
  • 41
  • 65
  • 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 – Sinaesthetic Nov 10 '10 at 10:38
0

Is this a winforms app...if so Check here

If its a ASP.NET web app...you can add a field called tooltip

Community
  • 1
  • 1
Whimsical
  • 5,985
  • 1
  • 31
  • 39