473,394 Members | 1,726 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,394 software developers and data experts.

MDI Application MenuItems.Add Throws exception

Create a simple MDI form and a child form, and try the following simple code
to add a new menu item on the Edit menu popup:

private void editMenu_Popup(object sender, System.EventArgs e)
{
editMoreMenu.MenuItems.Add("New MI " + count);
count++;
}

and get "An unhandled exception of type 'System.NullReferenceException'
occurred in system.windows.forms.dll" and "
Additional information: Object reference not set to an instance of an object."

Is this a known issue in .NET framework? Any thought is highly appreciated.

Full source code for MDI Form:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace WindowsApplication2
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.MainMenu mainMenu1;
private System.Windows.Forms.MenuItem fileMenu;
private System.Windows.Forms.MenuItem fileNewMI;
private System.Windows.Forms.MenuItem editMenu;
private System.Windows.Forms.MenuItem editMoreMenu;
private System.Windows.Forms.MenuItem menuItem2;
private System.Windows.Forms.MenuItem menuItem3;
private System.Windows.Forms.MenuItem menuItem4;
private System.Windows.Forms.MenuItem menuItem5;
private System.Windows.Forms.MenuItem menuItem6;

int count = 0;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.fileMenu = new System.Windows.Forms.MenuItem();
this.fileNewMI = new System.Windows.Forms.MenuItem();
this.editMenu = new System.Windows.Forms.MenuItem();
this.menuItem4 = new System.Windows.Forms.MenuItem();
this.menuItem5 = new System.Windows.Forms.MenuItem();
this.editMoreMenu = new System.Windows.Forms.MenuItem();
this.menuItem2 = new System.Windows.Forms.MenuItem();
this.menuItem6 = new System.Windows.Forms.MenuItem();
this.menuItem3 = new System.Windows.Forms.MenuItem();
//
// mainMenu1
//
this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.fileMenu,
this.editMenu,
this.menuItem3});
//
// fileMenu
//
this.fileMenu.Index = 0;
this.fileMenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.fileNewMI});
this.fileMenu.MergeType = System.Windows.Forms.MenuMerge.MergeItems;
this.fileMenu.Text = "File";
//
// fileNewMI
//
this.fileNewMI.Index = 0;
this.fileNewMI.Text = "New";
this.fileNewMI.Click += new System.EventHandler(this.fileNewMI_Click);
//
// editMenu
//
this.editMenu.Index = 1;
this.editMenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem4,
this.menuItem5,
this.editMoreMenu});
this.editMenu.Text = "Edit";
this.editMenu.Popup += new System.EventHandler(this.editMenu_Popup);
//
// menuItem4
//
this.menuItem4.Index = 0;
this.menuItem4.Text = "Cut";
//
// menuItem5
//
this.menuItem5.Index = 1;
this.menuItem5.Text = "Copy";
//
// editMoreMenu
//
this.editMoreMenu.Index = 2;
this.editMoreMenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem2,
this.menuItem6});
this.editMoreMenu.Text = "More...";
//
// menuItem2
//
this.menuItem2.Index = 0;
this.menuItem2.Text = "Existing MI 1";
//
// menuItem6
//
this.menuItem6.Index = 1;
this.menuItem6.Text = "Existing MI 2";
//
// menuItem3
//
this.menuItem3.Index = 2;
this.menuItem3.Text = "Help";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(600, 506);
this.IsMdiContainer = true;
this.Menu = this.mainMenu1;
this.Name = "Form1";
this.Text = "MdiForm";

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void editMenu_Popup(object sender, System.EventArgs e)
{
editMoreMenu.MenuItems.Add("New MI " + count);
count++;
}

private void fileNewMI_Click(object sender, System.EventArgs e)
{
FormChild c = new FormChild();
c.MdiParent = this;
c.Show();
}
}
}
Full trivial source code for the child form:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace WindowsApplication2
{
/// <summary>
/// Summary description for Form2.
/// </summary>
public class FormChild : System.Windows.Forms.Form
{
private System.Windows.Forms.MainMenu mainMenu1;
private System.Windows.Forms.MenuItem fileMenu;
private System.Windows.Forms.MenuItem filePreviewMI;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public FormChild()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.fileMenu = new System.Windows.Forms.MenuItem();
this.filePreviewMI = new System.Windows.Forms.MenuItem();
//
// mainMenu1
//
this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.fileMenu});
//
// fileMenu
//
this.fileMenu.Index = 0;
this.fileMenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.filePreviewMI});
this.fileMenu.MergeType = System.Windows.Forms.MenuMerge.MergeItems;
this.fileMenu.Text = "File";
//
// filePreviewMI
//
this.filePreviewMI.Index = 0;
this.filePreviewMI.Text = "Preview";
//
// FormChild
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(416, 394);
this.Menu = this.mainMenu1;
this.Name = "FormChild";
this.Text = "Document";

}
#endregion
}
}

Nov 16 '05 #1
0 1609

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Dan H | last post by:
I'm trying to add menu items dynamically in the Popup event of a MenuItem in an MDI child form. Actually, I'm using the Genghis MRU class, but have also dumbed it down to a simple test case in a...
7
by: Job Lot | last post by:
How can I terminate console application in Try…Catch…Finally…End Try block so that code in Finally gets executed. If I use End statement Finally does not get executed. Following is my code...
0
by: George Durzi | last post by:
I'm trying to extend the ExceptionManager Application Block and build a custom publisher that writes my exceptions to the database. That's easy part. The difficulty I am having is in configuring my...
2
by: nologin | last post by:
Hello. I have an application which has the use of another assembly. How can i make a deployment project, which will be copy this assembly not to application folder but system folder - depends on...
0
by: kk | last post by:
hello all, I want to call an exe(IISWeb.vbs) to create a site in Sharepoint.While calling that exe through Process.Start() it throws the following exception. 1.while calling through...
2
by: Michael Kalika | last post by:
Hi, We have developed a VSTO 2005 Excel application and we would like to leverage ClickOnce deployment mechanism for distribution of this application. How can we do that? I was digging in MSDN...
3
by: MWS | last post by:
Hello, I'm trying to loop through the app.Config file and add the keys from the app.Config as MenuItems on a MenuStrip. The following line blows up... ...
3
by: unleashsiva | last post by:
Hi, I developed an VB.Net application using Microsoft Visual Studio .NET 2003. I created an msi for that application. After installing the msi package, the application runs perfectly with Admin...
1
by: CodeSeeker | last post by:
I have an application, which uses pop3 to read the messages from the mailbox, and it has been working fine for so many year. We recently have started changing this application to use java mail IMAP 4...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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...

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.