Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

A C# MDI Starter Sample

0.00/5 (No votes)
17 Jan 2008 1  
A simple MDI (Multiple Document Interface) example.

MDI001exe

Introduction

This is my first article, so please excuse the coarseness of it. The purpose of the program is to spring board the reader into (MDI) Multi Document Interfaces when they are accustomed to writing single document programs. Basically, this program just shows how to keep track of which form the user is working on so the "main menu" can operate correctly, which can also be extended to represent how the main form communicates with the child document forms.

Background

The quick rundown is I fired up VS2005, with an empty project, created an MDI form, and created a variable to keep track of the document the user is working on. I then used the MDI child Active event function to update my focus tracking variable. Don't worry if that last sentence was confusing. I have posted the code below, it is mostly straightforward and simple.

Using the Code

I would like to write some profound explanation, but the code below is pretty much what it is. focusChild is a private global form variable, which gets assigned when the focus changes child forms.

private void MDIParent1_MdiChildActivate(object sender, EventArgs e)
{
    //MessageBox.Show("something was activated");
    focusChild = null;
    foreach (Form childForm in this.MdiChildren)
    {                
        if (childForm.ContainsFocus)
        {
            focusChild = (txtForm)childForm;
            if (!childForm.Text.Contains("Focused"))
                childForm.Text += " Focused";
        }
        else
            if (childForm.Text.Contains("Focused"))
                childForm.Text = childForm.Text.Remove(
                    childForm.Text.IndexOf("Focused")-1);
    }
}

The only other thing worth mentioning is what happens when the user clicks on an Open/Save button from the main menu. Below is the Save event which uses the tracking variable and pulls from our custom form and saves it to a file.

private void SaveAsToolStripMenuItem_Click(object sender, EventArgs e)
{
    SaveFileDialog saveFileDialog = new SaveFileDialog();
    saveFileDialog.InitialDirectory = 
       Environment.GetFolderPath(Environment.SpecialFolder.Personal);
    saveFileDialog.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*";
    if (saveFileDialog.ShowDialog(this) == DialogResult.OK)
    {
        string FileName = saveFileDialog.FileName;
        // TODO: Add code here to save the current contents of the form to a file.
        if (focusChild != null)
        {
            using (StreamWriter sw = new StreamWriter(FileName))
            {
                foreach (string line in focusChild.richTextBox1.Lines)
                    sw.WriteLine(line);
                sw.Flush();
                sw.Close();
            }
        }
    }
}

Points of Interest

It is worth noting that I use the childForm.ContainsFocus property above for a reason. ContainsFocus will return true if the form or any of its components have focus. form.focus will only return true if the form itself has focus.

History

Just a down and dirty quick sample, no changes to date.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here