Dynamic Menus in C# and Popup event handler

R

RahimAsif

I am writing an application that requires the a portion of the main
menu to be dynamic. The menu has file, panels, view files and help
across the top. The view files sub menu needs to be dynamically
generated, and the dynamic generation needs to occur right when the
user selects this menu item (that is on the Popup event handler).
However, everytime I put following code on the Popup event handler (of
the View Files menuitem) to dynamically generate the menu, I get
exceptions:

MenuItem x = new MenuItem("Item 1");
MenuItem y = new MenuItem("Item 2");
this.MenuItemViewFiles.MenuItems.Add(x);
this.MenuItemViewFiles.MenuItems.Add(y);

Note that if I put the code somewhere else, such as when the window
loads, the same code works perfectly fine.

Here's the exception that I get:

at System.Windows.Forms.MenuItem.get_MdiList()
at System.Windows.Forms.MenuItem.OnPopup(EventArgs e)
at System.Windows.Forms.MenuItem.OnInitMenuPopup(EventArgs e)
at System.Windows.Forms.MenuItem._OnInitMenuPopup(EventArgs e)
at System.Windows.Forms.Menu.ProcessInitMenuPopup(IntPtr handle)
at System.Windows.Forms.Form.WmInitMenuPopup(Message& m)
at System.Windows.Forms.Form.WndProc(Message& m)
at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,
Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DefFrameProc(IntPtr
hWnd, IntPtr hWndClient, Int32 msg, IntPtr wParam, IntPtr lParam)
at System.Windows.Forms.Form.DefWndProc(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.ContainerControl.WndProc(Message& m)
at System.Windows.Forms.Form.WmSysCommand(Message& m)
at System.Windows.Forms.Form.WndProc(Message& m)
at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,
Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DefFrameProc(IntPtr
hWnd, IntPtr hWndClient, Int32 msg, IntPtr wParam, IntPtr lParam)
at System.Windows.Forms.Form.DefWndProc(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.ContainerControl.WndProc(Message& m)
at System.Windows.Forms.Form.WmNcButtonDown(Message& m)
at System.Windows.Forms.Form.WndProc(Message& m)
at System.Windows.Forms.ControlNativeWindow.OnMeThe program '[4084]
EZDaqPC.exe' has exited with code 0 (0x0).
ssage(Message& m)
at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,
Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG&
msg)
at
System.Windows.Forms.ComponentManager.System.Windows.Forms.UnsafeNativeMethods+IMsoComponentManager.FPushMessageLoop(Int32
dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.ThreadContext.RunMessageLoopInner(Int32
reason, ApplicationContext context)
at System.Windows.Forms.ThreadContext.RunMessageLoop(Int32 reason,
ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at EZAutomation.Applications.EZDaqPC.MainWindow.Main() in
c:\documents and settings\mrahim\desktop\temp folder\ezdaq 1.2\ezdaq
1.2.3\ezdaqpc\windows\mainwindow.cs:line 301

Any help would be appreciated. Thanks in advance.
 
B

Barry Kelly

RahimAsif said:
I am writing an application that requires the a portion of the main
menu to be dynamic. The menu has file, panels, view files and help
across the top. The view files sub menu needs to be dynamically
generated, and the dynamic generation needs to occur right when the
user selects this menu item (that is on the Popup event handler).

However, everytime I put following code on the Popup event handler (of
the View Files menuitem) to dynamically generate the menu, I get
exceptions:

MenuItem x = new MenuItem("Item 1");
MenuItem y = new MenuItem("Item 2");
this.MenuItemViewFiles.MenuItems.Add(x);
this.MenuItemViewFiles.MenuItems.Add(y);

Note that if I put the code somewhere else, such as when the window
loads, the same code works perfectly fine.

How is your code semantically different to this:

---8<---
using System;
using System.Text;
using System.Windows.Forms;

class App
{
static void Main()
{
Form form = new Form();
form.Menu = new MainMenu();
MenuItem viewMenu = form.Menu.MenuItems.Add("View");
viewMenu.MenuItems.Add("First");
viewMenu.Popup += delegate
{
viewMenu.MenuItems.Add("New Item");
};
Application.Run(form);
}
}
--->8---

This successfully adds items to the menu in the Popup event.
Here's the exception that I get:

at System.Windows.Forms.MenuItem.get_MdiList()

This is the stack trace. You haven't included the error message and the
exception type.

-- Barry
 
R

RahimAsif

Functionally it looks the same, but mine always gives the exception
(error). Also the error is generated not at the point where I create
the dynamic menu but in the call to Application.Run

Here is the error message:

An unhandled exception of type 'System.NullReferenceException' occurred
in system.windows.forms.dll

Additional information: Object reference not set to an instance of an
object
 
B

Barry Kelly

RahimAsif said:
Functionally it looks the same, but mine always gives the exception
(error). Also the error is generated not at the point where I create
the dynamic menu but in the call to Application.Run

Here is the error message:

An unhandled exception of type 'System.NullReferenceException' occurred
in system.windows.forms.dll

Additional information: Object reference not set to an instance of an
object

Can you create a small, complete program which reproduces the problem?

-- Barry
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top