This article introduces how to use Visual C# 2015 to design a Windows application — notepad. Learn more about MenuStrip (menus), ToolStrip (toolbars), RichTextBox (advanced text boxes), and StatusStrip (status bar controls). And how to use CommonDialog to access and format text. (Note: all code must be used in English!) 1.1 Introduction to Notepad Notepad is a common software. Microsoft Windows provides a Notepad. Figure 1 shows the Notepad in Windows 10.

The notepad introduced by c# realizes some functions of the notepad in Windows, and can also arbitrarily change the font type, size and color, and display the time in the status bar. In order to facilitate the user’s operation, a toolbar is also placed on the form of the program. The Notepad program introduced in this chapter has the functions of creating, opening and saving files. Text copy, paste, delete functions; Font type, format setting function; View the date and time, and the user can show or hide the toolbar and status bar as needed. The following will be detailed introduction of notepad program design and implementation steps and methods. 1.2 Notepad Interface Design Create a Windows Forms application and name it Notepad. This section introduces the interface design of Notepad program and the property setting of each control on the interface. 1. Open VS2013 and click File > New > Project

2. Select template → Visual C# → Windows → Windows forms application write Notepad under the name below

3 Change the form name Click the form, → properties in the lower right corner →text Change it to “Notepad” as shown in the figure

Control Type Control Name Property Setting Result Form Form1, Name frmNotepad, Text Notepad, StartPosition CenterScreen, After creating the “Notepad” project, locate it in the Form designer window of Notepad program, and then place the following controls (one for each) on the form: (1) MenuStrip (menu control). Click on the left side of the form to appear toolbox → find menuStrip → drag to form

(2) Click the menu bar → Properties in the lower right corner → Modify the Name property to “mnusNotepad”

(3) Click and input [File (F)], [Edit (E)], [format (O)], [View (V)] and [Help (H)] as shown in the figure

(4). Set the file (F)], [edit (E)], [format (O)], [check (V)] and attributes – > click the help (H) 】 【 【 file (F)], [edit (E)], [format (O)], [check (V)], and [help (H)] find corresponding properties window, set according to the following table

(2) ToolStrip (toolbar controls). Click on the left side of the form and the toolbox will appear → find “ToolStrip” → drag into the form and click on the toolbar → Properties in the lower right corner → Modify the Name property to “tlsNotepad” as shown in the figure

Right click toolbar → Insert standard items as shown in the figure

Right click toolbar → Edit item to see our button members here we can delete or add, for beginners to learn, we only set a few simple functions (new, open, save, cut, paste, copy). We won’t set anything else.

For the sake of intuition, we do not change their Name attribute. Next, according to the needs of the program itself, add and delete the standard tool subitems, after setting the attributes of each subitem.

(3) RichTextBox (multi-format text box control). Click on the left side of the form and the toolbox will appear → find “RichTextBox” → drag to the form and click the toolbar → properties in the lower right corner → modify the Name property to “rtxtNotepad” as shown in the figure

Select “Top, Bottom, Left, Right” for the Anchor property so that when the form size changes, the size of the RichTextBox control changes, as shown in Figure 2.1.

(4) StatusStrip (status bar control) Add the StatusStrip control, set its Name property to “stsNotepad”, Dock property to “Bottom” and Anchor property to “Bottom, Left, Right”. Then click the button on the right to open the item Set Editor dialog box, as shown in the figure.

Keep the default option “StatusLabel” in the drop-down list, and then click “Add” button to add two statusLabels in turn and name them “tssLbl1” and “tssLbl2” respectively. Then set the Text attribute of tssLbl1 to “Ready”. The Text property of tssLbl2 is set to “Show date and time”.

(5) OpenFileDialog 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 filesAll files RTF | |. * “.

(6) Save dialog 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”.

(7) 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”.

(8) 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. The final setting interface is as shown in the figure below. The user interface design is completed. Next, the specific implementation process and the preparation of source code are introduced.

1.3 Writing program code First declare the following two public variables in the general section of the code. They are both Boolean, “B” to determine whether the file is new or opened from disk, and “S” to determine whether the file is saved. Double-click on the form, enter the code to fill in the following code / / * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * / * Boolean variable used to judge the file b is newly built and opened from the disk, False indicates that the file is created. The default value is false*/ bool b = false. /* bool s = true; /* bool s = true; / / * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * as shown:

Next introduced multi format text box (rtxtNotepad), menu (mnusNotepad), toolbar (tlsNotepad), timer (tmrNotepad) object program code. When the text in the multi-format text box is changed, the value of the Boolean variable “S” should be set to false, indicating that the file is not saved. Therefore, the code of the multi-format text box TextChanged event is written as follows: Double-click the RichTextBox, Enter the area code to fill in the following code / / * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * / / rich text box the TextChanged event code //************************************************************************* private void rtxtNotepad_TextChanged(object Sender, EventArgs e) {// Set s to false, indicating that the file is not saved. } / / * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * / / * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * as shown:

(2) Menu code In the Notepad program introduced in this chapter, almost all functions are realized through menus. This section will introduce the click event codes of each menu item.

  1. [File (F)] menu

[File (F)] The function of the menu is to complete the function of creating, opening, saving and saving files, and exit the Notepad program. The source code of each menu item is described in detail below.

(1) [New (N)] Menu item When you click this menu item to create a blank document, you should first determine whether the file is opened from disk or new. If the file is opened from disk, set the variable (b) previously defined to “true”, otherwise set it to “false”. Doing so allows you to do something to the file based on this variable. And each time the file is saved, set the previously defined variable (s) to “true”, indicating that the file has been saved. When you click the New menu, if the current file is opened from disk and has been modified, save the file in the path of the OpenFileDialog control. If it is a new file and the content is not empty, you need to use the SaveFileDialog control to save the file. The code of the [New (N)] menu item is as follows: Double-click “New” to enter the code area and fill in the following code

/ / * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * / / [new] menu code //************************************************************************* private void tsmiNew_Click(object sender, EventArgs e)

{// Determine whether the current file is opened from disk, or whether the new document is not empty and the file is not saved

if (b == true || rtxtNotepad.Text.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

}

When clicking this menu item, if you want to open the “*.rtf” file from disk or other devices, the same decision is made. The difference is that after making the decision, open the file with the OpenFileDialog control and set the previously defined variable (s) to “true” each time after saving the file. Indicates that the file has been saved. The code is as follows: Double-click “Open”, enter the code area and fill in the following code

/ / * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * / / open 】 【 menu code / / * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

private void tsmiOpen_Click(object sender, EventArgs e)

{

if (b == true || rtxtNotepad.Text.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; break; case "No": b = false; rtxtNotepad.Text = ""; break; } } } odlgNotepad.RestoreDirectory = true; if ((odlgNotepad.ShowDialog() == DialogResult.OK) && odlgNotepad.FileName ! = "") { rtxtNotepad.LoadFile(odlgNotepad.FileName); // open the code statement b = true; } s = true;Copy the code

}

(3) [Save (S)] Menu item Click this menu item to save the content of the text box. You need to determine whether the file is opened from the disk or a new one. If it is opened from the disk, you need to determine whether there is any change. If it is a new document, call SaveFileDialog to save the file, change bool b to “true”, and assign the SaveFileDialog file path to OpenFileDialog so that the next time you open the file, By default, the file path is the path where the file was just saved. After each file is saved, the variable (s) defined previously must be set to true, indicating that the file has been saved. The code is as follows: Double-click “Save”, enter the code area and fill in the following code

/ / * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * / / [save] menu code //************************************************************************* private void tsmiSave_Click(object sender, EventArgs e)

{

If (b == true && rtxtNotepad.modified == true) {rtxtNotepad.savefile (odlgNotepad.filename); s = true; } else if (b == false && rtxtNotepad.Text.Trim() ! = "" && sdlgNotepad.ShowDialog() == DialogResult.OK) { rtxtNotepad.SaveFile(sdlgNotepad.FileName); // save the statement s = true; b = true; odlgNotepad.FileName = sdlgNotepad.FileName; }Copy the code

}

(4) [Save as (A)] Menu item After saving the file as, the variable (s) defined above should be set to “true”, indicating that the file has been saved. The code for this menu item is as follows: Double-click “Save as” to enter the code area and fill in the following code

/ / * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * / / code “save as” menu //************************************************************************* private void tsmiSaveAs_Click(object sender, EventArgs e)

{

if (sdlgNotepad.ShowDialog() == DialogResult.OK)
{
    rtxtNotepad.SaveFile(sdlgNotepad.FileName);
    s = true;
}
Copy the code

}

The function of this menu item is to exit the Notepad program, and the code is as follows: Double click on the “exit” to fill in the following code / / * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * / / [out of] the menu code //************************************************************************* private void tsmiClose_Click(object sender, EventArgs e)

{

Application.Exit(); // End of programCopy the code

}

(3) [Edit (E)] Menu

[edit (E)] menu to complete cancellation (a recent to edit the text frame’s operation), copy (copy the selected text content), cut (shear selected text content), paste, paste the contents of the clipboard), future generations (rich text box selected all of the content) and append to a text file of the date of the current function. [Edit (E)] Click event codes of menu items are as follows: / / * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * / / click on the edit menu 】 each menu item code / / * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

Private void tsmiUndo_Click(object Sender, EventArgs e) {rtxtNotepad.undo (); / / cancellation}

Private void tsmiCopy_Click(object sender, EventArgs e) {rtxtNotepad.copy (); / / copy}

Private void tsmiCut_Click(object sender, EventArgs e) {rtxtNotepad.cut (); / / shear}

Private void tsmiPaste_Click(object sender, EventArgs e) {rtxtNotepad.paste (); / / paste}

Private void tsmiSelectAll_Click(object sender, EventArgs e) {rtxtNotepad.selectAll (); / / all}

Private void tsmiDate_Click(object sender, EventArgs e) { rtxtNotepad.AppendText(System.DateTime.Now.ToString()); // Display the current date}

(4) [Format (O)] Menu [Format (O)] Menu is used to set whether the open or new text content is automatically wrapped or not, and set the font format function. The default Checked property of this menu item is “True”. The text content is automatically wrapped according to the width of the text box. Otherwise, only the paragraph marks the line wrap. / / * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * / / code word wrap 】 【 menu //************************************************************************* private void tsmiAuto_Click(object sender, EventArgs e)

{

if (tsmiAuto.Checked == false) { tsmiAuto.Checked = true; // Select the menu item rtxtNotepad.WordWrap = true; } else {tsmiAuto.Checked = false; rtxtNotepad.WordWrap = false; }Copy the code

}

(2) [Font (F)] Menu Item When you click this menu item, a font dialog box will pop up to adjust the font, color and other properties of the selected content. So you need to use the FontDialog control you added earlier. Click event code of menu item is as follows: / / * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * / / code “font” menu //************************************************************************* private void tsmiFont_Click(object sender, EventArgs e)

{

fdlgNotepad.ShowColor = true;
if (fdlgNotepad.ShowDialog() == DialogResult.OK)
{
    rtxtNotepad.SelectionColor = fdlgNotepad.Color;
    rtxtNotepad.SelectionFont = fdlgNotepad.Font;
}
Copy the code

}

(5) 【 View (V) 】 menu 【 View (V) 】 menu is used to set whether the toolbar and status bar are displayed on the Notepad. These two menu items are selected by default, and you can set different display effects by clicking corresponding menu items. (1) [Toolbar (T)] Menu Item This menu item is used to control the display and hiding of the toolbar. By default, the toolbar is displayed. When hiding, the position and height of the multi-format text box should be modified. The code is as follows: / / * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * / / menu toolbar 】 【 code (not necessary) //************************************************************************* private void tsmiToolStrip_Click(object sender, EventArgs e)

{

Point point; If (tsmitoolstrip.checked == true) {// When hiding the toolbar, set the coordinate to (0, 24) because the menu height is 24 point = new Point(0, 24); tsmiToolStrip.Checked = false; tlsNotepad.Visible = false; Rtxtnotepad. Location = point; Rtxtnotepad. Height += tlsnotepad. Height; } else {/* New point (0, 49);} else {/* New point (0, 49); tsmiToolStrip.Checked = true; tlsNotepad.Visible = true; rtxtNotepad.Location = point; rtxtNotepad.Height -= tlsNotepad.Height; }Copy the code

} / / * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * / / * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * (2) the status bar (S) 】 【 menu item (not necessary) the menu item is used to control the show and hide the status bar, The status bar is displayed by default, and the height of the multi-format text box should be changed when hidden. The code is as follows: / / * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * / / code “status bar” menu //************************************************************************* private void tsmiStatusStrip_Click(object sender, EventArgs e)

{

if (tsmiStatusStrip.Checked == true)
{
    tsmiStatusStrip.Checked = false;
    stsNotepad.Visible = false;
    rtxtNotepad.Height += stsNotepad.Height;
}
else
{
    tsmiStatusStrip.Checked = true;
    stsNotepad.Visible = true;
    rtxtNotepad.Height -= stsNotepad.Height;
}
Copy the code

} / / * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * / / * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * (6) the help (H) 】 the menu This menu is only one menu item [about notepad (A)], This menu item invokes a form (frmAbout) to display some information about the program. Use the LinkLabel control to set up links that make it easy to send e-mails to authors. The design of the frmAbout form is covered in detail in the next section (17.3.3 About Notepad). After the frmAbout form is designed, in order to display the form, the click event code of the [About Notepad (A)] menu item needs to be written as follows: / / * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * / / menu about notebook 】 【 code //************************************************************************* private void tsmiAbout_Click(object sender, EventArgs e)

{

frmAbout ob_FrmAbout = new frmAbout();
ob_FrmAbout.Show();
Copy the code

}

1.4 About Notepad

Next click the Visual C# 2013 [project] → [add Windows forms] menu item to add a form named “frmAbout”, as shown in the figure.

  1. Interface design

Then add several Label, Button, LinkLabel and PictrueBox controls to the form “frmAbout”. The program interface after setting the properties according to the information in Figure 3.1 is shown as follows.

[About Notepad] The form and the property Settings of the controls on the form are shown in Table 3.1.

2. Code this section of the code is very simple, click the “OK” button to close the form. And use LinkLabel control code as follows: / / * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * / / code about the notepad 】 【 / / * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

Private void btnOk_Click(Object sender, EventArgs e)

{

this.Close();
Copy the code

}

/ / use LinkLabel open E-mail or page private void llblEmail_LinkClicked (object sender, LinkLabelLinkClickedEventArgs e)

{

System. Diagnostics. Process. The Start (” www.baidu.com “); }

The toolbar provides shortcut buttons for easy operation. When a user presses the button, it selects a menu item. The switch statement is as follows. / / * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * / / toolbar ItemClicked event code //************************************************************************* private void tlsNotepad_ItemClicked(object sender, ToolStripItemClickedEventArgs e) { int n; // The variable n is used to receive the index number of the pressed button starting from 0 n = tlsnotepad.items.indexof (e.lickedItem); Switch (n) {case 0: New NToolStripButton_Click (sender, e); break; Case 1: Open OToolStripButton_Click (sender, e); break; Case 2: Save SToolStripButton_Click (sender, e); break; /case 3: tsmiCopy_Click(sender, e); break;// / we don’t use case3

Case 4: Cut UToolStripButton_Click (sender, e); break; Case 5: Paste PToolStripButton_Click (sender, e); break; /*case 6: tsmiPaste_Click(sender, e); break; Case6 case 7: tsmiAbout_Click (sender, e); break; }Copy the code

} / / * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * / / toolbar ItemClicked event code / / * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 1.6 timer code should be displayed in the status bar tssLbl2 current time, We need to write the following code for the timer control to trigger the Tick event (every second) : / / * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * / / the Tick event timer control code //************************************************************************* private void tmrNotepad_Tick(object sender, EventArgs e) { tssLbl2.Text = System.DateTime.Now.ToString(); } / / * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * / / * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

1.7 Window code (we don’t need this code, it will automatically adjust when the window property is set to Auto). After changing the window size (e.g. maximizing the window size or using the mouse to change the window size), we should write the following SizeChanged event code for the window in order for the labels in the status bar to change their width as well: / / * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * / / form SizeChanged event code //************************************************************************* private void frmNotepad_SizeChanged(object sender, EventArgs e) { frmNotepad ob_frmNotepad = new frmNotepad(); tssLbl1.Width = this.Width / 2 – 12; tssLbl2.Width = tssLbl1.Width; } / / * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * / / * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

Run screenshot:

End of run! The source code:Gitee.com/xie-junlong…