Multiple Document Interface (MDI) With RichTextBox

After searching for a long time on the internet, I have read and consolidated knowledge from different websites into one place and created an original MDI application. I hope this will be useful for all coders who want to create a Wordpad like application in C#.NET using Visual Studio.

What is a Multiple Document Interface?

A multiple document interface (MDI) is a graphical user interface in which multiple windows reside under a single parent window. Such systems often allow child windows to embed other windows inside them as well, creating complex nested hierarchies. This contrasts with the single document interfaces (SDI) where all windows are independent of each other.

Advantages of Multiple Document Interface

 

  • With MDI, a single menu bar and/or toolbar is shared between all child windows, reducing clutter and increasing efficient use of the screen space.
  • An application's child windows can be hidden/shown/minimized/maximized as a whole.

In this article, we will see how to create Multiple Document Interface in C#. We chose C# because it is one of the most popular programming languages. So, let’s start.

Creating the application

  • In Visual Studio, click on Start->New->Project.
  • Select Windows Forms Application from the available options.
  • Let’s give the application a name. I named it “MDIAppwithRichTextBox”.
  • Click on the “OK” button. A Windows.Forms application is created.
  • Right-click on the “Form1” in the design view and click on "Properties".
  • On the Properties window, scroll down to “Design” and change the “Name” property to “MDIContainer” from “Form1”.
 
Multi Document Interface With RichTextBox 

Now, in the Properties window, scroll down to “Window Style” section and change the “IsMdiContainer” property to “true”. The change of the client area of the form changes to grey after doing this. This property tells the program that the form can contain other forms.

Multi Document Interface With RichTextBox 

 

  • Now, let’s add another form to the project. For that, right-click on the Project in Solution Explorer and select "Add" from the context menu.
  • Click on “Windows Form”. A window will appear.
  • Let’s name the form at the bottom of this window to “MyForm”.
  • Click on the “Add” button. A new form will be added to your project. This form will act as our edit area and will be contained in the MDIContainer form. We will shortly see how.
  • First, let’s add a RichTextBox to “MyFrom” form. From the toolbox, let’s drag and drop a RichTextBox to this form and go to the properties of the Richtextbox.
  • Scroll down to “Layout” section and change the “Dock” property to “Fill”.

 

 
Multi Document Interface With RichTextBox 

You will see that RichTextBox fills the entire client area of the form.

  • Now, let’s go to the first form “MDIContainer” and add a “MenuStrip” from the Toolbox-> Menus & Toolbars to the form.
  • You will see that a new text box appears at the top of the form within a strip. Now let’s create the menu. In the first Box type “File”.
  • A textbox will appear below it. Let’s create four submenu items. “New File”, “Open File”, “Save” and “Exit”.
  • Now, let’s create another topmost menu besides file. On the right of “File”, you will see another textbox.
  • Type “Insert” there and below that let’s create two submenus-“Text” and “Image”.
  • Now, let’s create another topmost menu.
  • On the right of “Insert” menu option, type “Edit”.
  • Below “Edit”, let’s create 7 submenus: “Undo”, “Redo”, “Mark Text”, “ForeColor”, “Font”, “Current Color”, “Current Font”, ”Cut”, ”Copy”, and “Paste”.
  • At last, let’s create another Menu item -> “CloseActiveForm”.

 

Multi Document Interface With RichTextBox 

This ends our menu section.

Now, let’s double click on File->New File. This will create a new method in the code behind file.

  1. private void newToolStripMenuItem_Click(object sender, EventArgs e)  
  2. {  
  3. }  

In this method, let’s add the following code.

  1. private void newToolStripMenuItem_Click(object sender, EventArgs e)  
  2.         {  
  3.             MyFrom childform = new MyFrom();  
  4.             childform.MdiParent = this;  
  5.             childform.Show();  
  6.         }  

Now, examine the code. The first line reads -

  1. MyFrom childform = new MyFrom();  

It creates a new instance of “MyForm” form which we added to the project.

The second line is -

  1. childform.MdiParent = this;  

It sets the MdiParent property of the instance of “MyForm”. We have set the MDIparent of this form to “this’ which is the current form -(“MDIContainer”) form.

The last line is -

  1. childform.Show();  

This opens MyForm. This will open inside the client area of “MDIContainer” form.

Let’s run the application and check it. In the running form, click on the “File >> New File” menu. It will open a form. You can click on “New File” many times and it will open multiple forms. Try writing in the form. You will see that you are able to write in any of the opened forms (which, in fact, are instances of “MyForm” which we added to the project).

Now, let’s write the code for opening a new file. The files which can be opened in Richtextbox can either be text files or a “*.rtf” rich text file. So, let’s drag and drop an “OpenFileDialog” to “MDIContainer” form from Toolbox->Dialogs. Now, double click on the second submenu under “File”, i.e., “Open File”. A new method will be generated in the code behind.

  1. private void openToolStripMenuItem_Click(object sender, EventArgs e)  
  2.         {  
  3.         }  

In this method, let’s add the following code.

  1. private void openToolStripMenuItem_Click(object sender, EventArgs e)  
  2.         {  
  3.             openFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);  
  4.             openFileDialog1.RestoreDirectory = true;  
  5.             openFileDialog1.Filter = "Text Files (*.txt)|*.txt| Rich Text Format (*.rtf)|*.rtf|" + "All Files (*.*)|*.*";  
  6.             if (openFileDialog1.ShowDialog() == DialogResult.OK)  
  7.             {  
  8.                 String fileName = openFileDialog1.FileName;  
  9.                 if (fileName.Length != 0)  
  10.                 {  
  11.                     try  
  12.                     {  
  13.                         MyFrom childform = new MyFrom();  
  14.                         childform.OpenFile(openFileDialog1.FileName);  
  15.                         childform.MdiParent = this;  
  16.                         childform.Show();                          
  17.                     }  
  18.                     catch  
  19.                     {  
  20.                         MessageBox.Show(String.Format("{0} is not " + "a valid file", fileName), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);  
  21.                     }  
  22.                 }  
  23.             }  
  24.         }  

 

  • The first line sets the directory to open by default when the openFile dialog opens. We have set it to “MyDocuments” folder.
  • The second line sets the default directory when closing File dialogue, i.e., it will be set to the current directory you have used while opening the file.
  • The third line sets the filter property of the "Open File" dialog. We have set three types *.txt , *.rtf, and *.*. These three types of files will be shown in the openFile dialog when you try to open any file.
  • The fourth line tries to open the openFile dialog and if the result is “OK”, the code in the following section will be executed.
  • In the try catch block, let’s add code to open a new instance of “MyForm”. I have added a new method “OpenFile(stringfileName)” in the code behind in “MyForm” which is to read the file provided by “MDIContainer” Form into its richtext box. We will see it soon.
  • Here, we have called its “OpenFile” method and passed the file name with full path, which we will select in the openFile dialog.
  • In the catch block, we have added a message box to show the error encountered.

 

Now, let’s examine the “OpenFile” method we have added in “MyForm”.

  1. public void OpenFile(string fileName)  
  2.         {  
  3.             string strExt;  
  4.             strExt = System.IO.Path.GetExtension(fileName);  
  5.             strExt = strExt.ToUpper();  
  6.             if (strExt == ".RTF")  
  7.             {  
  8.                 richTextBox1.LoadFile(fileName, RichTextBoxStreamType.RichText);  
  9.             }  
  10.             else if(strExt == ".TXT")  
  11.             {  
  12.                 richTextBox1.LoadFile(fileName, RichTextBoxStreamType.PlainText);  
  13.             }  
  14.             else  
  15.             {  
  16.                 System.IO.StreamReader txtReader;  
  17.                 txtReader = new System.IO.StreamReader(fileName);  
  18.                 richTextBox1.Text = txtReader.ReadToEnd();  
  19.                 txtReader.Close();  
  20.                 txtReader = null;  
  21.                 richTextBox1.SelectionStart = 0;  
  22.                 richTextBox1.SelectionLength = 0;  
  23.             }  
  24.             richTextBox1.Modified = false;  
  25.             this.Text = "Editor: " + fileName;  
  26.         }  

First, we get the extension of file we are going to open. If it is a *.rtf file, it will be opened with the following options.

  1. richTextBox1.LoadFile(fileName, RichTextBoxStreamType.RichText);  

If it is a *.txt file, it will be opened with the following options.

  1. richTextBox1.LoadFile(fileName, RichTextBoxStreamType.PlainText);  

Otherwise, we use the System.IO.StreamReader to read the file.

Lastly, we set the title text of the form to the filename.

  1. this.Text = "Editor: " + fileName;  

Try testing the application and opening a file.

Now, let’s see the code for saving the file.

For that, let’s drag and drop a “SaveFileDialog” to “MDIContainer” form. Now, double-click on the “Save File” submenu. The method will be generated in the code behind. Let’s add the following code to it.

  1. private void saveToolStripMenuItem_Click(object sender, EventArgs e)  
  2.         {    
  3.             saveFileDialog1.Filter = "Text Files (*.txt)|*.txt|RTF Files (*.rtf)|*.rtf|All Files(*.*)|*.*";  
  4.             saveFileDialog1.AddExtension = true;  
  5.             if (saveFileDialog1.ShowDialog() == DialogResult.OK)  
  6.             {  
  7.                 Form activeChildForm = this.ActiveMdiChild;  
  8.                 if (activeChildForm != null)  
  9.                 {  
  10.                     RichTextBox RichtxtEditor = activeChildForm.ActiveControl as RichTextBox;  
  11.                     if (RichtxtEditor != null)  
  12.                     {  
  13. string extension = System.IO.Path.GetExtension(saveFileDialog1.FileName);  
  14.                         if (extension.ToLower() == ".txt"/*saveFileDialog.FilterIndex==1*/  
  15.                         RichtxtEditor.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.PlainText);  
  16.                         else if(extension.ToLower()==".rtf")  
  17.                         RichtxtEditor.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.RichText);                    }  
  18.                 }                  
  19.             }                  
  20.         }  

The first line sets the filter for file types to be saved as. The second line is -

  1. saveFileDialog1.AddExtension = true;  

This sets the AddExtension property to true . It adds an extension according to the file type selected in saveFile dialog if it is omitted.

The next line reads -
  1. if (saveFileDialog1.ShowDialog() == DialogResult.OK)  

It opens the saveFile dialog if the dialog result is OK.

The next line is -
  1. Form activeChildForm = this.ActiveMdiChild;  

It is used to get the active child form (active MyForm in our case).

The next line reads -
  1. if (activeChildForm != null)  
  2.                 {  

It checks if there is an active child form.

In the next line, we get the Richtextbox of the active child form,
  1. RichTextBox RichtxtEditor = activeChildForm.ActiveControl as RichTextBox;  

The next line checks if the Control(RichTextBox) is available.

  1. if (activeChildForm != null)  

The next line gets the extension of file.

  1. var extension = System.IO.Path.GetExtension(saveFileDialog1.FileName);  

The next line checks the extension of the file and saves it accordingly.

  1. if (RichtxtEditor != null)  
  2.                     {  
  3. string extension = System.IO.Path.GetExtension(saveFileDialog1.FileName);  
  4.                         if (extension.ToLower() == ".txt"/*saveFileDialog.FilterIndex==1*/  
  5.                         RichtxtEditor.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.PlainText);  
  6.                         else if(extension.ToLower()==".rtf")  
  7.                         RichtxtEditor.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.RichText);                    }  
  8.                 }   

Now, let’s try to insert an image and some text.

For inserting text, let’s double click on the “Text” submenu under “Insert” menu. A new method will be generated in the code behind. Let’s add the following code to it.

  1. private void textToolStripMenuItem_Click(object sender, EventArgs e)  
  2.         {  
  3.             Form activeChildForm = this.ActiveMdiChild;  
  4.             if (activeChildForm != null)  
  5.             {  
  6.                 RichTextBox RichtxtEditor = activeChildForm.ActiveControl as RichTextBox;  
  7.                 if (RichtxtEditor != null)  
  8.                 {  
  9.                     DataFormats.Format myFormat = DataFormats.GetFormat(DataFormats.Text);  
  10.   
  11.                     if (RichtxtEditor.CanPaste(myFormat))  
  12.                     {  
  13.                         RichtxtEditor.Paste(myFormat);                          
  14.                     }                      
  15.                 }  
  16.             }  
  17.         }  

As previously, we get the active richtextbox; then set the data format which can be set to access the text on clipboard. Then, paste the text to the richtextbox.

Now, let’s add the code for inserting an image into the rich text box. Let’s double click on the “Image” submenu under the “Insert” menu. A stub code will be generated in the code behind. Let’s write the following code to it.

  1. private void imageToolStripMenuItem_Click(object sender, EventArgs e)  
  2.         {  
  3.             openFileDialog1.Title = "Insert Image";  
  4.             openFileDialog1.DefaultExt = "bmp";  
  5.             openFileDialog1.Filter = "Bitmap Files|*.bmp|JPEG Files|*.jpg|GIF Files|*.gif|PNG Files|*.png";  
  6.             openFileDialog1.FilterIndex = 1;  
  7.   
  8.             if (openFileDialog1.ShowDialog() == DialogResult.OK)  
  9.             {  
  10.                 if (openFileDialog1.FileName == "")  
  11.                 {  
  12.                     return;  
  13.                 }  
  14.   
  15.                 try  
  16.                 {  
  17.                     Form activeChildForm = this.ActiveMdiChild;  
  18.                     if (activeChildForm != null)  
  19.                     {  
  20.                         RichTextBox RichtxtEditor = activeChildForm.ActiveControl as RichTextBox;  
  21.                         if (RichtxtEditor != null)  
  22.                         {  
  23.                             string theImagePath1 = openFileDialog1.FileName;  
  24.                             Image img;  
  25.                             img = Image.FromFile(theImagePath1);  
  26.                             Clipboard.SetDataObject(img);  
  27.                             DataFormats.Format df;  
  28.                             df = DataFormats.GetFormat(DataFormats.Bitmap);  
  29.                             if (RichtxtEditor.CanPaste(df))  
  30.                             {  
  31.                                 RichtxtEditor.Paste(df);  
  32.                             }  
  33.                         }  
  34.                     }  
  35.                 }  
  36.                 catch  
  37.                 {  
  38.                     MessageBox.Show("Unable to insert image.""Error", MessageBoxButtons.OK, MessageBoxIcon.Error);  
  39.                 }  
  40.             }  
  41.         }  

The first line sets the title for openFile dialog. We have seen the filter in earlier openFile dialog so that’s understood. We come directly to the highlighted code, i.e., 

string theImagePath1 = openFileDialog1.FileName; 

Here, we get the full path of the image file selected in openFile dialog.
 
In the next line, i.e.,
  1. Image img;  
  2. img = Image.FromFile(theImagePath1);  

We get the image form where we declare an image file and set the image in it. In the next line, we set it on the clipboard of Windows so that we can paste it from there.

  1. Clipboard.SetDataObject(img);  

The next lines are obvious. We use these as we have done in the Paste text earlier.

  1. DataFormats.Format df;  
  2.  df = DataFormats.GetFormat(DataFormats.Bitmap);  
  3.  if (RichtxtEditor.CanPaste(df))  
  4.    {  
  5.        RichtxtEditor.Paste(df);  
  6.    }  

Now, let’s write the code for the Edit menu’s submenus. First, let’s write the code for “Undo” submenu. Double-click on the “Undo” submenu. The code will be generated in the code behind. Let’s write the following code.

  1. private void undoToolStripMenuItem_Click(object sender, EventArgs e)  
  2.         {  
  3.             Form activeChildForm = this.ActiveMdiChild;  
  4.             if (activeChildForm != null)  
  5.             {  
  6.                 RichTextBox RichtxtEditor = activeChildForm.ActiveControl as RichTextBox;  
  7.                 if (RichtxtEditor != null)  
  8.                 {  
  9.                     if (RichtxtEditor.CanUndo)  
  10.                     {  
  11.                         activeChildForm.SuspendLayout();  
  12.                         RichtxtEditor.Undo();                          
  13.                         activeChildForm.ResumeLayout();  
  14.                     }  
  15.                 }  
  16.             }  
  17.         }  

Here, we call the suspendlayout method of the active child form. This suspends the layout of the form. Then we call the undo method of richtextbox and then we resume the layout.

Similarly, for Redo.

  1. private void redoToolStripMenuItem_Click(object sender, EventArgs e)  
  2.         {  
  3.             Form activeChildForm = this.ActiveMdiChild;  
  4.             if (activeChildForm != null)  
  5.             {  
  6.                 RichTextBox RichtxtEditor = activeChildForm.ActiveControl as RichTextBox;  
  7.                 if (RichtxtEditor != null)  
  8.                 {  
  9.                     if (RichtxtEditor.CanRedo == true)  
  10.                     {  
  11.                         if (RichtxtEditor.RedoActionName != "Delete")  
  12.                         {  
  13.                             activeChildForm.SuspendLayout();  
  14.                             RichtxtEditor.Redo();  
  15.                             activeChildForm.ResumeLayout();  
  16.                         }  
  17.                      }       
  18.                  }                    
  19.             }  
  20.         }  

We call the “Redo” method of the richtextbox.

Next, we will see the mark text menu option for coloring the background of the selected text.

  • First, let’s add “colorDialog” to the “MDIContainer” Form.
  • Let’s go to Toolbox->Dialogs->colorDialog and double click on it.
  • Double click on “Mark Text” submenu option under “Edit” menu in “MDIContainer” form. A stub code will be generated in the code behind.
  • Let’s write the following code in it.
  1. private void markTextToolStripMenuItem_Click(object sender, EventArgs e)  
  2.         {  
  3.               
  4.                 Form activeChildForm = this.ActiveMdiChild;  
  5.                 if (activeChildForm != null)  
  6.                 {  
  7.                     RichTextBox RichtxtEditor = activeChildForm.ActiveControl as RichTextBox;  
  8.                     if (RichtxtEditor != null)  
  9.                     {  
  10.                         if (colorDialog1.ShowDialog() == DialogResult.OK)  
  11.                         {  
  12.                             if (RichtxtEditor.SelectionLength > 0)  
  13.                             {  
  14.                                 RichtxtEditor.SelectionBackColor = colorDialog1.Color;  
  15.                                 RichtxtEditor.DeselectAll();  
  16.                             }  
  17.                         }  
  18.                     }  
  19.                 }                              
  20.         }          
  21. }  

We will see the marked text. The first line is -

  1. RichtxtEditor.SelectionBackColor = colorDialog1.Color;  

Here, we set the backcolor of the selected text to the color of the colorDialog. Then, in the next line, we deselect the text.

Now, let’s see how to set the forecolor of the selected text.

Let’s double click on ForeColor submenu option and in the code behind add the following code.
  1. private void foreColorToolStripMenuItem_Click(object sender, EventArgs e)  
  2.         {  
  3.             Form activeChildForm = this.ActiveMdiChild;  
  4.             if (activeChildForm != null)  
  5.             {  
  6.                 RichTextBox RichtxtEditor = activeChildForm.ActiveControl as RichTextBox;  
  7.                 if (RichtxtEditor != null)  
  8.                 {  
  9.                     if (colorDialog1.ShowDialog() == DialogResult.OK)  
  10.                     {  
  11.                         RichtxtEditor.SelectionColor = colorDialog1.Color;  
  12.                         RichtxtEditor.DeselectAll();  
  13.                     }  
  14.                 }  
  15.             }                
  16.         }  

In the first line, we set the forecolor of the selected text. Then, we deselect  all the text.

Now, let’s see how to set the font of the selected text. Drag and drop a fontDialog to the “MDIContainer” form. Now, double-click on the “Font” submenu option. Now, let’s write the following code in the code behind.

  1. private void foreColorToolStripMenuItem_Click(object sender, EventArgs e)  
  2.         {  
  3.             Form activeChildForm = this.ActiveMdiChild;  
  4.             if (activeChildForm != null)  
  5.             {  
  6.                 RichTextBox RichtxtEditor = activeChildForm.ActiveControl as RichTextBox;  
  7.                 if (RichtxtEditor != null)  
  8.                 {  
  9.                     if (colorDialog1.ShowDialog() == DialogResult.OK)  
  10.                     {  
  11.                         if (RichtxtEditor.SelectionLength > 0)  
  12.                         {  
  13.                             RichtxtEditor.SelectionColor = colorDialog1.Color;  
  14.                             RichtxtEditor.DeselectAll();  
  15.                         }  
  16.                     }  
  17.                 }  
  18.             }                
  19.         }  

In the first line, we set the font of the selected text as per the font selected in fontDialog. In the next line, we deselect all the text.

Now, let’s see how to set the current default color of the text.

Let’s double click on the “Current Color” submenu option and go to the code behind and add the following code.

  1. private void currentTextToolStripMenuItem_Click(object sender, EventArgs e)  
  2.         {  
  3.             Form activeChildForm = this.ActiveMdiChild;  
  4.             if (activeChildForm != null)  
  5.             {  
  6.                 RichTextBox RichtxtEditor = activeChildForm.ActiveControl as RichTextBox;  
  7.                 if (RichtxtEditor != null)  
  8.                 {  
  9.                     if (colorDialog1.ShowDialog() == DialogResult.OK)  
  10.                     {  
  11.                         RichtxtEditor.ForeColor = colorDialog1.Color;                         
  12.                     }  
  13.                 }  
  14.             }           
  15.         }  

Here, in the line RichtxtEditor.ForeColor = colorDialog1.Color; we set the current color of text. This will be the color of the text as we type in some new text.

Now, let’s set the current font of text. Let’s double click on “Current Font” menu option and in the code behind, write the following code.

  1. private void currentFontToolStripMenuItem_Click(object sender, EventArgs e)  
  2.         {  
  3.             Form activeChildForm = this.ActiveMdiChild;  
  4.             if (activeChildForm != null)  
  5.             {  
  6.                 RichTextBox RichtxtEditor = activeChildForm.ActiveControl as RichTextBox;  
  7.                 if (RichtxtEditor != null)  
  8.                 {  
  9.                     if (fontDialog1.ShowDialog() == DialogResult.OK)  
  10.                     {  
  11.                         RichtxtEditor.Font = fontDialog1.Font;  
  12.                     }  
  13.                 }  
  14.             }           
  15.         }  

Here, we set the current font of the richtextbox from the fontdialog selection.

Now, let’s see how to “Cut” from richtextbox. Double click on “Cut” submenu option and in the code-behind, let’s write the following code.

  1. private void cutToolStripMenuItem_Click(object sender, EventArgs e)  
  2.         {  
  3.             Form activeChildForm = this.ActiveMdiChild;  
  4.             if (activeChildForm != null)  
  5.             {  
  6.                 RichTextBox RichtxtEditor = activeChildForm.ActiveControl as RichTextBox;  
  7.                 if (RichtxtEditor != null)  
  8.                 {  
  9.                     if (RichtxtEditor.SelectionLength > 0)  
  10.                         RichtxtEditor.Cut();     
  11.                 }  
  12.             }           
  13.         }  

If selection is not zero, we cut the text or image or both.

Similarly, for copy, we double click on the “Copy” submenu and write the following code in the code behind file.

  1. private void copyToolStripMenuItem_Click(object sender, EventArgs e)  
  2.         {  
  3.             Form activeChildForm = this.ActiveMdiChild;  
  4.             if (activeChildForm != null)  
  5.             {  
  6.                 RichTextBox RichtxtEditor = activeChildForm.ActiveControl as RichTextBox;  
  7.                 if (RichtxtEditor != null)  
  8.                 {  
  9.                     if (RichtxtEditor.SelectionLength > 0)  
  10.                         RichtxtEditor.Copy();  
  11.                 }  
  12.             }           
  13.         }  

Here, we copy the selection if it is not zero.

For pasting, we double click on the“Paste” submenu and in the code-behind file, we write the following code.

  1. private void pasteToolStripMenuItem_Click(object sender, EventArgs e)  
  2.         {  
  3.             Form activeChildForm = this.ActiveMdiChild;  
  4.             if (activeChildForm != null)  
  5.             {  
  6.                 RichTextBox RichtxtEditor = activeChildForm.ActiveControl as RichTextBox;  
  7.                 if (RichtxtEditor != null)  
  8.                 {  
  9.                     RichtxtEditor.Paste();  
  10.                 }  
  11.             }          
  12.         }  

Here, we paste the cut or copied selection.

Now, to close the currently active Form, we write the following code.

  1. private void closeActiveFormToolStripMenuItem_Click(object sender, EventArgs e)  
  2.         {  
  3.             this.ActiveMdiChild.Close();  
  4.         }  

Now, let’s go to “MyForm” and in its code behind, let’s write the following.   

  1. public MyFrom()  
  2.         {  
  3.             InitializeComponent();  
  4.             this.richTextBox1.EnableAutoDragDrop = true;  
  5.             this.richTextBox1.AcceptsTab = true;  
  6.             this.richTextBox1.WordWrap = true;  
  7.             this.richTextBox1.ScrollBars = RichTextBoxScrollBars.Both;  
  8.             this.richTextBox1.ShortcutsEnabled = true;  
  9.   
  10.         }  
  11.   
  12. this.richTextBox1.EnableAutoDragDrop = true;  this enables drag and drop to Richtextbox  
  13. this.richTextBox1.AcceptsTab = truethis enables tabs   
  14. this.richTextBox1.WordWrap = truethis enables Word wrap according to the width of your form  
  15. this.richTextBox1.ScrollBars = RichTextBoxScrollBars.Both; This enables scrollbars in the richtextbox  
  16. this.richTextBox1.ShortcutsEnabled = true; This enables the shortcut keys in RichTextBox.  

The following are the shortcut keys,

  • CTRL+Z
  • CTRL+E
  • CTRL+C
  • CTRL+Y
  • CTRL+X
  • CTRL+BACKSPACE
  • CTRL+V
  • CTRL+DELETE
  • CTRL+A
  • SHIFT+DELETE
  • CTRL+L
  • SHIFT+INSERT
  • CTRL+R

Summary

We saw in this article that Multiple Document Interface can be created using Windows.Forms Application. It is easy and very useful because it enables users to work with multiple documents at the same time. MDI applications can be used for a variety of purposes - for example, working on one document while referring to another document, viewing different presentations of the same information, viewing multiple Web sites at the same time, and any task that requires multiple reference points and work areas at the same time. Examples of MDI Applications are,

  • Visual Studio 
  • Adobe Photoshop
  • Google Chrome
  • Microsoft Word 2003
  • Microsoft Excel 2003 etc.

This completes our simple MDI with RichTextBox tutorial. You can play with it and add advanced features like indenting text, bulleting etc. You can check the original application attached to this article.

For reference, you can check the following website,

Thank You!