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

Dynamic MenuItems *weird* problem

P: n/a


Hi. I'M having a weird problem with addign menuitems to a menu dynamically
on the popup event of the menuitem containing the container of the
menuitems i wanna add (tools -> external -> {list of items dynamically
added})

All the code works #1 when only the MdiParent form is used, as soon as i
open an MdiChild in it, right after the popup event returns, i get this
error:

================================================== =======================
A first chance exception of type 'System.NullReferenceException' occurred
in system.windows.forms.dll

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

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.
Unhandled Exception: System.NullReferenceException: Object reference not
set to an instance of an object.
at System.Windows.Forms.MenuItem.UpdateMenuItemIfDirt y()
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.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.DispatchM essageW(MSG& msg)
at
System.Windows.Forms.ComponentManager.System.Windo ws.Forms.UnsafeNativeMeth
ods+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 MoHScriptEdit.frmMain.Main() in d:\my documents\visual studio
projects\medal of honor editor\frmmain.cs:line 518The program '[340]
MoHScriptEdit.exe' has exited with code 0 (0x0).

================================================== =======================

Here is the code i use to add the items, i repeat, it works fine when only
the MdiParent is opened. And the MdiChild DON'T have a "Tools" menu.

private void mnuTools_Popup(object sender, EventArgs e)
{
Debug.WriteLine("Enter frmMain.mnuTools_Popup()");
PopulateExternalToolsMenu();
Debug.WriteLine("Exit frmMain.mnuTools_Popup()");
}

private void PopulateExternalToolsMenu()
{
Debug.WriteLine("Enter frmMain.PopulateExternalToolsMenu()");
mnuExternalTools.MenuItems.Clear();

if(settings.Tools.Count > 0)
{
Debug.WriteLine("Tools Count:" + settings.Tools.Count);

foreach(ExternalTool tool in settings.Tools)
{
Debug.WriteLine("Tool:" + tool.toolName);
MenuItem mi = new MenuItem();
mi.Text = tool.toolName;

mnuExternalTools.MenuItems.Add(mi);

Debug.WriteLine("\t -> Added. Count is now " +
mnuExternalTools.MenuItems.Count);
}
}
Debug.WriteLine("Exit frmMain.PopulateExternalToolsMenu()");
}

I'm really lost, it's very basic simple code and even using step by step i
can't find what generates the error. I also tried adding items directly
through mnuExternalTools.MenuItems.Add("Item Text") but it gives the same
error.

Any clue on what it can be?

--
Deutsche Dogge
www.doggesoft.com
Jul 21 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.