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.
For quite a while now I've been using my own hosted nextcloud server for both mobiles and PCs/laptops. Also all my personal systems run windows 11 pro for workstation or windows 11 ltsc. All using local accounts. What annoys me is that MS keeps trying to force you to "use this (MSA) account system wide". Everything at home is on my own DC/AD. Why is this even a question? Why is MS forcing this onto people? Sure for windows home versions do whatever you want. But for pro/ pro for workstation or ltsc no. Just don't.
Not only its still 3 Euros listed for me but it doesn't seem to even support newer versions of Android, it just tells me it's incompatible with my device. EP2 works.
I have seen what it takes to uninstall co-pilot, it is not elegant and certainly not a straight forward Uninstall. Can't they just do what Apple have done, and just have a setting to turn it off?
i don't get all this pushing/forcing to get people to use Ai or get people to have accounts and that sort of thing. If people want to use the services, then they will, they don't need pushing.
They push/force, then they get people like me who looks on it with suspicion and find anyway to disable or not use the service
Question
ProclaimDragon
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:
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:
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.
Link to comment
https://www.neowin.net/forum/topic/533700-vbnet-2005-best-way-to-exit-the-application-with-a-notifyicon/Share on other sites
5 answers to this question
Recommended Posts