• 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.
  • Posts

    • GPUs selling higher than their MSRP is the new norm it seems. This shouldn't surprise anyone. Dumb duopoly, if Intel could just stop shooting itself in the foot and release some more cards and undercut the other two that'd be great.
    • I saw that recently, and the thing that would be useful also would be to have the PC model in that spec card as well. I know that it's on the main portal page of System, but repeating it in this "Device specs" module, especially if it's meant for the main specs of the computer, would certainly be useful.
    • Do you want a little in the ground or a lot in the air?
    • Bethesda readying two updates for Oblivion Remastered targeting bugs and performance by Pulasthi Ariyasinghe The surprise release of The Elder Scrolls IV: Oblivion Remastered was a massive success for Bethesda and Microsoft, with the title going on to gain over four million players in just three days at launch while also breaking franchise records for concurrent players. While reactions to the game have been positive from both critics and players, plenty of complaints have since been pouring in about the state of the remaster. Now, Bethesda is aiming to push out updates to resolve these concerns. Today, the company revealed that two updates are currently planned for the RPG, both with different scopes. The first of these is already available in beta form to Steam players. It seems Bethesda is using the same beta update format it uses for Starfield for this Elder Scrolls entry as well. "Thanks for all your excitement and feedback since the launch of The Elder Scrolls IV: Oblivion Remastered," said the development team today. "We are actively working on two separate updates to be launched in the coming weeks, both of which will come to our Steam Beta first, before being released to all other platforms." According to the studio, the first update is focused on "quests, major bugs and blockers, and quality of life fixes. " Following the June 5 launch on the Steam beta platform, all other PC players, as well as those on Xbox Series X|S, PlayStation 5, and Game Pass subscribers, will receive the update on June 11. The beta changelog for Update 1.1 can be seen here, with bug fixes coming for the UI, gameplay, quests, and more. Multiple crashes have been resolved here, with many being related to loading saves and exploring specific locations. Instances of infinite loading, alt-tab freezing, resetting settings, and more are being targeted with fixes, too. To opt into the Elder Scrolls IV: Oblivion Remastered beta update, Steam players can head into the game's properties, select Betas, and choose the [beta] branch to receive the new build. While Bethesda hasn't revealed any release dates for the second update, it did say that improving performance will be its main focus. This is a major point of criticism for the title at the moment, so the update can't come soon enough.
    • I wouldn't consider it harsh, but companies want as many loopholes and exceptions as possible. I agree on wanting everything to be above board. Hopefully they find a way to do this.
  • Recent Achievements

    • Week One Done
      jbatch earned a badge
      Week One Done
    • First Post
      Yianis earned a badge
      First Post
    • Rookie
      GTRoberts went up a rank
      Rookie
    • First Post
      James courage Tabla earned a badge
      First Post
    • Reacting Well
      James courage Tabla earned a badge
      Reacting Well
  • Popular Contributors

    1. 1
      +primortal
      408
    2. 2
      +FloatingFatMan
      181
    3. 3
      snowy owl
      179
    4. 4
      ATLien_0
      171
    5. 5
      Xenon
      135
  • Tell a friend

    Love Neowin? Tell a friend!