• 0

[VB.NET 2005] Best way to exit the application with a NotifyIcon


Question

Most of my applications, always add a NotifyIcon control where it would be visible all the time the application was running but I've always confused a lot when coding specific operations when using a NotifyIcon. In this topic I was having a little problem with an icon appearing twice in the notification area and so, after finding a way to fix it, here's how I start my applications with a notification area icon:

I have a main form called FormMain and a main module called ModuleMain. The application starting point, will be Sub Main() in ModuleMain and goes like this:

Module ModuleMain

	Friend Structure AGSStructSettings
		Dim StartHidden As Boolean
		Dim AskConfirmation As Boolean
		Dim MinimizeOnClose As Boolean
	End Structure

	Dim APPMain As FormMain

	Public Sub Main()
		APPMain = New FormMain
		APPMain.NotifyIcon.Visible = True

		' Should the main form start visible or hidden?
		If Settings.StartHidden = True Then
			Application.Run()
		Else
			Application.Run(APPMain)
		End If
	End Sub

End Module

Normally, this applications where I use a NotifyIcon, always have a context menu associated where Exit is always an option, this Exit will terminate the application. On this application example, I have some settings that are configurable by the user, which are: StartHidden, AskConfirmation and MinimizeOnClose; All of them are booleans and if they are True they will, start the application with the main form window hidden, ask confirmation before closing the main form window (exiting from the notification area does not ask for confirmation) and clicking the top-right X button on the main form window or pressing ALT+F4 will hide the main form window, respectively.

For both AskConfirmation and MinimizeOnClose (StartHidden is already explained in the Sub Main()), I have the following code:

Public Class FormMain

	Dim ExitFromNotificationArea As Boolean

	Private Sub FormMain_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
		If Not e.CloseReason = CloseReason.WindowsShutDown Or Not ExitFromNotificationArea Then
			If Settings.MinimizeOnClose Then
				e.Cancel = True
				ShowHideMainWindow()
			Else
				If Settings.AskConfirmation Then
					Dim DResult As DialogResult = MessageBox.Show("Doreally want to exit APPLICATION?", _
																  "APPLICATION", MessageBoxButtons.YesNo, _
																  MessageBoxIcon.Question)

					If DResult = Windows.Forms.DialogResult.No Then
						e.Cancel = True
					End If
				End If
			End If

			If e.Cancel = False Then
				Me.NotifyIcon.Visible = False
				Me.Dispose()
				Application.Exit()
			End If
		End If
	End Sub

	Private Sub ShowHideMainWindow()
		If Not Me.Visible Then
			Me.Show()
		Else
			Me.Hide()
		End If
	End Sub

	Private Sub mnuExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuExit.Click
		ExitFromNotificationArea = True

		Me.NotifyIcon.Visible = False
		Me.Dispose()
		Application.Exit()
	End Sub

End Class

What I want to know: Is this the best way to achieve all I have explained earlier? Can I optimize this code in any way? To make it more readable or better in someway? Do you spot any problems in this code? I'm asking all this cause I've always tested the application through but I've always thought that I'm missing something or something will not exactly work as I expected. Just a feeling I have, that this code, can be improved... If someone could take a look at it and suggest me improvements, I would really appreciate it.

5 answers to this question

Recommended Posts

  • 0

The code looks fine to me. I don't see any unneeded methods or anything. However, I would refactor the two instances of "Me.NotifyIcon.Visible = False", "Me.Dispose()", and "Application.Exit()" into a function such as "ExitApplication()" or something.

I'm not exactly good at writing elegant code, so don't rely solely on my opinion. ;)

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.