Introduction to Notepad Notepad is a common software (as shown in the following figure).
Note Pad introduced in this chapter implements some functions of The Notepad in Windows. In addition, you can 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. 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 VS2019 click file → new → project (2) select template → Visual C# → Windows → Windows form application write Notepad under the name below (3) change form name click form, → Lower right corner property →text Change to “Notepad” ii. (1) MenuStrip (menu control) ① Click on the left side of the form, the toolbox will appear → Find the Menustrip → Drag to the form ② Click on the menu bar → Lower right corner properties → Modify the Name property to “mnusNotepad” ③ Click input [file (F)], [Edit (E)], [Format (O)], [View (V)] and [Help (H)] (4) set up 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)] to find the corresponding properties window (2) the ToolStrip (toolbar controls). ① Click on the left side of the form to display the toolbox → Find “ToolStrip” → Drag to the form ② Click on the toolbar → Properties in the lower right corner → Modify the Name property to “tlsNotepad” ③ Right click on the toolbar → Insert standard items ④ Right click on the toolbar → Edit items (3) RichTextBox (multi-format text box control) ① Click on the left side of the form, the toolbox will appear → find “RichTextBox” → drag to the form ② Click on the toolbar → properties in the lower right corner → Modify the Name property to “rtxtNotepad” ③Anchor property select “Top, “Bottom, Left, Right”, so that when the form size changes, the RichTextBox control size also changes. ① 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. ② 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”. When the user clicks the [File] → [Open] menu item of Notepad, open the file with the open dialog box OpenFileDialog. OpenFileDialog controls the Name attribute of the “odlgNotepad”, the Filter attribute set to “| RTF files. All files RTF | |.” . 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 |. RTF. 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”. The Notepad described in this chapter displays the clock in the status bar, which requires the use of a Timer control. 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.
ToolStripMenuItem_Click(Object Sender, EventArgs e) {if (o.filename! = “” && richboxTextHasChanged == true && messagebox. Show(” Text content has changed \n Do you want to save the changes?” , the “message”, MessageBoxButtons. OKCancel) = = System. Windows. Forms. DialogResult. OK) {/ / if the text changes, First of all to save richTextBox1. SaveFile (od. The FileName, RichTextBoxStreamType. PlainText); } // otherwise the initialization interface od.FileName = “”; This. Text = “notepad”; this.richTextBox1.Clear(); this.richboxTextHasChanged = false; Private void ToolStripMenuItem_Click(Object Sender, EventArgs e) {od.showdialog (); // Select open text file if (od.filename! = “”) {/ / loaded into the interface of the content of the text file inside inner richTextBox1 LoadFile (od. The FileName, RichTextBoxStreamType. PlainText); This. Text = od.FileName + “-notepad”; ToolStripMenuItem_Click(Object sender, EventArgs e) {if (o.filename! = “”) {richTextBox1. SaveFile (od. The FileName, RichTextBoxStreamType PlainText); } else messagebox. Show(” Please open text file first “, “info prompt”, messageboxbuttons.ok); } (4) private void toolStripMenuItem1_Click(Object sender, EventArgs e) { if (sd.ShowDialog() == System.Windows.Forms.DialogResult.OK) { richTextBox1.SaveFile(sd.FileName, RichTextBoxStreamType.PlainText); Private void Dispose ToolStripMenuItem_Click(Object sender, EventArgs e) {this.dispose (); this.Close(); } (6) Private void ToolStripMenuItem_Click(object sender, EventArgs e) {richtextBox1.cut (); } (7) Private void Copy ToolStripMenuItem_Click(object Sender, EventArgs e) {richTextBox1.copy (); Private void Paste ToolStripMenuItem_Click(object sender, EventArgs e) {richtextBox1.paste (); Private void ToolStripMenuItem_Click(object sender, EventArgs e) {FontDialog fd = new FontDialog(); fd.ShowDialog(); richTextBox1.Font = fd.Font; } (10) private void AToolStripMenuItem_Click(Object sender, EventArgs e) {richTextBox1.selectAll (); } (11) private void DToolStripMenuItem_Click(object sender, EventArgs e) { richTextBox1.AppendText(System.DateTime.Now.ToString()); } ToolStripMenuItem_Click(Object sender, EventArgs e) {System. Diagnostics. Process. The Start (” answers.microsoft.com/en-us/windo…). ; } (13) AToolStripMenuItem_Click(object sender, EventArgs e) { AboutBox.Form1 about = new AboutBox.Form1(); about.ShowDialog(); } private void timer1_Tick(Object Sender, EventArgs e) {private void timer1_Tick(Object Sender, EventArgs e) {label2.text = “Time: “+ DateTime. Now. ToLongTimeString (). The ToString (); }
Using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms;
namespace NotePad { public partial class frmNotepad : Form { bool b = true; bool s = true; public frmNotepad() { InitializeComponent(); }
Private void new ToolStripMenuItem_Click (object sender, EventArgs e) {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; }}}} private void close ToolStripMenuItem1_Click(Object Sender, EventArgs e) {application.exit (); } private void toolStrip1_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: CreateToolStripMenuItem_click (sender, e); break; Case 1: Open ToolStripMenuItem_Click(sender, e); break; case 2: tsmiSave_Click(sender, e); break; /*case 3: tsmiCopy_Click(sender, e); break; Case3 case 4: tsmiCut_Click(sender, e); break; case 5: tsmiPaste_Click(sender, e); break; /*case 6: tsmiPaste_Click(sender, e); break; Case6 case 7: tsmiAbout_Click(sender, e); break; } } private void frmNotepad_Load(object sender, EventArgs e) { frmNotepad ob_frmNotepad = new frmNotepad(); tssLbl1.Width = this.Width / 2 - 12; tssLbl2.Width = tssLbl1.Width; } private void odlgNotepad_FileOk(object sender, CancelEventArgs e) { } private void rtxtNotepad_TextChanged(object sender, EventArgs e) { s = false; } private void open ToolStripMenuItem_Click (object sender, EventArgs e) {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; 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; } 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; } } private void tsmiSaveAs_Click(object sender, EventArgs e) { if (sdlgNotepad.ShowDialog() == DialogResult.OK) { rtxtNotepad.SaveFile(sdlgNotepad.FileName); s = true; } } 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()); } 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; } } private void tsmiFont_Click(object sender, EventArgs e) { fdlgNotepad.ShowColor = true; if (fdlgNotepad.ShowDialog() == DialogResult.OK) { rtxtNotepad.SelectionColor = fdlgNotepad.Color; rtxtNotepad.SelectionFont = fdlgNotepad.Font; } } 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; } } 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; } } private void tsmiAbout_Click(object sender, EventArgs e) { frmAbout ob_FrmAbout = new frmAbout(); ob_FrmAbout.Show(); } private void tmrNotepad_Tick(object sender, EventArgs e) { tssLbl2.Text = System.DateTime.Now.ToString(); }}Copy the code
}