Dynamic ContextMenuStrip problem

I am dynamically adding ToolStripMenuItems to a context menu during the Opening event and those menu items are coming from the main menu.  The problem is that the first time the right-click event occurs and the opening event is called nothing shows up.  Repeated right-clicks after that work but until the main menu is activated again.  To rebuild the menus during load, I have an array that stores all the menu items needed and use that to repopulate the menu strips.  Is there somekind of setting or work-around for getting my ContextMenuStrip to show up the first time its opened

Answer this question

Dynamic ContextMenuStrip problem

  • aahd1

    If you'll notice, the Opening event on ContextMenuStrip is cancellable.  A ContextMenuStrip auto-cancels itself from showing if there are no items before the opening event.  Setting e.Cancel = false should allow you to dynamically populate the menu.


    public Form1() {
       InitializeComponent();
       this.ContextMenuStrip = new ContextMenuStrip();
       ContextMenuStrip.Opening +=
    new CancelEventHandler(contextMenuStrip1_Opening);
    }

    void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
    {
    e.Cancel =
    false;
    ContextMenuStrip.SuspendLayout();
    ContextMenuStrip.Items.Add(
    "one");
    ContextMenuStrip.Items.Add(
    "two");
    ContextMenuStrip.Items.Add(
    "three");
    ContextMenuStrip.ResumeLayout();
    }


     



    Similarly in VB (with a contextMenuStrip1 assigned to the Form.ContextMenuStrip)


    Private Sub ContextMenuStrip1_Opening(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles ContextMenuStrip1.Opening

    e.Cancel = False

    ContextMenuStrip1.SuspendLayout()
    ContextMenuStrip1.Items.Add(
    "one")
    ContextMenuStrip1.Items.Add(
    "two")
    ContextMenuStrip1.Items.Add(
    "three")
    ContextMenuStrip1.ResumeLayout()

    End Sub


     



    Jessica
    Developer,
    Windows Forms Team, Microsoft


  • fulgerica

     Quadrillion wrote:

    // This repopulates the context menu before opening
    System::Void CtrlEditor::FormContextMenu_Opening(System::Object^ sender, System::ComponentModel::CancelEventArgs^ e)
    {
       m_FormContextMenu->Items->Clear();
       for (int ii = 0; ii < m_EditMenuItems->Length; ii++)
       {
          m_FormContextMenu->Items->Add(m_EditMenuItems[ii]);
       }
    }


    set e.Cancel = false in this event.

  • mbudiman

    I've had good luck with dynamic loading of menu items in the Opening event without using any special settings or work-arounds.  Can you post some code   Perhaps the problem would present itself if given the specifics of your implementation.

     - Mitchell S. Honnert

  • Chris_Kavanagh

    Here is the code that represents what I am doing.  The other thing to note is that I am using a control embedded into an empty form.  This fails to load the context menu the first time, but then it works after that until the main menu is clicked on.  Then it fails again for the first time and so on.

    public ref class CtrlEditor : public System::Windows::Forms::UserControl
    {
       private:
          array<ToolStripItem^>      ^m_EditMenuItems;

       private: System::Windows::Forms::MenuStrip^  m_MainMenu;
       private: System::Windows::Forms::ToolStripMenuItem^  m_EditMenuItem;
       ...
    }

    // This function is called during the constructor to store menu items
    System::Void CtrlEditor::InitMenuArrays()
    {
       int ii;
      
       m_EditMenuItems = gcnew array<ToolStripItem^>(m_EditMenuItem->DropDownItems->Count);
       for (ii = 0; ii < m_EditMenuItems->Length; ii++)
       {
          m_EditMenuItems[ii] = m_EditMenuItem->DropDownItems[ii];
       }
    }

    // This repopulates the context menu before opening
    System::Void CtrlEditor::FormContextMenu_Opening(System::Object^ sender, System::ComponentModel::CancelEventArgs^ e)
    {
       m_FormContextMenu->Items->Clear();
       for (int ii = 0; ii < m_EditMenuItems->Length; ii++)
       {
          m_FormContextMenu->Items->Add(m_EditMenuItems[ii]);
       }
    }

    // The Edit Menu is part of the control's main menu
    // This repopulates the main menu before opening
    System::Void CtrlEditor::EditMenu_Opening(System::Object^ sender, System::EventArgs^ e)
    {
       m_EditMenuItem->DropDownItems->Clear();
       for (ii = 0; ii < m_EditMenuItems->Length; ii++)
       {
          m_EditMenuItem->DropDownItems->Add(m_EditMenuItems[ii]);
       }
    }


  • Dynamic ContextMenuStrip problem