In this article you fill find information about how to implement the basic functions of the Notepad using c#
Visual Studio provides a good interface for creating different Windows Form Applications. One of the most popular application that is created during the studying is Notepad.
In this article I will not pay a lot of attention on the process of building GUI, because it's a simple task. I just give you one important tip on this step:
Always give proper names to your GUI elements. The Visual Studio generates names automatically, but you can always change the name of any component in the property window. It will help you very much in the process of constructing the application.
And for these, how is new to Visual Studio - to handle an event from pressing the element just double click on this element in the form's constructor. A function will be generated automatically and you will have only to set the actions to handle the event.
In the top of the Notepad always exists a menu. We will start implementing the Notepad's functions from the menu "File":
New function. To create a new file first step is to ask if the user wants to save changes on the current file. For this you can show a message dialog. And if the user wants to save changes, call the saveFile function:
DialogResult result = MessageBox.Show("Do you want to save changes to the current file?", "Warning", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning);
if (result == DialogResult.Yes)
{
if (currentFile != null)
saveFile();
else
saveFileAs();
}
else if (result == DialogResult.Cancel)
{
//code for Cancel
return;
}
After file is saved we can create a new file and empty the text area. You need to disable "save" and "print" buttons because the file is not existing in the file system. It has just a representation in the textBox. Save and print buttons will be enables only after you call save_as function:
textBox1.Text = "";
saveBtn.Enabled = false;
saveToolStripMenuItem.Enabled = false;
printBtn.Enabled = false;
printToolStripMenuItem.Enabled = false;
currentFile = null;
As you see, I didn't wrote the full list of components in the project. But all of the variables have the understandable names and saveBtn - means save Button, for example.
The next item in menu is "open file". This option opens a txt file and shows it in the textBox. First step it's to show openFile Dialog. In my Notepad I open only txt files. To filter the other file from txt files a
Filter
is set to the dialog. And the initial directory for the dialog is "c:\\"
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = "c:\\";
openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
openFileDialog1.FilterIndex = 2;
openFileDialog1.RestoreDirectory = true;
Now, you can open the selected file and load it to the text box:
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
currentFile = openFileDialog1.FileName;
System.IO.StreamReader sr = new System.IO.StreamReader(currentFile);
textBox1.Text = sr.ReadToEnd();
//enable save and print option
saveBtn.Enabled = true;
saveToolStripMenuItem.Enabled = true;
printBtn.Enabled = true;
printToolStripMenuItem.Enabled = true;
this.Text = currentFile + " Notepad";
sr.Close();
}
The save and save_as function are very similar. The main difference is that save button is enabled only when you opened a file or already saved file. The save file function is similar in the construction with the open file, you do the same as when you open a file, but you don't read data - you write it:
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.InitialDirectory = "c:\\";
saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
saveFileDialog1.FilterIndex = 2;
saveFileDialog1.RestoreDirectory = true;
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
currentFile = saveFileDialog1.FileName;
this.Text = currentFile + " Notepad";
saveFile();
}
saveBtn.Enabled = true;
saveToolStripMenuItem.Enabled = true;
printBtn.Enabled = true;
printToolStripMenuItem.Enabled = true;
The print menu just show the print file dialog, but it's not printing anything:
PrintDialog printDlg = new PrintDialog();
PrintDocument printDoc = new PrintDocument();
printDoc.DocumentName = currentFile;
printDlg.Document = printDoc;
printDlg.AllowSelection = true;
printDlg.AllowSomePages = true;
//Call ShowDialog
if (printDlg.ShowDialog() == DialogResult.OK)
printDoc.Print();
And the exit item just asks user if he really wants to exit. If the answer is yes - application is closed, and in case of "no" nothing happens:
DialogResult result = MessageBox.Show("Do you really want to exit?\n All the current changes will be lost", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
if (result == DialogResult.Yes)
{
this.Close();
}
else if (result == DialogResult.No)
{
//code for Cancel
return;
}
The "Format" menu changes the size and the font of the text in the text box:
this is performed by functions:
private void fontBox_SelectedIndexChanged(object sender, EventArgs e)
{
textBox1.Font = new Font(fontBox.SelectedItem.ToString(),textSizeValue);
}
and:
private void toolStripComboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
textSizeValue = int.Parse(sizeBox.SelectedItem.ToString());
textBox1.Font = new Font(fontBox.SelectedItem.ToString(), textSizeValue);
lineNumber.Font = new Font(fontBox.SelectedItem.ToString(), textSizeValue);
}
FontBox and SizeBox have a list of values of the font's and integer numbers. So, to set the font just the string representation of the font's name is passed to the constructor. But in the case of sizeBox you need to parse string representation of number to integer value.
lineNumber - is a textArea used to count lines in the text. I'll speak about my solution to count lines in a textBox in the next part of article. Also, in the next part you will be able to learn how to perform basic copy/paste/delete/cut operations on the text box and how to create your own undo and redo commands.