Click here to Skip to main content
15,911,896 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I've got a C# Windows Forms application that I want to swap out the menustrip dynamically in the code based on a users selection. Or at least a section of the menu. It would be nice to modularize the menu sections like FILE, ZOOM, ACTIONS, etc. and be able to build a menu dynamically. If I have a game with 10 scenarios and each has different items in the ACTIONS section of the menu, can I just swap out that code with another code block? I'd also like to move these menus to a separate code file so they are not cluttering up the Forms main code section. Any suggestions? I'm kind of new to this....
Posted

1 solution

Moving the menus to a different file is trivial: your form is declared as
C#
public partial class MyFormName : Form
So you just add more files that also generate partial class code for the same class - it's pretty standard for large classes to break it up in this way.

Can I make a suggestion on your menus, that you may not have thought about?
Create a menu for the first scenario, and make it complete for that scenario. Then set it's Visible property to false.
Do the same for the other nine. When you run your code, none of them will appear.
So all you have to do is set the appropriate Menu item Visible = true, and it and only it will appear to the user. Any common items have different names, but the same text, and are "wired" to teh same event handler method. Simples!
 
Share this answer
 
Comments
Member 10040991 16-May-13 14:57pm    
Yes, I thought about that - dragging 10 menustrips onto the form and making them invisible until needed, but wasn't sure that was the best way to do it. I forgot about the form being a partial class, I'll see if I can get that to do what I want. Thanks
OriginalGriff 16-May-13 15:44pm    
You're welcome!
The alternative is to remove them from the parent menu collection, and insert them in the right place again, but that's a nuisance as it means your code needs to know where in the UI the menu should go.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900