C# .NET (WinForm) - MainForm 分为菜单(左)和内容(右,子窗体)
已经很长时间没有在winform上工作了(现在我是一名asp.net开发人员),但是这次我需要一个应用程序来使用自己,所以我今天早上开始工作,问题就出现了(随着每个项目的开始......哈哈)。
我正在考虑有一个 MainForm,将其分为两侧:左侧(大约屏幕宽度的 20%)将包含一个 MENU(菜单条?),其余部分将是“子窗体”的一侧'。
这个想法是,单击菜单的一个元素(记住,主窗体的左侧),它将创建并实例化一个窗体,并将其显示在内容侧(右侧)。
内容侧(右侧)同时只有一个窗体,但我可能需要 ShowDialog 属性来创建一个新窗体,与主窗体分开。
问题:
我不知道应该在主窗体中使用哪个控件,在主窗体的右侧放置一个“contentplaceholder”(类似 asp.net),因此我可以通过单击动态加载/卸载窗体在菜单的元素中。
我知道如何打开一个新对话框(form.ShowDialog等...),但我不记得这是否可能。
Have been long time without workin on winform's (now I'm an asp.net developer), but these time I need an application to use myself, so I start working this morning and problems have appeared (as every project start...lol).
I was thinking on have a MainForm, dividing it on two sides: left side (about 20% of the width of the screen) wich will contain a MENU (menustrip?), and the rest, will be the side of the 'child form'.
The idea, is that clicking on one of the elements of the menu (remember, left side of the mainform), it will create and instance a form, and SHOW it in the content side (right side).
There will be ONLY one form at the same time on the content side (right), but I probably will need the ShowDialog property to create a new one, separately of the main form.
The problem:
I don't know which control should I use, in the mainform, to place a 'contentplaceholder' (asp.net like), on the right side of the mainform, so I can dynamically load/unload the form by clicking in the elements of the menu.
I know how to open a new dialog (form.ShowDialog, etc...), but I don't remember if that's possible or not.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是一种非常常见的用户界面模型。首先在表单上放置一个 SplitContainer,它为您提供两个面板和一个可以调整的分隔线。将 TreeView 拖放到左侧面板上,以提供导航。将其 Dock 属性设置为 Fill。向其中添加一些节点,它们将成为“菜单项”。您可以使用图标和嵌套节点使其任意美观。
您可以通过为树视图的 AfterSelect 事件实现事件处理程序来响应用户所做的选择。使用节点的 Tag 或 Text 属性识别单击了哪个节点。像这样:
showScreen() 方法需要用新控件替换右侧面板中显示的任何控件。您可以支持表单以及用户控件。两者在 Winforms 设计器中都表现良好,让您可以专注于它们的外观。就像这样:
这就是全部,只需关注这里的实际“屏幕”即可。
This is a very common user interface model. You start by dropping a SplitContainer on the form, that gives you two panels and a divider that can be adjusted. Drop a TreeView on the left panel, that provides the navigation. Set its Dock property to Fill. Add some nodes to it, they'll be the 'menu items'. You can make it arbitrarily fancy with icons and nested nodes.
You respond to selections made by the user by implementing an event handler for the tree view's AfterSelect event. Recognize what node was clicked by using the node's Tag or Text property. Like this:
The showScreen() method needs to replace whatever control is shown in the right panel with the new one. You can support forms as well as user controls. Both behave nicely in the Winforms designer, allowing you to focus on their appearance. Like this:
That's all there is to it, just focus on the actual 'screens' from here.
你的英语其实很好。您可能想要做的不是让每个菜单项都显示一个新的
Form
,而是让它创建一个新的Control
。由于您只想一次打开一个,因此您可以在主窗口的右侧添加一个面板,并在该面板中进出简单的交换控件。因此,当单击MenuItem1
时:Your English is actually quite good. What you may want to do is instead of having each menu item bring up a new
Form
, actually have it create a newControl
. Since you only want one open at a time, you can add a panel on the right side of the main window and simple swap controls in and out of that panel. So when aMenuItem1
is clicked:我同意汉斯关于一切的观点,除了我想补充一点,因为您想使用将放置在主窗体上的窗体而不是控件,您可能需要这样的东西:
它将有效地允许您使用您的占位符并放置基于表单的窗口而不是它!
I agree with Hans there regarding everything, except I'd like to add that as you would like to use FORMS that will be placed on the main form not CONTROLS, you might have a need for something like this:
It will effectively allow you to use your placeholder and put Form based window instead of it!