473,396 Members | 1,768 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Dynamic Menus in C# and Popup event handler

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
3 4750
"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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: cefrancke | last post by:
The only reason I ask is that no one has made this subject clear or given a definitive answer. What I would like to do is, after turning off all the menus/tbars/etc using the startup options. ...
1
by: Earl Teigrob | last post by:
I did a ton of searching to try and find a simple solution to this issue and finally wrote my own, which I am sharing with everyone. In my searching, I did find a very complete and robust solution at...
0
by: pbb | last post by:
I have a web page on which I dynamically create controls based on the selection a user makes from a dropdownlist (this ddl is not dynamic). Depending on the user's selection, the controls could be...
2
by: Kiyomi | last post by:
Hello, I have some codes under event ButtonSend_Click to check the user input values. This check is complicated enough using different stored procedures. Then according the result of the...
2
by: Ron M. Newman | last post by:
Hi, Just need a little advice. Id like to build *dynamic* context menus for tree nodes. I'm pretty versed in building context menus and attaching them to tree nodes. My question is, what...
4
by: jobs | last post by:
the javascript: function ShowTooltip(L1in) { document.getElementById("L1").innerText=L1in; y = event.clientY + document.documentElement.scrollTop; var Popup= document.getElementById("Popup")...
8
by: tonFrere | last post by:
Hello all, I did a lot of research on the subject and came to the conclusion that what I want to do might not be "good practice". I created a set of table to manage user rights to forms in my...
3
by: =?Utf-8?B?ZWFndWlsYXI=?= | last post by:
Hi, I am trying to dynamically generate a menu, based on entries on a text or xml file. The text file contains the "tree" after which the menu will need to be created. Something like the...
6
by: SAL | last post by:
hello, I'm using a radiobuttonlist in an updatepanel in an item template in a Gridview control. I'm populating the radiobuttonlist in the RowDataBound event. I have the control toolkit registered...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.