Creating Advanced Notepad In C#

 
 
 
  

Introduction

Well, we all know what notepad is, but nowadays a single document editor is used less frequently, is at all. Consider that you want to develop an editor or IDE that can handle multiple documents or files using C#, then this article is for you. In this article we will create a notepad that can handle multiple files or documents separately.
 
You require Visual Studio Professional 2013 or higher version and .NET framework 4.5 or higher.
 
Download the source code for Simple,Custom advanced notepad & also demo(.exe files)

As you know what kind of main functions are there to develop in a notepad like New, Open, Save, Save As, Close, Print,Run etc., in this article we will not see Syntax Highlighting, but if you want to add syntax highlighting to your project then you can use developed editors like ICShap code Avalon Edit or EditorControl, or you can develop your own editor. In this article we will see only for handling main functions like Open,Save,Close etc. We will also add Line Numbers to our RichTextBox. Go to the following link to see how to create line numbers for RichTextBox. 
We will also add Windows menu to our notepad, to create window menu go to following link,
You can also add Auto Completion function, Custom Windows Form, Auto Complete Brackets, and Custom Controls.

Go to following links to create these all functions,
You can use following articles to customize your notepad
If you want to create your own Integrated Development Environment(IDE) then see following article 
We will also create dark custom notepad in C#, see above or following black image.
 
 
 
To customize a form, see the article Customizing Windows Forms in C#. The code is same with a small change in customization code & in design. Download the source code for both simple & customize notepads. Remember, both the codes have same project names.
 
In "Customizing Windows Forms In C#" article, we created another form named CustomForm which we are designing & in mainform we just changing its super class. Here we need only one form to be customize which is a MainForm. so in this i didnt created any another form for customize.i have customized the main form in the way what i wanted & also adding codes in MainForm.cs file.
 
Let's Start

I have created a class with line numbers, MyRichTextBox.

For creating multiple tabbed documents you cannot directly add RichTextBox to TabControl when user creates a New or Open a file. You have to create a class (I am using MyTabPage) with its super class TabPage that takes an object of MainForm and then adds this class to TabControl. Remember about Modifiers of objects. Download the source code for better understanding.

Here's the code of MyTabPage class.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Drawing;  
  5. using System.Text;  
  6. using System.Windows.Forms;  
  7. using System.Diagnostics;  
  8. namespace AdvancedNotepad_CSharp  
  9. {  
  10.    public class MyTabPage : System.Windows.Forms.TabPage  
  11.     {  
  12.        public MainForm mainform;  
  13.        public MyRichTextBox _myRichTextBox = new MyRichTextBox();  
  14.   
  15.        public MyTabPage(MainForm mf)  
  16.        {  
  17.            mainform = mf;  
  18.   
  19.            this._myRichTextBox.Dock = DockStyle.Fill;  
  20.            this._myRichTextBox.richTextBox1.Text = "";  
  21.            _myRichTextBox.richTextBox1.Font = new System.Drawing.Font("Monospaced", 11, FontStyle.Regular);  
  22.            this._myRichTextBox.richTextBox1.Select();  
  23.   
  24.            _myRichTextBox.richTextBox1.TextChanged += new EventHandler(this.richTextBox1_TextChanged);  
  25.            _myRichTextBox.richTextBox1.SelectionChanged += new EventHandler(this.richTextBox1_SelectionChanged);  
  26.   
  27.            _myRichTextBox.richTextBox1.LinkClicked += new LinkClickedEventHandler(this.richTextBox1_LinkClicked);  
  28.   
  29.            this.Controls.Add(_myRichTextBox);  
  30.        }  
  31.   
  32.   
  33.   
  34.        private void richTextBox1_TextChanged(object sender, EventArgs e)  
  35.        {  
  36.            String str = this.Text;  
  37.            if (str.Contains("*"))  
  38.            {  
  39.   
  40.            }  
  41.            else  
  42.            {  
  43.                this.Text = str + "*";  
  44.            }  
  45.        }  
  46.   
  47.        private void richTextBox1_SelectionChanged(object sender, EventArgs e)  
  48.        {  
  49.            int sel = _myRichTextBox.richTextBox1.SelectionStart;  
  50.            int line = _myRichTextBox.richTextBox1.GetLineFromCharIndex(sel) + 1;  
  51.            int col = sel - _myRichTextBox.richTextBox1.GetFirstCharIndexFromLine(line - 1) + 1;  
  52.   
  53.            mainform.LineToolStripLabel.Text = "Line : " + line.ToString();  
  54.            mainform.ColumnToolStripLabel.Text = "Col : " + col.ToString();  
  55.        }  
  56.   
  57.   
  58.   
  59.        private void richTextBox1_LinkClicked(object sender, LinkClickedEventArgs e)  
  60.        {  
  61.            Process.Start(e.LinkText);  
  62.        }  
  63.   
  64.     }  
  65. }  
Here's the function "UpdateDocumentSelectorList()" that removes and re adds all elements of treeView1. This function just adds all visible tabs text of TabControl to treeView1.

You just need to call this function once New, Open, or Close action is completed for handling only visible documents.
  1. public void UpdateDocumentSelectorList()  
  2. {  
  3.     TabControl.TabPageCollection tabcoll = myTabControlZ.TabPages;  
  4.     treeView1.Nodes.Clear();  
  5.     foreach(TabPage tabpage in tabcoll)  
  6.     {  
  7.        String fname = tabpage.Text;  
  8.        Color color = Color.FromArgb(245, 255, 245);  
  9.         if (fname.Contains("*"))  
  10.         {  
  11.             fname = fname.Remove(fname.Length - 1);  
  12.         }  
  13.         if(fname.Contains("Untitled"))  
  14.         {  
  15.             color = Color.FromArgb(245, 255, 245);  
  16.         }  
  17.         else  
  18.         {  
  19.             color = Color.FromA#fffafa;  
  20.         }  
  21.   
  22.         TreeNode trnode = new TreeNode();  
  23.         trnode.Text = fname;  
  24.         trnode.BackColor = color;  
  25.         treeView1.Nodes.Add(trnode);  
  26.     }  
  27. }  
Getting RichTextBox Control from Current Tab in TabControl
 
When you add a Tab to TabControl and if you want to get the added RichTextBox control from the selected tab, then use the following syntax.
   
var object=(object)TabControl.TabPages[tabcontrol selected index].Controls[0];

e.g.:
  1. var _myRichTextBox = (MyRichTextBox)myTabControlZ.TabPages[myTabControlZ.SelectedIndex].Controls[0];  
Now, you can perform actions on _myRichTextBox which is the selected richTextBox from selected tab in TabControl. 
 
All right, let's move to the main form. Once you have created the Windows Forms Application then add MenuStrip, and SplitContainer. Add TreeView (treeView1) to panel1 and TabControl to panel2 of splitcontainer respectively. Remember you directly cannot add ContextMenuStrip to RichTextBox,rather than you must add ContextMenuStrip to every added RichTextBox to a TabControl. Download the source code for better understanding.

Now, let's see some important functions code.
 
1. New
 
This function creates a new Untitled document with tab and will also add this text to treeView1. 
  1. public static int count = 1;  
  2. private void File_New_MenuItem_Click(object sender, EventArgs e)  
  3. {  
  4.     MyTabPage tabpage = new MyTabPage(this);  
  5.     tabpage.Text = "Untitled " + count;  
  6.     myTabControlZ.TabPages.Add(tabpage);  
  7.   
  8.     myTabControlZ.SelectedTab = tabpage;  
  9.   
  10.     var _myRichTextBox = (MyRichTextBox)myTabControlZ.TabPages[myTabControlZ.SelectedIndex].Controls[0];  
  11.     _myRichTextBox.richTextBox1.Select();  
  12.   
  13.     //add contextmenustrip to richTextBox1  
  14.     _myRichTextBox.richTextBox1.ContextMenuStrip = myContextMenuStrip;  
  15.   
  16.     this.UpdateDocumentSelectorList();  
  17.   
  18.     this.Text = "Advanced Notepad in C# [ Untitled "+count+" ]";  
  19.   
  20.     FilenameToolStripLabel.Text = tabpage.Text;  
  21.   
  22.     UpdateWindowsList_WindowMenu();  
  23.   
  24.     count++;  
  25. }  
2. Open

This function will open multiple files from selecting files from OpenFileDialog by setting MultiSelect property to true. Let's see the code that creates the  tab. Read each file at a time and add that tab to tabcontrol. Also add ContextMenuStrip to RichTextBox.
  1. private void File_Open_MenuItem_Click(object sender, EventArgs e)  
  2.       {  
  3.           StreamReader strReader;  
  4.           String str;  
  5.           if (openFileDialog1.ShowDialog() == DialogResult.OK)  
  6.           {  
  7.               String[] files = openFileDialog1.FileNames;  
  8.               foreach (string filename in files)  
  9.               {  
  10.                   MyTabPage tabpage = new MyTabPage(this);  
  11.   
  12.                   strReader = new StreamReader(filename);  
  13.                   str = strReader.ReadToEnd();  
  14.                   strReader.Close();  
  15.   
  16.                   String fname = filename.Substring(filename.LastIndexOf("\\") + 1);  
  17.                   tabpage.Text = fname;  
  18.   
  19.                   //add contextmenustrip to richTextBox1  
  20.                   tabpage._myRichTextBox.richTextBox1.ContextMenuStrip = myContextMenuStrip;  
  21.   
  22.                   tabpage._myRichTextBox.richTextBox1.Text = str;  
  23.                   myTabControlZ.TabPages.Add(tabpage);  
  24.                   myTabControlZ.SelectedTab = tabpage;  
  25.   
  26.   
  27.                   this.UpdateDocumentSelectorList();  
  28.   
  29.   
  30.                   /* check (*) is available on TabPage Text 
  31.                    adding filename to tab page by removing (*) */  
  32.                   fname = tabpage.Text;  
  33.                   if (fname.Contains("*"))  
  34.                   {  
  35.                       fname = fname.Remove(fname.Length - 1);  
  36.                   }  
  37.                   tabpage.Text = fname;  
  38.   
  39.                   //adding filenames to OpenedFilesList list  
  40.                   OpenedFilesList.Add(filename);  
  41.   
  42.                   FilenameToolStripLabel.Text = filename;  
  43.                   this.Text = "Advanced Notepad in C# [ "+fname+" ]";  
  44.               }  
  45.   
  46.   
  47.               if (myTabControlZ.SelectedIndex >= 0)  
  48.               {  
  49.                   var _myRichTextBox = (MyRichTextBox)myTabControlZ.TabPages[myTabControlZ.SelectedIndex].Controls[0];  
  50.                   _myRichTextBox.richTextBox1.Select();  
  51.               }  
  52.               UpdateWindowsList_WindowMenu();  
  53.           }  
  54.       }  
3. Save, Save As, Save All

Well, you know how to save a file. So, we will see only Save All functions for saving all the files at once, without overwriting one file contents to another file contents. See the above image. In status strip, you can see full filename path. Here's the code to save all files one by one.
  1. private void File_SaveAll_MenuItem_Click(object sender, EventArgs e)  
  2. {  
  3.     if (myTabControlZ.TabCount > 0)  
  4.     {  
  5.         OpenedFilesList.Reverse();  
  6.         TabControl.TabPageCollection tabcoll = myTabControlZ.TabPages;  
  7.   
  8.         foreach(TabPage tabpage in tabcoll)  
  9.         {  
  10.             myTabControlZ.SelectedTab = tabpage;  
  11.             myTabControlZ_SelectedIndexChanged(sender, e);  
  12.               
  13.             if( ! tabpage.Text.Contains("Untitled"))  
  14.             {  
  15.                 try  
  16.                 {  
  17.                     var _myRichTextBox = (MyRichTextBox)myTabControlZ.TabPages[myTabControlZ.SelectedIndex].Controls[0];  
  18.                     File.WriteAllText(FilenameToolStripLabel.Text, "");  
  19.                     StreamWriter strwriter = System.IO.File.AppendText(FilenameToolStripLabel.Text);  
  20.                     strwriter.Write(_myRichTextBox.richTextBox1.Text);  
  21.                     strwriter.Close();  
  22.                     strwriter.Dispose();  
  23.                 }  
  24.                 catch { }  
  25.             }  
  26.         }  
  27.   
  28.         System.Windows.Forms.TabControl.TabPageCollection tabcollection = myTabControlZ.TabPages;  
  29.         foreach (TabPage tabpage in tabcollection)  
  30.         {  
  31.             String str = tabpage.Text;  
  32.             if (str.Contains("*")&& !str.Contains("Untitled"))  
  33.             {  
  34.                 str = str.Remove(str.Length - 1);  
  35.             }  
  36.             tabpage.Text = str;  
  37.         }  
  38.         UpdateWindowsList_WindowMenu();  
  39.     }  
  40. }   
4. Close

If you have used any editor like Notepad++, it provides a function to close a file. Here, we will also create that function. This function is for nothing but to remove Tab from TabControl and remove node from treeView1 after saving a file. Here's the code to close a document. Download the source code to view "Close All" function code.
  1. private void File_Close_MenuItem_Click(object sender, EventArgs e)  
  2. {  
  3.     if (myTabControlZ.TabCount > 0)  
  4.     {  
  5.         TabPage tabpage = myTabControlZ.SelectedTab;  
  6.         if (tabpage.Text.Contains("*"))  
  7.         {  
  8.             DialogResult dg = MessageBox.Show("Do you want to save " + tabpage.Text + " file before close ?""Save before Close ?", MessageBoxButtons.YesNo);  
  9.             if (dg == DialogResult.Yes)  
  10.             {  
  11.                 //save file before close  
  12.                 File_Save_MenuItem_Click(sender, e);  
  13.                 //remove tab  
  14.                 myTabControlZ.TabPages.Remove(tabpage);  
  15.   
  16.                 //RemoveFileNamesFromTreeView(tabpage.Text);  
  17.                 this.UpdateDocumentSelectorList();  
  18.   
  19.                 UpdateWindowsList_WindowMenu();  
  20.                 myTabControlZ_SelectedIndexChanged(sender, e);  
  21.   
  22.                 LineToolStripLabel.Text = "Line";  
  23.                 ColumnToolStripLabel.Text = "Col";  
  24.   
  25.                 if (myTabControlZ.TabCount == 0)  
  26.                 {  
  27.                     FilenameToolStripLabel.Text = "Advanced Notepad in C#";  
  28.                 }  
  29.             }  
  30.             else  
  31.             {  
  32.                 //remove tab  
  33.                 myTabControlZ.TabPages.Remove(tabpage);  
  34.   
  35.                 UpdateDocumentSelectorList();  
  36.   
  37.                 UpdateWindowsList_WindowMenu();  
  38.                 myTabControlZ_SelectedIndexChanged(sender, e);  
  39.   
  40.                 LineToolStripLabel.Text = "Line";  
  41.                 ColumnToolStripLabel.Text = "Col";  
  42.   
  43.                 if (myTabControlZ.TabCount == 0)  
  44.                 {  
  45.                     FilenameToolStripLabel.Text = "Advanced Notepad in C#";  
  46.                 }  
  47.             }  
  48.         }  
  49.         else  
  50.         {  
  51.             //remove tab  
  52.             myTabControlZ.TabPages.Remove(tabpage);  
  53.   
  54.             RemoveFileNamesFromTreeView(tabpage.Text);  
  55.             UpdateDocumentSelectorList();  
  56.   
  57.             UpdateWindowsList_WindowMenu();  
  58.             myTabControlZ_SelectedIndexChanged(sender, e);  
  59.   
  60.             LineToolStripLabel.Text = "Line";  
  61.             ColumnToolStripLabel.Text = "Col";  
  62.   
  63.             if (myTabControlZ.TabCount == 0)  
  64.             {  
  65.                 FilenameToolStripLabel.Text = "Advanced Notepad in C#";  
  66.             }  
  67.         }  
  68.   
  69.         if (myTabControlZ.SelectedIndex >= 0)  
  70.         {  
  71.             var _myRichTextBox = (MyRichTextBox)myTabControlZ.TabPages[myTabControlZ.SelectedIndex].Controls[0];  
  72.             _myRichTextBox.richTextBox1.Select();  
  73.         }  
  74.   
  75.     }  
  76.     else  
  77.     {  
  78.         FilenameToolStripLabel.Text = "Advanced Notepad in C#";  
  79.   
  80.         LineToolStripLabel.Text = "Line";  
  81.         ColumnToolStripLabel.Text = "Col";  
  82.     }  
  83. }  
5. Form Closing
 
When the form is closing, we need to check whether you want to save opened files or not. First, get each tab from tabcontrol, check whether that tab text contains "*" or not. If yes, then show dialog otherwise remove that tab page from tab control. If the result from dialog box is yes, then call save function & remove that tab. If dialog result is "Cancel", then stop the procedure & get out from the loop (for-each).
  1. private void MainForm_Closing(object sender, FormClosingEventArgs e)   
  2. {  
  3.     if (myTabControlZ.TabCount > 0)   
  4.     {   
  5.         TabControl.TabPageCollection tabcoll = myTabControlZ.TabPages;   
  6.         foreach (TabPage tabpage in tabcoll)  
  7.         {   
  8.             myTabControlZ.SelectedTab = tabpage;   
  9.             if (tabpage.Text.Contains("*"))   
  10.             {   
  11.                 DialogResult dg = MessageBox.Show("Do you want to save file " + tabpage.Text + " before close ?""Save or Not", MessageBoxButtons.YesNoCancel);   
  12.                 if (dg == DialogResult.Yes)   
  13.                 {   
  14.                     File_Save_MenuItem_Click(sender, e);   
  15.                     myTabControlZ.TabPages.Remove(tabpage);   
  16.                     myTabControlZ_SelectedIndexChanged(sender, e);   
  17.                 }   
  18.                 else if (dg == DialogResult.Cancel)   
  19.                 {   
  20.                     e.Cancel = true;  
  21.                     myTabControlZ.Select();  
  22.                     break;  
  23.                 }   
  24.             }   
  25.             else   
  26.             {  
  27.                 myTabControlZ.TabPages.Remove(tabpage);   
  28.                 myTabControlZ_SelectedIndexChanged(sender, e);   
  29.             }   
  30.         }   
  31.     }   
  32. }  
Full Screen Mode

To set your application to full screen mode do following actions,
  1. Set Visibility of MainForm to false.
  2. Set FormBorderStyle to None of MainForm.
  3. Set Visibility of MainForm to true.
That's it your application is in full screen mode. To go to normal mode, perform all the above steps only just FormBorderStyle to Sizable.

Here's the code.
  1. private void View_FullScreen_MenuItem_Click(object sender, EventArgs e)  
  2. {  
  3.     if(View_FullScreen_MenuItem.Checked==false)  
  4.     {  
  5.         this.Visible = false;  
  6.         this.FormBorderStyle = FormBorderStyle.None;  
  7.         this.Visible = true;  
  8.   
  9.         View_FullScreen_MenuItem.Checked = true;  
  10.     }  
  11.     else  
  12.     {  
  13.         this.Visible = false;  
  14.         this.FormBorderStyle = FormBorderStyle.Sizable;  
  15.         this.Visible = true;  
  16.   
  17.         View_FullScreen_MenuItem.Checked =false ;  
  18.     }  
  19. }  
But, if you have a customized form, then you cannot directly go to full screen mode as above. As your FormBorderStyle is none. So you need to store the current location & size of the MainForm. To go to full screen mode set WindowState to Maximized & Normal to go to normal window.
 
But remember to go to normal mode, first you need to set old(exact) location & size to your form(the size which was before full screen),then set form to visible and then set WindowState to Normal.
Here's the code
  1. Size formsizeholder = new Size(new Point(500, 300));  
  2. Point formloc = new Point(0, 0);  
  3.   
  4. private void View_FullScreen_MenuItem_Click(object sender, EventArgs e)  
  5. {  
  6.     if(View_FullScreen_MenuItem.Checked==false)  
  7.     {  
  8.         this.Visible = false;  
  9.         TopPanel.Visible = false;  
  10.         this.WindowState = FormWindowState.Maximized;  
  11.         this.Visible = true;  
  12.   
  13.         formsizeholder = this.Size;  
  14.         formloc = this.Location;  
  15.   
  16.         View_FullScreen_MenuItem.Checked = true;  
  17.     }  
  18.     else  
  19.     {  
  20.         this.Visible = false;  
  21.         TopPanel.Visible =true;  
  22.         this.Location = formloc;  
  23.         this.Size = formsizeholder;  
  24.         this.Visible = true;  
  25.   
  26.         this.WindowState = FormWindowState.Normal;  
  27.   
  28.         View_FullScreen_MenuItem.Checked =false ;  
  29.     }  
  30. }  
TabControl ContextMenuStrip

    
 
See above image that provides functions to selected tabpage like Save, Close, Close All But This, Open File Folder etc. You can see this function in almost all advanced editors like Notepad++ or IDE's like Visual Studio. You just need to create context menu strip, adding menus to it and adding this menu strip to tabcontrol.
 
1. Close All But This : This function perfoms action like to remove all other tabs without removing selected or current tab.

Here's the code.
  1. private void myTabControl_CloseAllButThis_MenuItem_Click(object sender, EventArgs e)  
  2. {  
  3.     String tabtext = myTabControlZ.SelectedTab.Text;  
  4.     if (myTabControlZ.TabCount > 1)  
  5.     {  
  6.         TabControl.TabPageCollection tabcoll = myTabControlZ.TabPages;  
  7.         foreach (TabPage tabpage in tabcoll)  
  8.         {  
  9.             myTabControlZ.SelectedTab = tabpage;  
  10.             if (myTabControlZ.SelectedTab.Text != tabtext)  
  11.             {  
  12.                 File_Close_MenuItem_Click(sender, e);  
  13.             }  
  14.         }  
  15.     }  
  16.     else if (myTabControlZ.TabCount == 1)  
  17.     {  
  18.         File_Close_MenuItem_Click(sender, e);  
  19.     }  
  20. }  
2. Open File Folder : This function opens the folder where the file is saved. Here's the code.
  1. private void myTabControl_OpenFileFolder_MenuItem_Click(object sender, EventArgs e)  
  2. {  
  3.     if(myTabControlZ.TabCount>0)  
  4.     {  
  5.         if( ! myTabControlZ.SelectedTab.Text.Contains("Untitled"))  
  6.         {  
  7.             if(FilenameToolStripLabel.Text.Contains("\\"))  
  8.             {  
  9.                 TabPage tabpage = myTabControlZ.SelectedTab;  
  10.                 String tabtext = tabpage.Text;  
  11.                 if(tabtext.Contains("*"))  
  12.                 {  
  13.                     tabtext = tabtext.Remove(tabtext.Length - 1);  
  14.                 }  
  15.                 String fname = FilenameToolStripLabel.Text;  
  16.                 String filename=fname.Remove(fname.Length-(tabtext.Length+1));  
  17.                 Process.Start(filename);  
  18.             }  
  19.         }  
  20.     }  
  21. }  
File Association

Visual Studio allows you to add specific file extensions to the application. To access this property the .NET Framework version must be greater than or equal to 3.5. For adding file extensions, go to your Project Properties. Select Publish tab. Click on Options button. Once "Publish Options" Dialog box appears then select "File Associations" item and add your file extension with icon here.

   
 
So how to open a file in the application when double clicking on file ?

Well it's so much simpler when you double click on a file or right click on a file and select a program, that file name is passed as an argument to the application. So you just have to open this file in the application in the main function of the program. First define the function in the MainForm that opens a file and takes a filename as an argument. You must define this function modifier to public. Code is same as open file as define above in Open function just adding parameter to it. I am using OpenAssociatedFiles_WhenApplicationStarts(String[] files) function to open files.
  
You have to call this function in the main function of program which is in Programs.cs file. First check that the arguments are not null, if it is null then call Application.Run(new MainForm()); otherwise create object of MainForm and call the defined function that open these files (OpenAssociatedFiles_WhenApplicationStarts(args);).

Here's the main code of my application or Program.cs file code.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Windows.Forms;  
  5. using System.IO;  
  6. namespace AdvancedNotepad_CSharp  
  7. {  
  8.     static class Program  
  9.     {  
  10.         /// <summary>  
  11.         /// The main entry point for the application.  
  12.         /// </summary>  
  13.         [STAThread]  
  14.         static void Main(String[] args)  
  15.         {  
  16.             if (args != null && args.Length > 0)  
  17.             {  
  18.                 String[] files = args;  
  19.   
  20.                 MainForm mf = new MainForm();  
  21.                 mf.IsArgumentNull = false;  
  22.                 mf.OpenAssociatedFiles_WhenApplicationStarts(files);  
  23.                 Application.EnableVisualStyles();  
  24.                 Application.Run(mf);  
  25.             }  
  26.             else  
  27.             {  
  28.                 Application.EnableVisualStyles();  
  29.                 Application.SetCompatibleTextRenderingDefault(false);  
  30.                 Application.Run(new MainForm());  
  31.             }  
  32.         }  
  33.     }  
  34. }  
Tab Changed

When you change the tab of tabcontrol then the status strip label value must be changed like full filename. To handle this situation,you need to add SelectedIndexChanged event to tabcontrol. Here's the code that set filename to status strip when tab index is changed.
  1. private void myTabControlZ_SelectedIndexChanged(object sender, EventArgs e)  
  2. {  
  3.     if (myTabControlZ.TabCount > 0)  
  4.     {  
  5.         TabPage tabpage = myTabControlZ.SelectedTab;  
  6.         if (tabpage.Text.Contains("Untitled"))  
  7.         {  
  8.             FilenameToolStripLabel.Text = tabpage.Text;  
  9.             this.Text = "Advanced Notepad in C# [ "+tabpage.Text+" ]";  
  10.             UpdateWindowsList_WindowMenu();  
  11.         }  
  12.         else  
  13.         {  
  14.             foreach (String filename in OpenedFilesList)  
  15.             {  
  16.                 if (tabpage != null)  
  17.                 {  
  18.                     String str = filename.Substring(filename.LastIndexOf("\\") + 1);  
  19.                     if (tabpage.Text.Contains("*"))  
  20.                     {  
  21.                         String str2 = tabpage.Text.Remove(tabpage.Text.Length - 1);  
  22.                         if (str == str2)  
  23.                         {  
  24.                             FilenameToolStripLabel.Text = filename;  
  25.                             this.Text = "Advanced Notepad in C# [ " + tabpage.Text + " ]";  
  26.                         }  
  27.                     }  
  28.   
  29.                     else  
  30.                     {  
  31.                         if (str == tabpage.Text)  
  32.                         {  
  33.                             FilenameToolStripLabel.Text = filename;  
  34.                             this.Text = "Advanced Notepad in C# [ " + tabpage.Text + " ]";  
  35.                         }  
  36.                     }  
  37.                 }  
  38.             }  
  39.   
  40.             UpdateWindowsList_WindowMenu();  
  41.         }  
  42.     }  
  43.     else  
  44.     {  
  45.         FilenameToolStripLabel.Text = "Advanced Notepad in C#";  
  46.         this.Text = "Advanced Notepad in C#";  
  47.         UpdateWindowsList_WindowMenu();  
  48.     }  
  49. }   
Select Tab when Mouse Click on Tree Node

As you know, every editor has a document selector where a user can click on list of document and that document is activated. In Tabbed document,when you double click on tree node then same text of tree node is searched in tabcontrol,if that text is matched then that tab is selected.
 
Here's the code to select the tab.
  1. private void treeView1_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)  
  2. {  
  3.     String str = treeView1.SelectedNode.ToString();  
  4.     String st = str.Substring(str.LastIndexOf(":") + 2);  
  5.     int treenode_length = st.Length;  
  6.     int tab_count = myTabControlZ.TabCount;  
  7.   
  8.     System.Windows.Forms.TabControl.TabPageCollection tb = myTabControlZ.TabPages;  
  9.     foreach (TabPage tabpage in tb)  
  10.     {  
  11.         String tabstr = tabpage.Text;  
  12.         int tab_length = tabstr.Length;  
  13.         if (tabstr.Contains(st))  
  14.         {  
  15.             myTabControlZ.SelectedTab = tabpage;  
  16.         }  
  17.     }  
  18.   
  19.     if (myTabControlZ.SelectedIndex >= 0)  
  20.     {  
  21.         var _myRichTextBox = (MyRichTextBox)myTabControlZ.TabPages[myTabControlZ.SelectedIndex].Controls[0];  
  22.         _myRichTextBox.richTextBox1.Select();  
  23.     }  
  24.   
  25.     this.UpdateWindowsList_WindowMenu();  
  26. }   
Show Information about Menu Item

  
 
When you move the mouse on the menu item, it shows information about that menu item actions in the status strip label. As you know this can be achieved by adding MouseEnter & MouseLeave events to the menu item. But if you have many items then it could take so much time to type code for each menu item. So here's the solution. First create the function that take parameter of a class ToolStripMenuItem (ChangeTextOfReadyLabel(ToolStripMenuItem menuitem)). Add MouseEnter & MouseLeave events to the object of ToolStripMenuItem. In MouseEnter events function get text of selected menu item.

e.g.:

Object b = (ToolStripMenuItem)sender;
String s = b.ToString().Trim();
 
Compare String s with your menu item text like "File","New","Open" etc. and set label text to its information. In MouseLeave events function set label text to its default text. Now create another function,and call created above function "ChangeTextOfReadyLabel()" and pass object of menu item.  And call this created function in Form_Load event. Call following function UpdateReadyLabel() in Form_Load event function.

Here's the code
  1.         public void ChangeTextOfReadyLabel(ToolStripMenuItem menuitem)  
  2.         {  
  3.             menuitem.MouseEnter += new EventHandler(this.menuitem_MouseEnter);  
  4.             menuitem.MouseLeave += new EventHandler(this.menuitem_MouseLeave);  
  5.         }  
  6.         private void menuitem_MouseEnter(object sender,EventArgs e)  
  7.         {  
  8.             Object b = (ToolStripMenuItem)sender;  
  9.             String s = b.ToString().Trim();  
  10.             switch(s)  
  11.             {  
  12.                 case "File": AboutLabel.Text = "Create New,Open,Save,Close and Print Documents";  
  13.                     break;  
  14.                 case "New": AboutLabel.Text = "Create New document";  
  15.                     break;  
  16.                 case "Open": AboutLabel.Text = "Open New Document";  
  17.                     break;  
  18.                 case "Save": AboutLabel.Text = "Save Current Document";  
  19.                     break;  
  20.                 case "Save As": AboutLabel.Text = "Save As Current Document";  
  21.                     break;  
  22.                 case "Save All": AboutLabel.Text = "Save All opened documents";  
  23.                     break;  
  24.                 case "Close": AboutLabel.Text = "Close Current Document";  
  25.                     break;  
  26.                 case "Close All": AboutLabel.Text = "Close All Opened Documents";  
  27.                     break;  
  28.                 case "Open In System Editor": AboutLabel.Text = "Open current document in its system editor";  
  29.                     break;  
  30.                 case "Print": AboutLabel.Text = "Print Current Document";  
  31.                     break;  
  32.                 case "Print Preview": AboutLabel.Text = "Print Preview Current Document";  
  33.                     break;  
  34.                 case "Exit": AboutLabel.Text = "Exit from Application";  
  35.                     break;  
  36.             }  
  37.         }  
  38.         private void menuitem_MouseLeave(object sender, EventArgs e)  
  39.         {  
  40.             AboutLabel.Text = "Ready";  
  41.         }  
  42.   
  43.         public void UpdateReadyLabel()  
  44.         {  
  45.             ChangeTextOfReadyLabel(File_MenuItem);  
  46.             ChangeTextOfReadyLabel(File_New_MenuItem);  
  47.             ChangeTextOfReadyLabel(File_Open_MenuItem);  
  48.             ChangeTextOfReadyLabel(File_Save_MenuItem);  
  49.             ChangeTextOfReadyLabel(File_SaveAs_MenuItem);  
  50.             ChangeTextOfReadyLabel(File_SaveAll_MenuItem);  
  51.             ChangeTextOfReadyLabel(File_Close_MenuItem);  
  52.             ChangeTextOfReadyLabel(File_CloseAll_MenuItem);  
  53.             ChangeTextOfReadyLabel(File_OpenInSystemEditor_MenuItem);  
  54.             ChangeTextOfReadyLabel(File_Print_MenuItem);  
  55.             ChangeTextOfReadyLabel(File_PrintPreview_MenuItem);  
  56.             ChangeTextOfReadyLabel(File_Exit_MenuItem);  
  57.         }  
Download the source code to view complete application. You can use syntax highlighting editors instead of RichTextBox. you can use IC SharpCode AvalotEdit or TextEditorControl.

I used simple RichTextBox only for showing about advanced operations like Open, Save All, Close etc.
 
Read more articles on C#:


Similar Articles