C# .NET (WinForm) - MainForm divided on Menu (left) and Content (Right, child form)
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 开发者_StackOverflowremember if that's possible or not.
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:
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e) {
switch (e.Node.Text) {
case "Home":
showScreen(new UserControl1());
break;
case "View":
showScreen(new Form2());
break;
// etc...
}
}
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:
private void showScreen(Control ctl) {
while (splitContainer1.Panel2.Controls.Count > 0)
splitContainer1.Panel2.Controls[0].Dispose();
// Support forms too:
if (ctl is Form) {
var frm = ctl as Form;
frm.TopLevel = false;
frm.FormBorderStyle = FormBorderStyle.None;
frm.Visible = true;
}
ctl.Dock = DockStyle.Fill;
splitContainer1.Panel2.Controls.Add(ctl);
}
That's all there is to it, just focus on the actual 'screens' from here.
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 new Control
. 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 a MenuItem1
is clicked:
MyControl1 newControl= new MyControl1();
newControl.Dock = DockStyle.Fill;
placeHolderPanel.Controls.Clear();
placeHolderPanel.Controls.Add(newControl);
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:
static public void ReplaceControl(Control ToReplace, Form ReplaceWith) {
ReplaceWith.TopLevel=false;
ReplaceWith.FormBorderStyle=FormBorderStyle.None;
ReplaceWith.Show();
ReplaceWith.Anchor=ToReplace.Anchor;
ReplaceWith.Dock=ToReplace.Dock;
ReplaceWith.Font=ToReplace.Font;
ReplaceWith.Size=ToReplace.Size;
ReplaceWith.Location=ToReplace.Location;
ToReplace.Parent.Controls.Add(ReplaceWith);
ToReplace.Visible=false;
}
It will effectively allow you to use your placeholder and put Form based window instead of it!
精彩评论