1

I am coding a C# Forms application and I have a question about validating a form.

I have a textbox called textBoxCodeToSearchFor that I want to validate when I press a button. If the textbox does not have any text in it, I need a tooltip to be shown.

Here is my code:

private void textBoxCodeToSearchFor_Validating(object sender, CancelEventArgs e)
{
    if (String.IsNullOrEmpty(textBoxCodeToSearchFor.Text))
    {
        e.Cancel = true;
        toolTip.Show("Please enter the code to search for", textBoxCodeToSearchFor);
    }
}

On the button click I have the following code:

bool validated = this.Validate();

The tooltip is then shown, however, I cannot close the form when pressing a cancel button.

How can I shown a tooltip for a textbox if the textbox does not validate, but still close out of the form is wanted?

Thanks in advance.

Simon
  • 7,991
  • 21
  • 83
  • 163
  • have you tried creating an onclosing event and seeing if its reached when you press the close button? it may be practical for you to dispose of the tooltip prior to the form closing. – horHAY Apr 09 '15 at 10:10
  • possible duplicate of [WinForm UI Validation](http://stackoverflow.com/questions/769184/winform-ui-validation) – Clint Apr 09 '15 at 10:41
  • possible duplicate of [How to skip Validating after clicking on a Form's Cancel button](http://stackoverflow.com/questions/1882523/how-to-skip-validating-after-clicking-on-a-forms-cancel-button) – Vojtěch Dohnal Apr 09 '15 at 11:34

0 Answers0