In this paper, the content

Simple notepad (code) with C# language.

Thought analysis

The functional requirements

Refer to win10 notepad, the implementation of simple notepad should have the following menu controls:

It also includes:

To realize the order

Considering that even simple notepad needs to achieve more functions, so you can take the first implementation of notepad (interface) framework, and then gradually fill the logical code to complete the task in order.

The advantage of doing this is that you can refine the code against the interface and are less prone to omissions.

The specific implementation

Interface design

In fact, win10 built-in notepad interface slightly modified, can become the interface template. All you need to do next is reverse engineer the existing interface into the components in C#.

Create a New Windows Forms application and change the text in the lower-right property to “Notepad” to maximize the outer border.

MenuStrip (Menu control)

This step implements each menu option.

First find the Menustrip in the toolbox and drag it to the form, and change the Name in the property in the lower right corner to “mnusNotepad” (note: change the Name here for convenience of subsequent manipulation, Name can be freely defined, the same below).

The next step is to fill each of the controls shown above into the Menustrip.

It looks a lot and different, but the approach is basically the same. Click on menustrip and enter File (&f), Edit (&e), Format (&O), View (&V), and Help (&H), where “&” acts as the underscore below the implementation letter. After the input is completed, what is actually completed is the first-level menu, that is, the menu at the level of the first line of “Notepad” in the picture.

At this point, you should also continue to click on the level 1 category menu and enter the level 2 menu (which is the part of the drop-down box in the figure). Take the secondary menu under “File” as an example, click “File”, respectively enter: New (&n), open (&O), save (&s), save as (&A), enter “-” to generate A split line and exit (&x).

The following figure shows the specific attributes:

Click each control in turn to modify the property.

At this point, the menu bar frame is set.

ToolStrip (Toolbar controls) (not recommended)

Find “ToolStrip” in the toolbox and drag it into the form. Change the Name in the genus to “tlsNotepad”.

Right click the toolbar and select Insert standard items to complete the layout of the graph.

The result is shown in figure. But the current notepad actually does not have this toolbar, and this toolbar also affects the overall appearance, function repetition, so it is not recommended to add in the program.

RichTextBox (multi-format text box control)

Again, find “RichTextBox” in the toolbox, drag it to the form, and change the Name to “rtxtNotepad” in the property.

Also, set the Anchor property to “Top, Bottom, Left, Right” so that the RichTextBox control changes size when the form changes size.

The text box implemented here is the white part of the image used to edit text in it.

StatusStrip (Status bar control)

Add the StatusStrip control with its Name property set to “stsNotepad”, Dock property set to “Bottom” and Anchor property set to “Bottom, Left, Right”. In fact, here the Anchor property will eventually automatically change to “Top, Left”.

This setting also allows the status bar at the bottom to change as the box size changes.

Right click to open the “Item Set Editor” dialog box, keep the default “StatusLabel” in the drop-down list, and then click the “Add” button to add two StatusLabels and name them “tssLbl1” and “tssLbl2” respectively. Then set the Text property of tssLbl1 to “ready” and the Text property of tssLbl2 to “Show date and time”.

If you want to achieve the effect in the figure, you need to further set the padding or margin attribute values of two StatusLabels. By default, the two StatusLabels are attached to each other. It is also important to note that “Show date, time” is actually a placeholder for subsequent time displays.

Each dialog

OpenFileDialog (open dialog)

When the user clicks the [File] → [Open] menu item of Notepad, open the file using the Open dialog box OpenFileDialog. OpenFileDialog controls the Name attribute of the “odlgNotepad”, the Filter attribute set to “| RTF files. All files RTF | |. *”.

SaveFileDialog

When the user clicks the “File → save” (or “Save as”) menu item of Notepad, use the save dialog box SaveFileDialog to save the file. SaveFileDialog controls the Name attribute of the “sdlgNotepad”, FileName attribute to “no title”, the Filter attribute set to “RTF file | *. The RTF”.

FontDialog

When the user clicks the [format] → [font] menu item of notepad, use the FontDialog box FontDialog to set the text font. The Name property of the FontDialog control is “fdlgNotepad”.

Timer control

The Notepad described in this chapter displays the clock in the status bar, using a Timer control to do this. Set the Name attribute of the Timer control to “tmrNotepad”, the Enabled attribute to “True”, and the Interval attribute to “1000”, indicating that the Tick event is triggered once every second, that is, the clock is changed once every second.

About Notepad

Click the Add Windows Forms menu item of VS project, add a form named “frmAbout”, and add several Label, Button, LinkLabel and PictrueBox controls to the form “frmAbout”. Set the properties according to the following information.

After completion, the following controls should be displayed:

At this point the user interface design is complete.

Logic code

Click on Gitee to see the code

First, the generic section in the code declares the following two Boolean public variables, “B” to determine whether the file is new or opened from disk, and “S” to determine whether the file is saved. Then complete the multi-format text box (rtxtNotepad), menu (mnusNotepad), toolbar (tlsNotepad), timer (tmrNotepad) object logic code.

Due to lack of space, I won’t cover all of them here, but the Gitee source code contains annotations. Here select “new” function of the program for supplementary explanation.

Private void tsmiNew_Click(object sender, EventArgs e) {private void tsmiNew_Click(object sender, EventArgs e) { Documents have not been saved and the if (b = = true | | rtxtNotepad. Text. The Trim ()! If (s == false) {string result; Result = messagebox.show (" file not saved, save?") , "save file", MessageBoxButtons YesNoCancel). The ToString (); switch (result) { case "Yes": If (b == true) {rtxtNotepad.savefile (odlgNotepad.filename); } else if (sdlgNotepad.showdialog () == dialogresult.ok) {rtxtNotepad.savefile (sdlgNotepad.filename); } s = true; rtxtNotepad.Text = ""; break; case "No": b = false; rtxtNotepad.Text = ""; break; }}}}Copy the code

In fact, the code is almost done “new”, but there is a small bug. The code takes into account the “new document is not empty, and the file is not saved” case.

If you have just opened and entered the text, there will be no problem. But if this is the first click “open”, edit existing originally after the RTF file and save (or do not edit), then click “new” cases, code execution effect is not satisfactory: the program will not generate a new text box for the user to edit, user operation is still the previous open source file.

To code a little before and after analysis can know, due to “save” and “save as” command last variable s will be set to true, then after open a saved file, “open” command code does not meet the if (s = = false) this condition, so the whole piece of code does not perform, there would be no new text box also generated.

This bug is not difficult to fix, just need to fix s == true case.

Private void tsmiNew_Click(object sender, EventArgs e) {private void tsmiNew_Click(object sender, EventArgs e) { Documents have not been saved and the if (b = = true | | rtxtNotepad. Text. The Trim ()! If (s == false) {string result; Result = messagebox.show (" file not saved, save?") , "save file", MessageBoxButtons YesNoCancel). The ToString (); switch (result) { case "Yes": If (b == true) {rtxtNotepad.savefile (odlgNotepad.filename); } else if (sdlgNotepad.showdialog () == dialogresult.ok) {rtxtNotepad.savefile (sdlgNotepad.filename); } s = true; rtxtNotepad.Text = ""; break; case "No": b = false; rtxtNotepad.Text = ""; break; } } else { rtxtNotepad.Text = ""; b = false; }}}Copy the code

Else {rtxtNotepad.Text = “”; b = false; } take s == true into account when b = false; Rtxtnotepad. Text = “”; Empty the text box so that you can create a new file.

Here is part of the execution:

That’s all for this article.