In Form1
I'm enabling IsMdiContainer
and I added a MenuStrip
. In Form1_Load
I "new" Form2
and I'm assiging Form2.MdiParent
to this
which is Form1
. I'm also maximizing Form2
and this operation works well.
In Form2
I have a treeView
on the left side of the form and on the right side of the form I would like to display a number of different forms with various editing capabilities which will be dependent upon the node or level selected in the treeView
.
I would like to create a number of different forms for editing data that would be displayed in Form2
depending on the selection from the treeView
. I can't seem to add a form to the MdiChild and I've been seeing some posts where adding a form to a form may create some programming problems which I'm not sure about.
I really don't have any code to paste into this post because nothing seemed to work except for the Mdi Parent and Child relationship which was pretty simple.
Thanks in advance for any help.
There is a lot of information on this subject, but some documentation can be difficult to understand for some new developers. Follow these steps:
- Open Visual Studio
- Create a Windows Form Application
- Click your Form
- Go to Properties for that Form
- Minimum Size : 1366 pixels by 768 pixels.
- Launch Maximized
- The important element is IsMdiContainer
- Open your Toolbox.
- Go to Menus
- Drag FileMenu onto your Form
- Build your Menu
- Then go to Solution Explorer
- Right-Click Add Item
- Add another Form
- I left mine as Form2 (In a real program, not a good name).
So within those fifteen steps, we have all that we need to accomplish our goal. So what we will do to finish our task is:
- Go back to our First Form
- Go to our FileMenu
- Double Click on the menu button you wish to link.
It will load a code view, inside the area put this:
Form2 newFrm = new Form2();
newFrm.MdiParent = this;
newFrm.Show();
What this code is doing is three distinct things:
Line 1: It is actually calling our object, in this case a second form. It is actually building our object for us.
Line 2: Is actually linking our second form to our current form, this is physically turning our second form into a Child Form.
Line 3: This is actually physically showing our second form when the button is clicked.
That is all you need to physically show a Form.
In regards to your second question, I'm not entirely sure what your attempting to accomplish. It sounds like your trying to have a tree, then as a Node is selected the right hand side of the Form changes to specific context.
Now this isn't the nicest example, but do you mean something like this?
TreeNode node = treeView1.SelectedNode;
if (node.Text.Contains("XP"))
{
TextBox one = new TextBox();
Panel i = new Panel();
i.Dock = DockStyle.Right;
i.BackColor = Color.Black;
i.Controls.Add(one);
i.Show();
TreeFrm.ActiveForm.Controls.Add(i);
}
Not sure if that is what you are seeking. Obviously you'd want to implement a FlowLayoutPanel to make the positioning not a pain for you. Keep in mind an MDI Parent, with a Child Form acting as a MDI Parent will not work very well. As most things will default to MDI Parent Forms Docking / Positioning. This example is not pretty, but I'm not entirely sure of what your asking.
Are you trying to dock other forms or components on the same form?
来源:https://stackoverflow.com/questions/16575052/add-a-form-to-a-mdi-child