How to stop textbox leave event firing on form close

好久不见. 提交于 2019-12-05 00:11:51


using c# winforms vs2008

I've got a textbox on a form with a method being called from the textBox1_Leave event. The method takes the contents of the textbox in question and populates other textboxes based on the contents. My problem is that is the user has focus on the text box then clicks the button to close the form (calling this.close) then the form does not close because the textbox leave event gets fired. If the textbox does not have focus on form close then the form closes fine.

If however a user closes the form by clicking the little X close icon in the top corner the it closes fine all the time with out the textbox leave event being fired.

How can I duplicate the X close functionality so that I can always close the form without the textbox leave event being fired?


The simplest solution is going to be to check which control is actually focused before doing your post-processing - but you can't do it in the Leave handler, because the focus will still be on the text box at that point.

Instead, you need to move your logic to the LostFocus event, which is not in the designer. You'll have to wire it up at runtime:

public class Form1 : Form
    public Form1()
        textBox1.LostFocus += new EventHandler(textBox1_LostFocus);

    private void textBox1_LostFocus(object sender, EventArgs e)
        if (closeButton.Focused)
        // Update the other text boxes here

The LostFocus event happens to fire after the new control receives focus.

Clarification - you might find that it works by putting this logic in the Leave event - if the focus is changed by the mouse. If the keyboard is used instead, you'll get the wrong behaviour. LostFocus is reliable in both cases - the focused control will always be the "new" control. This is documented on MSDN: Order of Events in Windows Forms.

Incidentally, the reason why you're not having this problem with the "red X" is that the X is not actually a control that can receive focus, it's part of the window. When the user clicks that, it's not causing the text box to lose focus, and therefore isn't causing the Leave event to fire.


Another approach: Use the textbox's validating event instead of it's leave event, then change the button's CausesValidation property to false. You will also have to set the textbox to not cause validation in the button's click event so the validating event will not fire when the form is closing (thanks to @Powerlord for pointing this out).

private void button1_Click(object sender, EventArgs e)
    this.textBox1.CausesValidation = false;


You could also handle the FormClosing event and make sure the e.Cancel argument does not get set to true by the validating events on the other controls on the form. I think they will be fired off before the FormClosing event.

    private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
            if (e.CloseReason == CloseReason.UserClosing)
                e.Cancel = false;


you can check to see which control has just got focus.

private void textBox1_Leave(object sender, EventArgs e)
    if (btnClose.Focused)
    // go from here


Just check if the form owning the textbox is disposing? If it's getting closed, it's disposing. If it's disposing you could simply end the pesky 'leave' event without doing anything. I didn't check it and forgive me, I'm choked on a project of my own so and I was searching myself, so I don't think I'll have time for that.

private void GuiltyTextBox_Leave(object sender, EventArgs e) {
  Form formOwningTheTextBox=(Form)((Control)sender).TopLevelControl;
  if (formOwningTheTextBox.Disposing || formOwningTheTextBox.IsDisposed) return;

I just believe this is going to work with minimum effort and wanted to send a quick answer before I resume searching my own answer.


Write Following line of code in text box leave event on top

if me.closing then
end if