Shaitan00 Posted December 31, 2003 Posted December 31, 2003 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? Quote
Administrators PlausiblyDamp Posted December 31, 2003 Administrators Posted December 31, 2003 Which line do you get the error on? Could you post a bit more of the code like the foreach bit. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Shaitan00 Posted December 31, 2003 Author Posted December 31, 2003 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! Quote
AndreRyan Posted January 1, 2004 Posted January 1, 2004 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); } Quote .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?
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.