Adding a Form to an MDI Child

In Form1

I include IsMdiContainer

and I added MenuStrip

. In Form1_Load

I am "new" Form2

and I accept Form2.MdiParent

before this

, which is Form1

. I am also maximizing Form2

and this operation works well.

In Form2

I have treeView

on the left side of the form, and on the right side of the form I would like to display several different forms with different editing capabilities that will depend on the node or level selected in treeView

.

I would like to create several different forms for data editing, which will be displayed Form2

depending on the selection from treeView

. I am unable to add a form to MdiChild and I have seen several posts where adding a form to a form can create some programming problems that I am not sure about.

I don't really have the code to embed in this post because nothing seemed to work except for the Mdi Parent and Child relationship, which was pretty simple.

Thanks in advance for your help.

-2


source to share


1 answer


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

  • Open Visual Studio
  • Creating a Windows Form Application
  • Click on your form
  • Go to "Properties for this form"
  • Minimum size: 1366 pixels by 768 pixels.
  • Maximized launch
  • The important element is IsMdiContainer
  • Open the toolbox.
  • Go to the menu
  • Drag FileMenu onto your form.
  • Create your menu
  • Then go to Solution Explorer
  • Right click the Add item
  • Add another shape
  • I left mine as Form2 (in a real program, not in a good name).

So, during these fifteen steps, we have everything we need to achieve our goal. So what will we do to complete our task:

  • Let's go back to our first form
  • Go to our FileMenu
  • Double click the menu button you want to link.

It will load the code view, inside the scope put this:

Form2 newFrm = new Form2();
newFrm.MdiParent = this;
newFrm.Show();

      

What this code does is three different things:



  • Line 1: It actually calls our object, in this case the second form. In fact, we are building our facility for us.

  • Line 2: This actually links our second form to our current form, this physically turns our second form into a child form.

  • Line 3: Actually the second form is physically rendered when the button is clicked.

That's all you need to physically show your form.

As for your second question, I'm not entirely sure what you are trying to accomplish. It looks like you are trying to create a tree, and then by selecting Node, the right side of the form changes in a specific context.

Now this is not 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 this is what you are looking for. Obviously you want to implement FlowLayoutPanel to make positioning not a pain for you. Be aware that an MDI parent with a child form acting as the MDI parent won't work very well. Since most things will default to Mocking Parent Forms Docking / Positioning. This example is not very good, but I am not entirely sure about your queries.

Are you trying to dock other shapes or components in the same shape?

+1


source







All Articles