Click here to Skip to main content
15,355,296 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, so I have been trying for the past while to get my program to only close when a button is pressed in another window. To clarify, I made a custom form to use as a message box specifically for the program. I want to use my custom message box to close a window when someone clicks the [X] button.

Let me paint a scenario: The user clicks the [X] button, my dialog forms prompts the user whether to continue closing the program. If the user clicks [Cancel], the dialog form closes and the main window remains open. Otherwise, both my custom dialog and the main form will close.

The only way I know how to do this now is to use the "e.cancel = true/false" arguement within the FormClosing event. But setting it to true will completely disable the ability for the program to close which is not what Im looking for. I want this to be done from the form's [X] Button. Is there a way to force close the main window from a second window button click?

Can someone pinpoint what Im doing wrong?

What I have tried:

Main Window
Private Sub Front_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
        Ending_Session_Quest.ShowDialog()

        Dim EndSession As DialogResult
        Ending_Session_Quest.btn_Cancel.DialogResult = DialogResult.Cancel
        Ending_Session_Quest.btn_OK.DialogResult = DialogResult.OK

        If EndSession = System.Windows.Forms.DialogResult.Cancel Then
            e.Cancel = True
        Else
            e.Cancel = False
            Shut_Down.Show()
        End If
    End Sub


Custom Dialog Window
Public Class Ending_Session_Quest

    Private Sub btn_OK_Click(sender As Object, e As EventArgs) Handles btn_OK.Click
        Front.DialogResult = System.Windows.Forms.DialogResult.OK

    End Sub

    Private Sub btn_Cancel_Click(sender As Object, e As EventArgs) Handles btn_Cancel.Click
        Front.DialogResult = System.Windows.Forms.DialogResult.Cancel
    End Sub
Posted
Updated 21-Mar-22 2:39am
Comments
Maciej Los 21-Mar-22 8:30am
   
How do you call your cstom dialog form?
Delta Boogaloo 21-Mar-22 8:31am
   
Currently when a user clicks the [X] button
Maciej Los 21-Mar-22 8:32am
   
I'm not asking how you close top window, but how do you call it from main window. I need to see your code.
Delta Boogaloo 21-Mar-22 8:35am
   
Its called from a menu item

Private Sub CloseToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles CloseToolStripMenuItem.Click
Ending_Session_Quest.ShowDialog()
End Sub

1 solution

I usually just add a Property to the form to indicate how the form is being closed - e.g. from within Code or via a User action. Then only set cancel based on that property or if the "close" is coming from your code, allow it through

As each form is a class you can add a property to it - here are some examples of how https://www.dotnetperls.com/property-vbnet[^]
VB
Private Sub CloseToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles CloseToolStripMenuItem.Click
    If Ending_Session_Quest.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
         This.AllowClose = True
    Else
         This.AllowClose= False
    End If
End Sub
Then in your Form closing event so something like this
VB
Private Sub Form1_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
    If Not This.AllowClose Then
        e.Cancel = False
    End If
End Sub
   
v2
Comments
Delta Boogaloo 21-Mar-22 8:53am
   
And how do I do that?
CHill60 21-Mar-22 12:51pm
   
I've amended my solution - but be aware I couldn't test this as I don't have access to VB at the moment
Maciej Los 21-Mar-22 15:19pm
   
5ed!
Delta Boogaloo 22-Mar-22 8:21am
   
Ah I see it now. Thank you :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900