Click here to Skip to main content
13,202,155 members (52,837 online)
Click here to Skip to main content
Add your own
alternative version


14 bookmarked
Posted 17 Jan 2008

A C# MDI Starter Sample

, 17 Jan 2008
Rate this:
Please Sign up or sign in to vote.
A simple MDI (Multiple Document Interface) example.



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.


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";
            if (childForm.Text.Contains("Focused"))
                childForm.Text = childForm.Text.Remove(

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 = 
    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)

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.


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


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

United States United States
Currently I work on industrial machinery as a mechanic. I have a few other things on my resume before that are mostly computer related. I have been writing programs in various languages for about 10 years. The single most complex thing I have ever encountered as a programmer, an engineering student, a Christian, a husband, a soldier, and father is, Jesus The Christ, and the book called The Bible. It is like a masterful fractal, but like a graphical representation of a fractal you do not have to understand the math to see how simple, beautiful, and complex it is. Yes, the physical bible is a bunch of words on paper. However, to understand what The Bible says is a scary thing. The world you now live in becomes like a movie studio set, of plywood cutouts. And to look behind the cutout and understand that things are not what they seem is a real mind bender. I could keep going, but the Bible is the smallest most efficient way to communicate the information inside. Note: It is easy to read The Bible and not understand what it means. It is impossible to understand The Bible unless God allows you to, I am not being impetuous, but until you find your life the things you say and do, today, in that book. You may close your eyes and think you know what it means to be blind, but if you have ever been blind you don't 'think you know' you understand.

You may also be interested in...

Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.171020.1 | Last Updated 17 Jan 2008
Article Copyright 2008 by LowlyServ-Ant
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid