“This article has participated in the call for good writing activities, click to view: the back end, the big front end double track submission, 20,000 yuan prize pool waiting for you to challenge!”

Based on C# development of a notepad software, including notepad file new, open, read, save, text bold, tilt, font, size, notepad batch operation, etc..

directory

  • Note book form

  • Gets the system font function

  • Font space function

  • Size space function

  • Bold control functions

  • Tilt control function

  • Save control function

  • Open file control function:

  • Create a new text control function

  • Form close control function

  • Master page parent form

  • New Notepad

  • Closed form

  • Close all forms

  • Out of control

Hello! Big bad Wolf again to share with you worth learning C# development project, today and you should share a familiar development project – “notepad”.

Many people have probably used a notebook to record important things or things they forget easily, but do you know how it was developed?

The notepad project can also be developed in many different languages. Today, the big bad Wolf will first introduce the idea and process of using C# to develop notepad.

At the same time for Java and C# in GUI programming similarities and differences, partners can read my previous article “Java interactive interface implementation calculator development design [attached function source]”,

All right, without further ado, let’s get right to the tutorial.

Notepad development in C# requires us to define two forms, that is, a parent form that hosts all notepads and a child form that has the basic functions of notepad.

.

Note book form

So let’s start by talking about the development of a subform with notepad’s basic functionality.

In C# forms applications, we can directly control the layout of the interface, do not need to use code to define controls, C# forms application development in this point is very good.

So in notepad application, we should add controls in the form should have, file new, open, save, and font style, size, whether bold tilt these basic Notepad controls. At the same time, of course, also need notepad edit text box, convenient for us to modify and input the content.

With these basic controls in mind, it’s time to write the functions that each control is bound to,

Gets the system font function

First of all, we need to get all the font styles of the system when setting the font.

Through the following functions:

Private void FormSon_Load(object sender, EventArgs e) {InstalledFontCollection MyFontStaly = new InstalledFontCollection(); FontFamily[] ff = MyFontStaly. Family; ArrayList list = new ArrayList(); Int count = ff.length; for (int i = 0; i < count; String FontName = ff[I].name; // Save the font in the font control toolstripcombobox_fonystaly.items.Add(FontName); }}Copy the code

Font space function

After obtaining all the font styles that can be set by the system, it is the control that changes the font when we click the font. Here we need to set it in the control that changes the font index.

The function is as follows:

Private void toolStripComboBox_fonyStaly_SelectedIndexChanged(object sender, String fontName = ToolStripcombobox_fonystaly.text; Parse(toolstripcombobox_fontsize.text); // Accept the value of the current size box float fontsize = float. textBox_Text.Font = new Font(fontname, fontsize); }Copy the code

Size space function

After setting the property of the font control, it is the size property. Similar to setting the idea of the font control, we also need to get the index to change the size first, the function is as follows:

Private void toolStripComboBox_fontSize_SelectedIndexChanged(Object sender, String fontName = ToolStripcombobox_fonystaly.text; Parse(toolstripcombobox_fontsize.text); // Accept the value of the current size box float fontsize = float. textBox_Text.Font = new Font(fontname, fontsize); }Copy the code

When the size changes, we need to change the size of the content in the text box. At this time, we need to set it in the corresponding function:

Private void toolStripComboBox_fontSize_TextChanged(object sender, String fontName = ToolStripcombobox_fonystaly.text; Parse(toolstripcombobox_fontsize.text); // Accept the value of the current size box float fontsize = float. textBox_Text.Font = new Font(fontname, fontsize); }Copy the code

Bold control functions

Private void toolStripButton_over_Click(object sender, EventArgs e) {private void toolStripButton_over_Click(object sender, EventArgs e) { If (textbox_text.font.Bold == false) {textbox_text.font = new Font(textbox_text.font, fontstyle.bold); Else {textbox_text.font = new Font(textbox_text.font, fontstyle.regular); }}Copy the code

Tilt control function

// Private void toolStripButton_slanr_Click(object sender, EventArgs e) { if (textBox_Text.Font.Italic == false) { textBox_Text.Font = new Font(textBox_Text.Font, FontStyle.Italic); } else { textBox_Text.Font = new Font(textBox_Text.Font, FontStyle.Regular); }}Copy the code

Save control function

When doing the save controls, we need to consider, is what we save the file information, whether the new text boxes, such as we are a new notepad, are preserved when you need to choose to save the path and file name, if we are to the original file for the secondary editor, is only when we click the save button to save the can, You do not need to change the file saving path.

The function code is as follows:

Private void toolStripButton_save_Click(object sender, EventArgs e) {/ / create a Filter saveFileDialog1. Filter = (" text documents (*, TXT) | *. TXT "); If (textbox_text.text.trim ()! // If the notepad file is new, Select path to save the if (this. Text = = "notepad") {/ / save the file to the user specified directory / / users is to save or cancel the if (saveFileDialog1. ShowDialog () = = DialogResult. OK) { String path = savefiledialog1.filename; StreamWriter sw = new StreamWriter(path, false); sw.WriteLine(textBox_Text.Text.Trim()); // Display path this.Text = path at the top of the form; // Flush the cache sw.flush (); sw.Close(); Else {// Save the file to the specified directory of the user // Get the file selected by the user and its path string path = this.text; StreamWriter sw = new StreamWriter(path, false); sw.WriteLine(textBox_Text.Text.Trim()); sw.Flush(); sw.Close(); Else {MessageBox.Show(" The current text box is empty! \n Cannot be saved!" , "warning "); }}Copy the code

Open file control function:

In the open operation of the file, we need to open the file screening, only open TXT format text files.

The implementation function is as follows:

Private void toolStripButton_open_Click(object sender, EventArgs e) {/ / screening of an open file openFileDialog1. Filter = (" text documents (*, TXT) | *. TXT "); if (openFileDialog1.ShowDialog() == DialogResult.OK) { string path = openFileDialog1.FileName; StreamReader sr = new StreamReader(path, Encoding.UTF8); string text = sr.ReadToEnd(); // Display the path this.Text = path at the top of the Text box; textBox_Text.Text = text; sr.Close(); }}Copy the code

Create a new text control function

After clicking New text, we need to clear out the original edit and re-create the edit.

The function is as follows:

Private void toolStripButton1_Click(Object Sender, EventArgs e) {this.Text = "Notepad "; textBox_Text.Text = ""; toolStripTextBox1.Text = ""; }Copy the code

Form close control function

After our text is modified, if the user clicks the close button, the user needs to be prompted at this time. If the user chooses close, the notepad text box will be closed without saving. If the user clicks no, the user will return to the editing page for editing.

The function is as follows:

Private void FormSon_FormClosing(Object Sender, FormClosingEventArgs e) {// If the file has been modified, If (toolstripTextBox1.text == "Text modified ") {DialogResult Dr = messagebox.show (" Text modified! Will you continue to withdraw?" , "prompt ", messageBoxbuttons. YesNo, messageboxicon. Question); // If (Dr == dialogresult.yes) {this.dispose (); } // If the user chooses no, return page else {e.camel = true; }}}Copy the code

After these functions are completed, our Notepad text editing window is complete. Look at the effect:

.

Master page parent form

Next comes the parent form that hosts the Notepad editing window.

The controls that need to be included in the parent form are new, close, all close, exit of notepad and so on.

New Notepad

To set the properties of the new Notepad control, we simply call the notepad form we just completed.

The function is as follows:

Private void ToolStripMenuItem_newfile_Click(object sender, EventArgs e) {// Define a variable to receive child form FormSon son = new FormSon(); // Define the parent of the child window son.mdiparent = this; // Display the child form son.show (); }Copy the code

Closed form

When we close the form, we close the active form that is currently at the front.

The function is as follows:

Private void ToolStripMenuItem_closefile_Click(Object sender, EventArgs e) {// Receive the current Form Form = this.activemdichild; // Close the current form form.close (); }Copy the code

Close all forms

Clicking Close All forms will close all Notepad forms that are already open.

The function is as follows:

Private void ToolStripMenuItem_closeall_Click(object sender, Foreach (Form Form in this.mdichildren) {Form FRM = this.activemdichild; foreach (Form Form in this.mdichildren) {Form FRM = this.activemdichild; frm.Close(); }}Copy the code

Out of control

The last thing that needs to be set is exit, which closes the Notepad form as a whole when clicked. Therefore, we only need to close the currently open form.

The function is as follows:

// Exit private void ToolStripMenuItem_quit_Click(object sender, EventArgs e) {this.close (); }Copy the code

After setting up the parent form, the overall development of Notepad is complete. Now take a look at the overall effect.

Remember to click “like” to follow the big bad Wolf!

You can click here to download the “source code”, or pay attention to the big bad Wolf wechat public number “grey Wolf hole master” reply “C# notepad” can obtain the complete source code!

Big bad Wolf accompany you to progress together!