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

Dynamic ContextMenuStrip problem
aahd1
public Form1() {
void contextMenuStrip1_Opening(object sender, CancelEventArgs e)InitializeComponent();
this.ContextMenuStrip = new ContextMenuStrip();
ContextMenuStrip.Opening += new CancelEventHandler(contextMenuStrip1_Opening);
}
{
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 =
FalseContextMenuStrip1.SuspendLayout()
"one")ContextMenuStrip1.Items.Add(
ContextMenuStrip1.Items.Add("two")
ContextMenuStrip1.Items.Add("three")
ContextMenuStrip1.ResumeLayout() End Sub
Jessica
Developer,
Windows Forms Team, Microsoft
fulgerica
set e.Cancel = false in this event.
mbudiman
- 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]);
}
}