The purpose of this article

How to use Visual C# to design a Windows application – notepad

1.1 Introduction to Notepad

Notepad is a commonly used software. In Windows, Microsoft comes with a Notepad software. The Notepad software in Windows 7 is shown in Figure 1.

The notepad introduced in this article, the realization of Windows notepad part of the function, and can also arbitrarily change the font type, size and color, and in the status bar display time. In order to facilitate the user’s operation, also placed a toolbar on the form of the program, and the program has the function of new file, open, save; 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.

1.2 Notepad interface design

New and Settings

1. Open VS Click File → New → Project

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

The interface is displayed, as shown in The figure

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

Follow 3 to set the remaining form properties

attribute Set the results
Name frmNotepad
Text notepad
StartPosition CenterScreen
Size 600, 450

(1) Interface design

After creating the “Notepad” project, navigate to the Forms Designer window of the Notepad program and place the following controls (one each) on the form in turn:

(1) MenuStrip (menu control).

  1. 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 → Change the Name property to “mnusNotepad”

  1. Click to enter File (F), Edit (E), Format (O), View (V) and Help (H).

As is shown in

  1. Set the properties → for File (F), Edit (E), Format (O), View (V) and Help (H)

Click File (F), Edit (E), Format (O), View (V) and Help (H) to find the corresponding properties window and set it according to the table below

To this menu bar frame setup is complete!

(2) ToolStrip (Toolbar control)

  1. Click on the left side of the form to bring up the toolbox → find “ToolStrip” → drag into the form

2. Click the toolbar → Properties in the lower right corner → Modify the Name property to tlsNotepad, as shown in the figure

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

  1. 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)

  1. Click on the left side of the form will appear toolbox → find “RichTextBox” → drag to form
  2. Click the toolbar → Properties in the lower right corner → Modify the Name property to “rtxtNotepad” as shown in the figure

3. Select “Top, Bottom, Left, Right” as the Anchor property so that when the form size changes, the size of the RichTextBox control changes as well.

StatusStrip (Status bar control)

  1. Add the StatusStrip control, set its Name property to “stsNotepad”, Dock property to “Bottom” and Anchor property to “Bottom, Left, Right”.
  2. Then click […] to the right of Items(Collection). Button to open the item Set Editor dialog box, as shown in the figure.
  3. 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”.

Ready at the bottom, showing the time, in fact, where the Anchor property eventually automatically changes to “Top, Left”. This setting also allows the status bar at the bottom to change as the box size changes.

(5) OpenFileDialog

  1. Go to the OpenFileDialog from the toolbox and drag it directly into the form and click properties
  2. When the user clicks the [File] → [Open] menu item of Notepad, the OpenFileDialog is used to open the file. 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.

1.3 Programming code

First declare the following two public variables in the generic 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 the form to enter the code area and fill in the following code:

/ / * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

/* The Boolean variable b is used to determine whether the file is newly created or opened from disk,

True indicates that the file is opened from disk. False indicates that the file is created. The default value is false*/

bool b = false;

/* The Boolean variable s is used to determine whether the file is saved.

True indicates that the file has been saved, false indicates that the file has not been saved. The default value is true*/

bool s = true; / / * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

As shown in the figure:

(1) Multi-format text box code (RichTextBox)

When the text in the multi-format text box changes, you should set the Boolean variable “S” to false to indicate that the file is not saved, so write the code for the multi-format TextChanged event as follows: Double-click the RichTextBox to enter the code area and fill in the following code

/ / format text box the TextChanged event code / / * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * private void RtxtNotepad_TextChanged (object sender, EventArgs e) {rtxtNotepad_TextChanged(object sender, EventArgs e) {rtxtNotepad_TextChanged(object sender, EventArgs e) { } / / * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * / / * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *Copy the code

As shown in figure:

(2) Menu code

In the Notepad program introduced in this article, 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) {// Check whether the current file is open from disk, or if the new file is not empty, 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

(2) [Open (O)] menu item

When you click this menu item, if you want to open the “*.rtf” file from disk or other device, you also need to make a judgment, except that after the judgment, you need to open the file with the OpenFileDialog control, and after each save, you need to set the previously defined variable (s) to “true” to indicate 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 contents of the text box. You need to check whether the file is opened from the disk or a new one. If the file is opened from the disk, check whether the file is changed. 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 to 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); 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, set the previously defined variable (s) to “true” to indicate 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

/ / "save as" menu code / / * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * private void tsmiSaveAs_Click(object sender, EventArgs e) { if (sdlgNotepad.ShowDialog() == DialogResult.OK) { rtxtNotepad.SaveFile(sdlgNotepad.FileName); s = true; }}Copy the code

(5) [Exit (X)] menu item

The function of this menu item is to exit the Notepad program, the code is as follows:

Double-click “Exit” to fill in the following code

/ / [out of] the menu code / / * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * private void tsmiClose_Click(object sender, EventArgs e) { Application.Exit(); / / the end} / / * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * / / * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *Copy 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 / / * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * / / undo menu code 】 private void tsmiUndo_Click(object sender, EventArgs e) { rtxtNotepad.Undo(); Private void tsmiCopy_Click(Object sender, EventArgs e) {rtxtNotepad.copy (); Private void tsmiCut_Click(object sender, EventArgs e) {rtxtNotepad.cut (); Private void tsmiPaste_Click(object sender, EventArgs e) {rtxtNotepad.paste (); Private void tsmiSelectAll_Click(object sender, EventArgs e) {rtxtNotepad.selectAll (); Private void tsmiDate_Click(object sender, EventArgs e) { rtxtNotepad.AppendText(System.DateTime.Now.ToString()); // Display the current date}Copy the code

(4) [Format (O)] Menu

[Format (O)] Menu is used to set whether the open or new text content is automatically wrapped, and set the font format function.

The Checked property of this menu item is “True” by default. The text content is automatically wrapped according to the width of the text box. Otherwise, only line wrap is marked by paragraph.

[Wrap (W)] The code for the menu item is as follows:

/ / menu code word wrap 】 【 / / * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 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

(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. You can click the corresponding menu items to set different display effects.

(1) [Toolbar (T)] Menu item

This menu item controls the display and hiding of the toolbar. The toolbar is displayed by default. The position and height of the multi-format text box should be changed when the toolbar is hidden. The code is as follows:

/ / "toolbar" menu 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) [Status bar (S)] Menu item (not required)

This menu item controls the display and hiding of 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:

/ / / / "status bar" menus code * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 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) [Help (H)] Menu

This menu has only one menu item [About Notepad (A)], which calls A form (frmAbout) to display some information about this 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:

/ / code/menu/on a notepad 】 【 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 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. Write code

The code for this section is simple: click ok to close the form. And use the LinkLabel control

The code is as follows:

/ / code/on a notepad 】 【 / * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * / / [sure] button private void btnOk_Click(object sender, EventArgs e) { this.Close(); } private void llblEmail_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) {System. Diagnostics. Process. The Start (" http://www.baidu.com "); }Copy the code

Toolbar code

The toolbar provides some shortcut buttons to facilitate user operations. 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; }} / / * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * / / toolbar ItemClicked event code / / * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *Copy the code

1.6 Timer Code

To display the current time in tssLbl2 of the status bar, write the following code for the timer control’s Tick event (which fires every second) :

/ / the Tick event timer control code / / * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * private void tmrNotepad_Tick(object sender, EventArgs e) { tssLbl2.Text = System.DateTime.Now.ToString(); } / / * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * / / * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *Copy the code

1.7 Form Code

(We don’t need this code, it will be adjusted automatically when the form property is set to Auto)

To make the labels in the status bar change their width as you change the size of the form (for example, maximizing the window or resize the window with the mouse), write the following SizeChanged event code for the form:

/ / 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; } / / * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * / / * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *Copy the code

Run screenshot: