hello,

which event is fired when i close form with X button ? i want event that fired only with X button pressed, i know that there is a FormClosing event, but the problem is that it fires each time when form is closed... also with frm.close()

and i don't want that happen

tanx

You mean you don't want the user to be able to click on it?
A FormClosing event happens before a Form is closed, a FormClosed event happens after a Form is closed. You don't need to implement these if you don't want to.

commented: Thats how I would do it +14

no,
what i meant to is that i want to catch some event which is fired when i press X button on the form and only then,
and if i have line like " frm.close()" in my code, the event wont be fired...

You could also set the ControlBox property of the Form to false.

As far as I know the "X" just sends the form close message to your form and you cannot distinguish the caller. This will work though:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace daniweb
{
  public partial class frmImage : Form
  {
    private bool allowFormClose = false;

    public frmImage()
    {
      InitializeComponent();
    }

    protected override void WndProc(ref Message m)
    {
      if (!allowFormClose && (m.Msg == 0x0010)) //form "X"
      {
        System.Diagnostics.Debugger.Break();
        //Do something
      }
      else
        base.WndProc(ref m);
    }

    private void button2_Click(object sender, EventArgs e)
    {
      allowFormClose = true;
      this.Close();
    }
  }
}

[edit]
This is the functionally identical to just canceling the form close in the "FormClosing" event that ddanbe pointed out, with a lot less of a headache.
[/edit]

Wait -- You can do it!:icon_cheesygrin:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace daniweb
{
  public partial class frmImage : Form
  {
    private bool allowFormClose = false;

    public frmImage()
    {
      InitializeComponent();
    }

    protected override void WndProc(ref Message m)
    {
      if (!allowFormClose && (m.Msg == 0x0010)) //form "X"
      {
        //System.Diagnostics.Debugger.Break();
        //Do something
      }
      else
        base.WndProc(ref m);
    }

    new void Close()
    {
      allowFormClose = true;
      base.Close();
    }

    private void button2_Click(object sender, EventArgs e)
    {
      this.Close(); //will work
    }

    private void button3_Click(object sender, EventArgs e)
    {
      (this as Form).Close(); //will not work
    }
  }
}

But this method doesn't work very well with inheritance and will probably break things down the road. You're still better off listening to Danny.

That's interesting sknake, I never would have thought of catching the close method like that.

But as far as all these answers go, the Simple response is the X button does only call the form to close. and however you catch that close call, either by wndproc or by a simple form closing event, the trick is to set a flag, just a simple Boolean variable, if its true, cancel the closing and handle your business, if its false, do nothing and allow the form to close.

when you need to call this.Close(); first change your variable. else you can create a public method for your form called DoClose() or something and change the flag there and call close, its up to you.

commented: Simple things are often the best! +12
commented: Thats what I would do +14
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.