locked
Is It Possible To Setup A Confirmation Message When Closing Form1 From The Control Box RRS feed

  • Question

  • Hello community quick query please...

    I know well enough how to setup a message box to prompt for yes/no confirmation to close the main from from say a button click event so users dont accidentally close my app with one click. My Problem is when users are closing from the control box.

    My question is How to i set up confirming closes from the top right [X] in the control box. The only event handler i can find that activates from that event is the form close event. But obviously putting my confirmation in there is pointless, its already closing. Unless of course there is a way to cancel the close event after its started?

    My workaround atm is to disable the control box (which i dont want to do) and use a button click event to run a confirmed exit.

    Is it possible to setup confirmation dialog on controlbox closes please? If so How Please?

    ty for help

    greatly appreciated

    {-_o}


    • Edited by AussieHack Saturday, August 3, 2013 11:39 AM
    Saturday, August 3, 2013 11:38 AM

Answers

  • Try the FormClosing event. Set e.Cancel = True to prevent closing.

    Armin

    Saturday, August 3, 2013 11:58 AM
  • Hi,

     As Armin said, use the Form_Closing event and set (e.cancel = True) if the messagebox returns the button you are using. Here is an example that if the user tries closing the form the messagebox asks to confirm and if the user clicks the (Yes) button then the program closes and if the user clicks the (No) button then e.cancel is set to True.

        Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
            Dim ret As DialogResult = MessageBox.Show("Are you sure you want to close the program?", "Closing...", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
            If ret = Windows.Forms.DialogResult.No Then e.Cancel = True
        End Sub
    

    • Proposed as answer by Frank L. Smith Saturday, August 3, 2013 4:48 PM
    • Marked as answer by AussieHack Sunday, August 4, 2013 2:24 AM
    Saturday, August 3, 2013 12:55 PM

All replies

  • Try the FormClosing event. Set e.Cancel = True to prevent closing.

    Armin

    Saturday, August 3, 2013 11:58 AM
  • Hi,

     As Armin said, use the Form_Closing event and set (e.cancel = True) if the messagebox returns the button you are using. Here is an example that if the user tries closing the form the messagebox asks to confirm and if the user clicks the (Yes) button then the program closes and if the user clicks the (No) button then e.cancel is set to True.

        Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
            Dim ret As DialogResult = MessageBox.Show("Are you sure you want to close the program?", "Closing...", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
            If ret = Windows.Forms.DialogResult.No Then e.Cancel = True
        End Sub
    

    • Proposed as answer by Frank L. Smith Saturday, August 3, 2013 4:48 PM
    • Marked as answer by AussieHack Sunday, August 4, 2013 2:24 AM
    Saturday, August 3, 2013 12:55 PM
  • Hi,

     As Armin said, use the Form_Closing event and set (e.cancel = True) if the messagebox returns the button you are using. Here is an example that if the user tries closing the form the messagebox asks to confirm and if the user clicks the (Yes) button then the program closes and if the user clicks the (No) button then e.cancel is set to True.

        Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
            Dim ret As DialogResult = MessageBox.Show("Are you sure you want to close the program?", "Closing...", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
            If ret = Windows.Forms.DialogResult.No Then e.Cancel = True
        End Sub
    


    Ty works a treat
    Sunday, August 4, 2013 2:24 AM