How to close an application??

  • Thread starter Thread starter pamelafluente
  • Start date Start date
P

pamelafluente

Hi Guys,

I have a small application which uses a NotifyIcon. The user can set
the program so that when he clicks on the form-cancel button "X", the
program will instead be minimized in the icon try. See code below.

My problem is that my code is problably too naive. In fact there is a
problem when one tries to shut dow or restart Windows. Windows does not
want to shut down!

Could anybody by so kind as to suggest me the correct approach to do
this. Is there a way to detect the the closing is due to system restart
or shutdown?

Thank you very much in advance,

-Pam

-----------------------------------------------------------------------------

Private PreventClosing As Boolean
'if this is set TRUE by the user, the program minimizes (instead of
closing) when "X" is pressed

Private ShowInTaskbarSaved As Boolean
Private WindowStateSaved As FormWindowState

'...

Private Sub Form1_Closing(ByVal sender As Object, ByVal e As
System.ComponentModel.CancelEventArgs) Handles MyBase.Closing

If Me.PreventClosing Then
Me.HideInTrayIcon()
e.Cancel = True
End If

End Sub

Private Sub HideInTrayIcon()

Me.WindowStateSaved = Me.WindowState
Me.WindowState = FormWindowState.Minimized
Me.ShowInTaskbar = False
Me.NotifyIcon1.Visible = True

End Sub


'...
Private Sub RestoreWindow()
Me.NotifyIcon1.Visible = False
Me.ShowInTaskbar = Me.ShowInTaskbarSaved
Me.WindowState = Me.WindowStateSaved
Me.Show()
Me.Focus()
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top