-2

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.

Mehran
  • 1,409
  • 2
  • 17
  • 27
Michael
  • 41
  • 1
  • 1
  • 7

1 Answers1

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?

Greg
  • 11,302
  • 2
  • 48
  • 79
  • Thanks for the response. If I have a treeView containing 4 different levels I would like to display 4 different editing screens preferrably as forms to the right of the treeView. I was accomplishing this with a tabControl. If level 2 was selected on my treeView, the 2nd tab in the tabControl was displayed. The problem with this solution is that I had to prevent the user from selecting a tab and that undermines how a user views a tab control. Instead I would like to display 4 forms and bring the appropriate form to the front based on the level selected in the treeView. – Michael May 16 '13 at 13:25
  • @Michael The sub area of code I wrote would apply, you'll want to use a `FlowLayoutPanel` but you may have to write the entire application on a single `Form` without a `MdiChild`. As the process for docking to it will not truly occur, it will dock to the parent. It is possible, but it will take a lot of trial and error. Just docking a component through code to a child form can be a quest on it's own. Hope that helps though. If I have a few more ideas I'll post for you. – Greg May 16 '13 at 15:44