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?
"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:
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);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With