I've created a VB.Net application which consists of a form and a series of classes. One class (GUI) contains all of the routines that handle the logic associated with the controls on my form. These controls are placed at design time.
My application runs in the system tray so I need to trap the FormClosing event so that the application is minimised rather than closed. Before moving everything into the GUI class my code worked but I'm now getting an error on this line:
Private Sub Monitor_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
which is "Event 'FormClosing' cannot be found"
I'm completely stumped by this. I've changed changing the MyBase parameter to refer to the form by name, leaving it off and also adding:
Private WithEvents Monitor As System.Windows.Forms.Form
but none work.
EDIT: Here's a stripped down version of my GUI class containing the bits that handle minimising and restoring the form to the system tray
Note that there's no reference to the menu bar and menu items but VS doesn't complain about those.
Public Class GUI
' Make form controls accessible to this class (partial list only
' there's a lot of controls)
Private WithEvents tabControl As TabControl
Private WithEvents btn_status_start As Button
Private WithEvents btn_status_stop As Button
Private WithEvents btn_dataSource As Button
' **************************************************************************************
' * *
' * Control form behaviour so that it can be put into and taken from system tray *
' * *
' **************************************************************************************
Private Sub cms_menu_exit_Click(sender As Object, e As EventArgs) Handles cms_menu_exit.Click
' Handles the saving and shutting down of the program
' Save any settings
My.Settings.Save()
' Remove the icon
Monitor.Dispose()
' Finally close the program
Monitor.Close()
End Sub
Private Sub Notify_MouseDoubleClick(sender As Object, e As MouseEventArgs) Handles notify.MouseDoubleClick
' Code to restore the program window from the system tray when double clicked
Me.Restore()
End Sub
Private Sub Notify_MouseClick(sender As Object, e As MouseEventArgs) Handles notify.MouseClick
' Code to restore the program window from the system tray when single clicked
Me.Restore()
Monitor.Focus()
End Sub
Private Sub Restore()
If Monitor.WindowState = FormWindowState.Minimized Then
Monitor.WindowState = FormWindowState.Normal
End If
Monitor.Visible = True
End Sub
Private Sub Monitor_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
' Catch the click on the X on the main form and set program to minimise instead
If (e.CloseReason = CloseReason.UserClosing) Then
e.Cancel = True
Monitor.Hide()
End If
End Sub
Private Sub cms_menu_open_Click(sender As Object, e As EventArgs) Handles cms_menu_open.Click
' Maximise the main form if selected
Monitor.Show()
Monitor.Focus()
End Sub
End Class