Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Given a MainMenu [mainMenu1] which has a MenuItem [mo] with another MenuItem [mi] as the child of [mo].

 

At run-time [dynamically] I want to add menuitems to [mi] which would have check marks beside them. These sub menu items of [mi] represents each option, if checked it is viewed, else it is hidden.

 

Concept: Incase my description is unclear, I want to mimic something like [in Visual Studio�s .Net]: File -> New [see it gives a list of options inside the �New� menuItem which is inside the �File� menuItem.

 

:

foreach ..

{

MenuItem mnu = new MenuItem();

mnu.Text = OptionName;

mnu.Checked = true;

mi.MenuItems.Add(mnu);

}

 

However this does not work, I get the following error: An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in system.windows.forms.dll

Additional information: Specified argument was out of the range of valid values.

 

Is this because I am adding a MenuItem to a MenuItem in a MainMenu?

Posted

Acctually decided not to use the foreach, but here is the exact code.

 

MenuItem mnu = new MenuItem();

int iMachines = RobotNumber;

for (int iMachineIndex = 0; iMachineIndex < iMachines; iMachineIndex++)

{

mnu.Text = getName(iMachineIndex);

mnu.Checked = true;

miSelStat.MenuItems.Add(mnu);

}

 

Where The Menu would have FILE -> miSelStat -> the machine Names [created dynamically]

 

Happy New Year!

Posted

I think the error is here:

MenuItem mnu = new MenuItem();

You're creating the MenuItem once but you're changing it's values and adding it again, you can't "reuse" the MenuItem because you'll continuously change all the previous items anyway.

 

Try this code instead:

MenuItem mnu;
int iMachines = RobotNumber;
for (int iMachineIndex = 0; iMachineIndex < iMachines; iMachineIndex++)
{
mnu = new MenuItem();
mnu.Text = getName(iMachineIndex);
mnu.Checked = true;
miSelStat.MenuItems.Add(mnu);
}

.Net allows software to be written for any version of Windows and not break like Unmanaged applications unless using Unmanaged procedures like APIs. If your program uses large amounts of memory but releases it when something else needs it, then what's the problem?

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...