By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,053 Members | 1,595 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,053 IT Pros & Developers. It's quick & easy.

Dynamic Menus in C# and Popup event handler

P: n/a
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(Even tArgs e)
at System.Windows.Forms.MenuItem._OnInitMenuPopup(Eve ntArgs e)
at System.Windows.Forms.Menu.ProcessInitMenuPopup(Int Ptr 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(M essage& m)
at System.Windows.Forms.NativeWindow.DebuggableCallba ck(IntPtr hWnd,
Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DefFrameP roc(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(Mes sage& m)
at System.Windows.Forms.ContainerControl.WndProc(Mess age& 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(M essage& m)
at System.Windows.Forms.NativeWindow.DebuggableCallba ck(IntPtr hWnd,
Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DefFrameP roc(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(Mes sage& m)
at System.Windows.Forms.ContainerControl.WndProc(Mess age& 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(M essage& m)
at System.Windows.Forms.NativeWindow.DebuggableCallba ck(IntPtr hWnd,
Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchM essageW(MSG&
msg)
at
System.Windows.Forms.ComponentManager.System.Windo ws.Forms.UnsafeNativeMethods+IMsoComponentManager. FPushMessageLoop(Int32
dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.ThreadContext.RunMessageLoopI nner(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.

Jul 11 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"RahimAsif" <Ra*******@gmail.comwrote:
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

--
http://barrkel.blogspot.com/
Jul 11 '06 #2

P: n/a
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

Barry Kelly wrote:
"RahimAsif" <Ra*******@gmail.comwrote:
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

--
http://barrkel.blogspot.com/
Jul 11 '06 #3

P: n/a
"RahimAsif" <Ra*******@gmail.comwrote:
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

--
http://barrkel.blogspot.com/
Jul 11 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.