1.11M Members

FormClosing Event Question

 
0
 

I have pretty much finished my program I'm writing. It's a program that fixes my issue with Firefox (It never deletes the saved sessions). I have the program minimize to the notification area, however, here's my dilemma: The way I'm doing it is by cancelling the formclosing event and instead minimizing. To close the program, I have a context menu on the notification icon with an exit option which uses the End method which is kind of annoying because it's not the right way to close a program and it leaves the icon behind until I hover over it.

The form is a fixed tool window, so it does not have a minimize button.

Here's the code for FormClosing and the exit option:

    Private Sub Form1_FormClosing(sender As System.Object, e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
        Try
            e.Cancel = True
            Me.WindowState = FormWindowState.Minimized
            Me.Hide()
        Catch ex As Exception
            MsgBox("Error: " & ex.Message, MsgBoxStyle.Critical, "Error")
        End Try
    End Sub



    Private Sub ExitToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles ExitToolStripMenuItem.Click
        Try
            End
        Catch ex As Exception
            MsgBox("Error: " & ex.Message, MsgBoxStyle.Critical, "Error")
        End Try
    End Sub

So, is there a way I can figure out which sub is calling FormClosing so the program can either properly close or minimize to tray depending on if you click the exit button on the form or the exit option from the menu? I tried having FormClosing also handle ExitToolStripMenuItem.Click and figure which is the sender but I knew that would not work.

I know it could be simply fixed by using a border style that has the minimize button, but I like the fixed tool window style since it's small and non-invasive. I'm also making this program way more complex then it should be considering it's just for me but it's good for learning and experience.

Thanks!

 
1
 

How about a global boolean named MinimizeOnClose which is normally set to True. You could set it to False in ExitToolStripMenuItem_Click. Then in Form1_FormClosing you could test for that and determine whether or not to hide or close. Then your menu handler becomes

Private Sub ExitToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles ExitToolStripMenuItem.Click
    MimimizeOnClose = False
    Me.Close()
End Sub
 
0
 

Thanks! I can't believe I didn't think about that before.

Question Answered as of 10 Months Ago by Reverend Jim
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article