Jump to content

The X button

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1
dirkfirst

dirkfirst

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 354 posts
How can I see and control when the user presses the X button in the upper right hand corner?

I'd like to bring up a msgbox that asks if the user really wants to quit. I notice a lot of programs do this. Is it possible to disable the X button at certain times?

#2
Guest_NeedHelp_*

Guest_NeedHelp_*
  • Guests
Click on your form and then click on the lightning bolt. It is one of those options, maybe close?

#3
dirkfirst

dirkfirst

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 354 posts
Where? I do not see it.

#4
hoser2001

hoser2001

    Programmer

  • Members
  • PipPipPipPip
  • 173 posts

dirkfirst said:

How can I see and control when the user presses the X button in the upper right hand corner? I'd like to bring up a msgbox that asks if the user really wants to quit.

In your dialog class (the one you want to confirm closing) There is a FormClosing event that gets called before the closing happens. Here is an example:

private void MainApp_FormClosing(object sender, FormClosingEventArgs e)
{
if (MessageBox.Show("Are you sure you want to close?", "Confirm Close", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
e.Cancel = false;
}
else
{
e.Cancel = true;
}
}

This is a very primitive C# example, but it works.

dirkfirst said:

I notice a lot of programs do this. Is it possible to disable the X button at certain times?

There are two ways to do this, the only one I can remember is the "control box" propery, set this to 'false' and it will disable all the buttons in the top right of the screen. There is a property for each of these individually, so you should be able to find a property for the exit button.

To disable and enable it during your code merely add this line to disable:

Form.Controlbox = false; (or form.controlbox.enabled = false;) i dont remember

and the obvious to re-enable it.