Create notepad using C# part -3

0

Create notepad using C# part -3

notepad-by-Inder1

In part-1 and part-2 of this series the layout and source code for menu items of File menu was discussed. Now in this we discuss the source code for the menu items of Edit menu, this part is very easy because some of the operations are predefined so there is no need to apply any hard logic.

Part 1, Part 2, Part 3, Part 4, Part 5 , Part 6 of the article.

part3_picture1

In the Edit Menu the menu items are redo, undo, cut, copy, paste and some others as shown in picture, so let’s start with redo and undo.

  1. Redo and Undo: The Redo and Undo action can be handed very easily in Rich Textbox because there is a predefined function for redo and undo. We have to simply invoke this function in the handler of the click event of the item. Following  the handler of redo menu item is given :-
    private void redoToolStripMenuItem_Click(object sender, EventArgs e)
            {
                richTextBox1.Redo();
            }
    

    Undo: For the undo operation there is a little trick applied, we perform the redo operation if the undo button is pressed twice. This can be checked by taking a flag and update its value in after each click on the undo button. The code for undo is given bellow

    bool isundo=false;
    private void undoToolStripMenuItem_Click(object sender, EventArgs e)
            {
                isundo=!isundo;
                if (isundo)
                {
                    richTextBox1.Undo();
                    redoToolStripMenuItem.Enabled = true;
                   
                }
                else
                {
                    richTextBox1.Redo();
                }
                
            }
    

     2. Cut, Copy and Paste :Same as undo and redo Rich Textbox have function for cut copy and paste, we have to just invoke these function in the handler of their click event.

    The code for each is given

            private void cutToolStripMenuItem_Click(object sender, EventArgs e)
            {
                
                richTextBox1.Cut();
            }
    
            private void copyToolStripMenuItem_Click(object sender, EventArgs e)
            {
                richTextBox1.Copy();
            }
    

    For the paste button we have to check is there any text copied in the clip board so that we can paste it in our file, for this Rich textbox provide a function named as CanPaste which take the one argument of type clipFormat

part-3_picture-2

So the coding for paste menu item is done as

        private void pasteToolStripMenuItem1_Click(object sender, EventArgs e)
        {

            DataFormats.Format myformat = DataFormats.GetFormat(DataFormats.Text);
            if (richTextBox1.CanPaste(myformat))
            {
                richTextBox1.Paste(myformat);
            }
            
        }

 3. Delete: For the deletion of the text we use the function remove and pass the start point from where the selection of the text start and length of the selected text that we want to delete, the code behind this is given as

        private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
        {
               richTextBox1.Text = richTextBox1.Text.Remove(richTextBox1.SelectionStart,richTextBox1.SelectionLength);
               }

 

Till now the coding the Rich Textbox provide the relevant functions and the task become very easy from now there is no default function is defined so we apply our own logic to complete the task.

4. Find: The find option is available after we write some text in the file, if the notepad file is empty then the find option not works, this can be managed in the drop down event of the edit menu and in by verifying the modification in the rich textbox’s text. The code for this is given as

 private void editToolStripMenuItem_DropDownOpening(object sender, EventArgs e)
        {
            if (richTextBox1.Modified)
            {
                 findToolStripMenuItem.Enabled = undoToolStripMenuItem.Enabled = true;
      
             }    
                
               }

Now this option is enabled after the modification in the text of rich text box .When we click on this button to find some text in the file, a new form open which in which a text box , button and radio buttons are given as shown

part-3_picture-3

So for same functionality we create a new form like this and show this in the handler of click event of find menu item , the code to show the new form is given as

        private void findToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form2 obj = new Form2();
            obj.Show();
        }

You can give name to this form like Find in place of Form2. Like in the find form of Microsoft notepad , add a label , textbox, two buttons, a check box ,and two radio button in the group box so the only one is active at a time. Our form2 look after adding these things as

part-3_picture-4

 

The Button find next is by default disabled this is enabled after adding text in the text box so, this can be done at run time as

 private void textBox1_TextChanged(object sender, EventArgs e)
        {
            if (textBox1.Text.Length > 0)
            {
                button1.Enabled = true;
            }

        }

 

Note: The text of rich text box is obtain in this form by making the object of the rich Textbox at the form load as shown bellow:

RichTextBox rt;
        public static Button bt;
        private void Form2_Load(object sender, EventArgs e)
        {
            textBox1.Focus(); 
            rt = Form1.rt;
        }

Now the code behind the Find Next button of Find form is given as :

        int m;
        int end;

        private void button1_Click(object sender, EventArgs e)
        {
            if(rt.SelectionStart==rt.Text.Length)
            {
                m = rt.SelectionStart;
            }



            bt = button1;
            if(radioButton2.Checked)
            {
                rt.SelectionStart = m;
                m = rt.SelectionStart;
                end=rt.Text.Length+1;
            }
            else
            {
                rt.SelectionStart = m;
                m = rt.SelectionStart;
                end = -1;
            }
            
            //    m = rt.SelectionStart;
                if (checkBox1.Checked)
                {
                    int j = rt.Find(textBox1.Text, m, end, RichTextBoxFinds.MatchCase);
                    if (j >= 0)
                    {
                        m = j + textBox1.Text.Length;
                       
       
                        rt.Focus();
                    }
                    else
                    {

                        MessageBox.Show(string.Format("can't find \"{0}\"", textBox1.Text));
                        m = 0;
                    }

                }
                else
                {
                   

                    int i = rt.Find(textBox1.Text,  m,end, RichTextBoxFinds.None);
                    if (i >= 0)
                    {

                        m = i + textBox1.Text.Length;
                       
       
                        rt.Focus();
                    } 
                    else
                    {
                        MessageBox.Show(string.Format("can't find \"{0}\"", textBox1.Text));
                        m = 0;
                    }
                }
            
            
        }

 

part-3_picture-5

Replace: The next option in the Edit Menu is Replace in which the specific word is first searched in the text and then replace by some other word. For this we have to add new form  in the project like we add for find. The replace form in Microsoft Notepad is shown as:

part-3_picture-6

To provide the same functionality add new form in the project and name it as “Replace”, Now add two label with text “Find what” and “Replace with” ,add two textboxes, four button and a check box .

Our replace form look like as :

part-3_picture-7

Now make the object of this form in the handler of click event of replace menu item as shown below:

private void replaceToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form3 ob=new Form3();
            ob.Show();
   }

 

Now open the Replace form and code the following lines of c# in the backend of the form

public partial class Form3 : Form
    {
        public Form3()
        {
            InitializeComponent();
        }

       private void Form3_Load(object sender, EventArgs e)
        {
            textBox1.Focus();

            rt = Form1.rt;
            
        }

      
        RichTextBox rt;
        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            
        }
        int m;
        private void findnext_button_Click(object sender, EventArgs e)
        {
        
            if (checkBox1.Checked)
            {
                int j = rt.Find(textBox1.Text,m,rt.Text.Length+1, RichTextBoxFinds.MatchCase);
                if (j >= 0)
                {
                    m = j + textBox1.Text.Length;
                    rt.Focus();
                }
                else
                {

                    MessageBox.Show(string.Format("can't find \"{0}\"", textBox1.Text));
                }

            }
            else
            {


                int i = rt.Find(textBox1.Text,m,rt.Text.Length+1,RichTextBoxFinds.None);
                if (i >= 0)
                {
                    m = i + textBox1.Text.Length;
                    rt.Focus();
                }
                else
                {
                    MessageBox.Show(string.Format("can't find \"{0}\"", textBox1.Text));
                }

            }
        }
        
   

        private void replacebutton_Click(object sender, EventArgs e)
        {
            if (rt.SelectedText.Length > 0 )
            {
                rt.SelectedText = textBox2.Text;
                button1.PerformClick();
            }
        }

        private void cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void replaceall_Click(object sender, EventArgs e)
        {
            while( (rt.SelectedText.Length > 0 ))
            {
                rt.SelectedText = textBox2.Text;
                button1.PerformClick();
            }

        }
}

 

The above lines of code preform the complete functioning of find and replace as performed in the Microsoft notepad.

5. Go To : The next option in the file menu item is Go To , in this  we enter the line number where we want to shift the cursor, for this purpose we again add a new form form 4 or Go To form with one text box,

one label and one button as shown bellow:

part-3_picture-8

 

Now as previous cases make the object  of this form and invoke the show function in the handler of the click event of Go TO  Menu Item in the main form as

private void goToToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form4 ob = new Form4();
            ob.Show();

        }

//The code behind the Go to form or form 4 is given as :
public partial class Form4 : Form
    {
        public Form4()
        {
            InitializeComponent();
        }
        RichTextBox rt;
        int line;
        private void Form4_Load(object sender, EventArgs e)
        { 
            textBox1.Focus();
            rt = Form1.rt;
            line = 1 + rt.GetLineFromCharIndex(rt.GetFirstCharIndexOfCurrentLine());
        }

        private void button1_Click(object sender, EventArgs e)    // go to button handler 
        {
            line = int.Parse(textBox1.Text);
                        rt.SelectionStart = line;
            this.Close();
        }
  }

 6. Find On web: The next option in the file menu it is find the selected text on the web either google.com or on the bing.com. By this we can find the selected text directly on Google and Bing search engine if internet connection is provided. To code this we use the process class provided in the C# and by using some of its property the Google and Bing can be opened directly in the your default web browser.

The code for Google Menu item is given as:

private void googleToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Process proc = new Process();
            if (richTextBox1.SelectedText.Length > 0)
            {
                proc.StartInfo.FileName = "http://www.google.com/search?q=" + richTextBox1.SelectedText;
                proc.Start();
            }
            else
            {
                MessageBox.Show("Please select text for search on Google");
            }
  }

The code for Bing menu item is given as:
private void bingToolStripMenuItem_Click(object sender, EventArgs e)
        {
           Process proc = new Process();
            if (richTextBox1.SelectedText.Length > 0)
            {
                proc.StartInfo.FileName = "http://www.bing.com/search?q=" + richTextBox1.SelectedText;
                proc.Start();
            }
            else
            {
                MessageBox.Show("Please select text for search on BING");
            }
            
        }
      

 7. Select All: The next menu item is select all , the code for this is very simple because the select function is given for reich text box in C#. the code behind handler of Select all button click is given bellow:

  private void selectAllToolStripMenuItem_Click(object sender, EventArgs e)
        {
            richTextBox1.SelectAll();
        }

 8. Time/Date: The next option in edit is time and date, the functionality of this potion is very simple it put the current date and time at point where the pointer resides in the file. The code for this is given bellow:

private void timeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            richTextBox1.Text += DateTime.Today.ToShortDateString();
        }

9. Convert Case to: The next option is to convert the case of selected  text either in upper case  or in lower case ,for this the rich textbox provides the function Toupper and ToLower which we use in our code as shown below:

           private void upperCaseToolStripMenuItem_Click(object sender, EventArgs e)
        {
            richTextBox1.SelectedText = richTextBox1.SelectedText.ToUpper();
        }

        private void lowerCaseToolStripMenuItem_Click(object sender, EventArgs e)
        {
            richTextBox1.SelectedText = richTextBox1.SelectedText.ToLower();
        }

Note: Some of the option in edit menu item is initially disabled and enabled when we enter the text in our notepad or select the existing text. This can be handled in the handler of drop down opening event as shown below:

private void editToolStripMenuItem_DropDownOpening(object sender, EventArgs e)
        {
            if (richTextBox1.Modified)
            {
                 replaceToolStripMenuItem.Enabled = findToolStripMenuItem.Enabled = undoToolStripMenuItem.Enabled = true;
         //       isundo = true;
            
                
            }

            findOnWebToolStripMenuItem.Enabled = convertCaseToolStripMenuItem.Enabled = deleteToolStripMenuItem.Enabled = cutToolStripMenuItem.Enabled = copyToolStripMenuItem.Enabled = richTextBox1.SelectedText.Length > 0;
 }

 

This completes the code for each option in Edit menu item. The rest menu items will be discussed in the next article.

Your valuable feedback and suggestion would be appreciated.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

CommentLuv badge