Add a form to a MDI child

笑着哭i 提交于 2019-12-02 13:26:20

There is a lot of information on this subject, but some documentation can be difficult to understand for some new developers. Follow these steps:

  1. Open Visual Studio
  2. Create a Windows Form Application
  3. Click your Form
  4. Go to Properties for that Form
  5. Minimum Size : 1366 pixels by 768 pixels.
  6. Launch Maximized
  7. The important element is IsMdiContainer
  8. Open your Toolbox.
  9. Go to Menus
  10. Drag FileMenu onto your Form
  11. Build your Menu
  12. Then go to Solution Explorer
  13. Right-Click Add Item
  14. Add another Form
  15. 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:

  1. Go back to our First Form
  2. Go to our FileMenu
  3. 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?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!