I have a class clsContextPopUpMenu
to create a ContextMenuStrip with some basic functions (e.g.copy) that I can use in different controls.
Friend Sub New(ByRef objControl As System.Windows.Forms.Control)
m_objControlContainer = objControl
m_mnuCopyCell2Clipboard = New ToolStripMenuItem("Copy Cell")
m_PopupMenu = New ContextMenuStrip
m_PopupMenu.Items.AddRange(New ToolStripMenuItem() {m_mnuCopyCell2Clipboard})
End Sub
For example, I can use it in a DataGridView DGVTable:
Private m_objPopUpMenu As clsContextPopUpMenu
m_objPopUpMenu = New clsContextPopUpMenu(CType(DGVTable, System.Windows.Forms.Control))
However, note that m_objPopUpMenu
IS NOT associated with the form having the above datagridview. According to ContextMenuStrip constructor explanation in MSDN, I think that m_objPopUpMenu
cannot be disposed automatically since it is not a child of the form.
My question is, do I have to explicitly dispose m_objPopUpMenu
in designer:
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
Try
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
**m_objPopUpMenu.Dispose()**
End If
Finally
MyBase.Dispose(disposing)
End Try
End Sub
A broader question is that when should I dispose objects/resource by myself? Of course, gc collector is not a magician to release all available memory. Can I always dispose objects/resource in Dispose Sub as shown above?