Simple Notepad Application using C# - Part 2
Submitted by pavel7_7_7 on Wednesday, February 12, 2014 - 06:43.
This is the second part of the article about C# Notepad.
In the first part of this tutorial we already created a simple Notepad with possibilities to open, save, save as and print file. Also, you can change now the font and it's size from the menu.
The next step is to implement the basic operations in the text redactor:
copy, paste and cut. This actions can be performed in a very simple way by using functions of the textBox class. So, to copy a sequence just call copy function:
The paste and cut functions can be performed in the same way:
This is really very simple.
Now we will implement a find option. For this we need a new form to input a word for search. For these reason a new form is created:
The implementation of this form is really simple. We just need to get the word for search from the word:
And now in the main form class we can call this form to get word for search:
And now just highlight the found words in the text box:
The words are found by one so you will need to press next button when you want to get the next found word.
The replace function is similar to the find function. We need to get a word that will be replaced by a new word. For this a new form is implemented:
As you see, we need two text boxes and two buttons. This form is implemented in the next way:
And now we can call this form from the main form of the notepad:
As you see, textBox class has a build in function -
The shortcut keys can be set in the property window for every control:
The process of building a Notepad is very simple, because textBox has a variety of functions to perform automatically a lot of options. In the list if this options are undo and redo. But my task was to implement them manually. So, I held all the changes of the text in a vector and on undo/redo I get the last change of the text. Here is the implementation of these functions.
UNDO:
REDO
Of course, There is no need to implement these function and you can simply call them from the textBox object, but it's a good example of the fact that functions from the classes can save a lot of your time and energy.
You can download the source code of this project and try it by yourself.
- private void copyToolStripMenuItem_Click(object sender, EventArgs e)
- {
- textBox1.Copy();
- }
- private void pasteToolStripMenuItem_Click(object sender, EventArgs e)
- {
- textBox1.Paste();
- }
- private void cutToolStripMenuItem_Click(object sender, EventArgs e)
- {
- textBox1.Cut();
- }
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- namespace Notepad
- {
- public partial class findForm : Form
- {
- String findWord;
- public findForm()
- {
- InitializeComponent();
- }
- private void cancelBtn_Click(object sender, EventArgs e)
- {
- findWord = "";
- this.Close();
- }
- private void okBtn_Click(object sender, EventArgs e)
- {
- findWord = findText.Text;
- this.Close();
- }
- public String getFindWord()
- {
- return findWord;
- }
- }
- }
- findForm f1 = new findForm();
- f1.ShowDialog();
- if (f1.getFindWord() != "")
- {
- int index = 0;
- while (index != -1 && index < textBox1.Text.Length)
- {
- // Searches the text in a RichTextBox control for a string within a range of text withing the control and with specific options applied to the search.
- index = textBox1.Text.IndexOf(f1.getFindWord(), index);
- if (index != -1)
- {
- textBox1.Select(index, f1.getFindWord().Length);
- // After a match is found the index is increased so the search won't stop at the same match again. This makes possible to highlight same words at the same time.
- index++;
- f1.ShowDialog();
- }
- }
- }
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- namespace Notepad
- {
- public partial class replaceForm : Form
- {
- public String findWord;
- public String replaceWord;
- public replaceForm()
- {
- InitializeComponent();
- }
- private void replaceBtn_Click(object sender, EventArgs e)
- {
- findWord = findText.Text;
- replaceWord = replaceText.Text;
- this.Close();
- }
- private void cancelBtn_Click(object sender, EventArgs e)
- {
- findWord = "";
- replaceWord = "";
- this.Close();
- }
- }
- }
- private void Replace()
- {
- replaceForm r1 = new replaceForm();
- r1.ShowDialog();
- String findWord = r1.findWord;
- String replaceWord = r1.replaceWord;
- if (findWord != "")
- {
- textBox1.Text = textBox1.Text.Replace(findWord, replaceWord);
- MessageBox.Show("Done", "Done", MessageBoxButtons.OK);
- }
- else
- MessageBox.Show("Nothing to replace", "Done", MessageBoxButtons.OK);
- }
Replace
So, you task is very simple: get findWord and replaceWord and call Replace function.
The notepad functions are almost implemented, but you have to think about the users of the your product. Every Notepad has shortcut keys for the main function. The same thing has this Notepad. You can see the list of shortcuts by pressing help menu:
The information is shown in the text box:
- private void helpToolStripMenuItem_Click(object sender, EventArgs e)
- {
- MessageBox.Show("Shortcuts for notepad \n ctrl+n - new file\n
- ctrl+o - open file\n ctrl+p - print file\n ctrl+e - exit\n
- ctrl+f - find\n ctrl+r - replace\n f1 - help\n", "Help", MessageBoxButtons.OK, MessageBoxIcon.Information);
- }
- private void myUndo()
- {
- //adding current text to redo
- redoList.Add(textBox1.Text);
- redoBtn.Enabled = true;
- redoToolStripMenuItem.Enabled = true;
- //get last change and set text in text box
- textBox1.Text = (String)undoList[undoList.Count - 2];
- undoList.RemoveAt(undoList.Count - 1);
- undoList.RemoveAt(undoList.Count - 1);
- if (undoList.Count == 1)
- {
- undoBtn.Enabled = false;
- undoToolStripMenuItem.Enabled = false;
- }
- }
- private void myRedo()
- {
- textBox1.Text = (String)redoList[redoList.Count - 1];
- redoList.RemoveAt(redoList.Count-1);
- if (redoList.Count == 0)
- {
- redoBtn.Enabled = false;
- redoToolStripMenuItem.Enabled = false;
- }
- }
Comments
Add new comment
- Add new comment
- 1320 views