Child MDI forms for dummies

eightbits

Newcomer
Joined
Mar 8, 2003
Messages
1
I am a newbie to vs.net and I am trying to create a child MDI form. Now, I use this code to open a new child form:

C#:
private void menuItem2_Click(object sender, System.EventArgs e)
		{
			Form2 newMDIChild = new Form2();
			newMDIChild.MDIParent = this;
			newMDIChild.Show();
		}
This is taken almost character for character from theVS.net help.
I have seen a million examples of this exact method on the web.
But, when I try to Debug (F5), I get this error:

C:\...\Form1.cs(329): 'temp.Form2' does not contain a definition for 'MDIParent'

Any ideas? I generated the form by right clicking on the solution
in the Solution Explorer and doing Add > Windows Form > etc.

Within Form2, I have tried to manually set the MdiParent property
but I can't. I have tried this line of code:

this.MdiParent = Form1;

That fails for obvious reasons, but I was willing to try anything.

Hope you can help me out. I'm sure I'm missing something
obvious, but I followed the VS.net "tutorials" to the letter and it
just plain doesn't work and I can't figure it out! Thanks!

Here is the code for Form2 (in case it matters):
C#:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace temp
{
	/// <summary>
	/// Summary description for Form2.
	/// </summary>
	public class Form2 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.RichTextBox richTextBox1;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public Form2()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.richTextBox1 = new System.Windows.Forms.RichTextBox();
			this.SuspendLayout();
			// 
			// richTextBox1
			// 
			this.richTextBox1.Dock = System.Windows.Forms.DockStyle.Fill;
			this.richTextBox1.Name = "richTextBox1";
			this.richTextBox1.Size = new System.Drawing.Size(292, 273);
			this.richTextBox1.TabIndex = 0;
			this.richTextBox1.Text = "";
			this.richTextBox1.TextChanged += new System.EventHandler(this.richTextBox1_TextChanged);
			// 
			// Form2
			// 
			this.AccessibleName = "Form2";
			this.AccessibleRole = System.Windows.Forms.AccessibleRole.Window;
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(292, 273);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.richTextBox1});
			this.Name = "Form2";
			this.Text = "Form2";
			this.ResumeLayout(false);

		}
		#endregion

		private void richTextBox1_TextChanged(object sender, System.EventArgs e)
		{
		
		}
	}
}
 
Last edited by a moderator:
+ [Windows Form Designer generated code]

Visual Basic:
    Dim frm As New frmChild1()
'^^^^ put that line just below the windows form design line ( naming the child form obviously to the name you want )
'the next code below is one i found on this board a while back and is very good. make sure you set the parent form as ( is mdicontaner ) on the properties box

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If Not IsOpen(frm) Then
            frm.MdiParent = Me
            frm.Show()
        End If
    End Sub
    Private Function IsOpen(ByVal frm As Form) As Boolean
        Dim ret As Boolean, stdForm As Form
        ret = False

        For Each stdForm In Me.MdiChildren
            If stdForm.Name = frm.Name Then
                stdForm.Focus()
                ret = True
            End If
        Next

        Return ret
    End Function
 
Last edited by a moderator:
If you say that form1 does not contain mdi definition then check if you set isMdiContainer property of form1 to true.
 
Your code when creating the form should read as follows:

C#:
private void menuItem2_Click(object sender, System.EventArgs e)
        {
            Form2 newMDIChild = new Form2();
            newMDIChild.MdiParent = this;
            newMDIChild.Show();
        }

You were trying to refer to an "MDIParent" property. Remember that c# is case-sensitive.
 
Yes there is a problem with the C# example which also caused me major headaches. The line:

newMDIChild.MDIParent = this;

should actually read:

newMDIChild.MdiParent = this;
 
Last edited:
Back
Top