Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6777601
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:10:02+00:00 2026-05-26T16:10:02+00:00

I have a class clsContextPopUpMenu to create a ContextMenuStrip with some basic functions (e.g.copy)

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T16:10:02+00:00Added an answer on May 26, 2026 at 4:10 pm

    Revised answer due to better understanding of issue:

    Because the ContextMenuStrip implements IDisposable, you will either need to add it to the list of Components managed by the form so that it is disposed appropriately and automatically or manage the disposal yourself as suggested in your original question.

    Here is a revision of your class that will support the automatic disposal in the same way that windows would handle it if you were to add ContextMenuStrip directly to the form:

    Friend Sub New(ByVal objControl As System.Windows.Forms.Control, ByVal components As System.ComponentModel.IContainer)
    
        m_objControlContainer = objControl
        m_mnuCopyCell2Clipboard = New ToolStripMenuItem("Copy Cell")
        m_PopupMenu = New ContextMenuStrip(components)
        m_PopupMenu.Items.AddRange(New ToolStripMenuItem() {m_mnuCopyCell2Clipboard})
    End Sub
    

    To call this new constructor from within your form or user control:

     Private m_objPopUpMenu As clsContextPopUpMenu
     m_objPopUpMenu = New clsContextPopUpMenu(DGVTable, Me.components)
    

    Note that I also removed the ByRef from the constructor since it is not required, which also eliminates the need to cast the controls before passing them to the constructor.

    One additional note: it used to be (“back in the day”) that components was not necessarily present on every form or user control. I believe that this has changed/been fixed, but if you find yourself without, it is easy to add manually:

    Private components As System.ComponentModel.IContainer
    

    In your constructor:

    Me.components = New System.ComponentModel.Container()
    

    In your Dispose method (I have added the full dispose method in case it is not already present; if it is, just added the components-related code):

    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have class method that returns a list of employees that I can iterate
I have class, that contains different types and implements Iterable<Object> interface. But it is
I have class (MyCustomWebView) that extend webview can i do something like this ?
I have class A that has some primitive attributes and also member of type
I have class and properties in there. Some properties can be marked attribute (it's
I have class MyLogger, where I use log4net. How can I modify my appender
I have class with a member function that takes a default argument. struct Class
I have class A that is supposed to inherit class B whose name is
I have class A, with methods foo and bar, that are implemented by A1
I have class dot and have html code like that: <span class=dot>Text1</span> <span class=dot>Text2</span>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.