Click here to Skip to main content
12,500,748 members (46,584 online)
Click here to Skip to main content
Add your own
alternative version

Stats

18.1K views
841 downloads
14 bookmarked
Posted

A C# MDI Starter Sample

, 17 Jan 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

LowlyServ-Ant
Other
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...

Pro
Pro

Comments and Discussions

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