Add a form to a MDI child

廉价感情. 提交于 2019-12-04 07:08:42

问题


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.


回答1:


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?



来源:https://stackoverflow.com/questions/16575052/add-a-form-to-a-mdi-child

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