C#Windows窗体MenuStrip - 如何在表单上交换菜单或菜单部分? [英] C# Windows Forms MenuStrip - how can I swap out a menu or section of a menu on a form?

查看:108
本文介绍了C#Windows窗体MenuStrip - 如何在表单上交换菜单或菜单部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个C#Windows窗体应用程序,我想根据用户选择在代码中动态交换menustrip。或者至少是菜单的一部分。模块化FILE,ZOOM,ACTIONS等菜单部分并能够动态构建菜单会很不错。如果我有一个包含10个场景的游戏,并且在菜单的操作部分中每个都有不同的项目,我可以将该代码替换为另一个代码块吗?我还想将这些菜单移动到一个单独的代码文件中,这样它们就不会混淆Forms主代码部分。有什么建议?我对此很陌生....

解决方案

将菜单移动到另一个文件是微不足道的:您的表单被声明为

  public   partial   class  MyFormName:Form 

所以你只需要为同一个类添加更多同样生成部分类代码的文件 - 它是非常标准的大班以这种方式打破它。



我可以在你的菜单上提出一个你可能没想过的建议吗?

为第一个场景创建一个菜单,并使其完成该场景。然后将它的可见属性设置为false。

对其他九个执行相同操作。当你运行你的代码时,它们都不会出现。

所以你要做的就是设置相应的菜单项Visible = true,它只会显示给用户。任何常见项目都有不同的名称,但文本相同,并且连线到相同的事件处理程序方法。 Simples!


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....

解决方案

Moving the menus to a different file is trivial: your form is declared as

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!


这篇关于C#Windows窗体MenuStrip - 如何在表单上交换菜单或菜单部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆