Moment For Technology

WEEK9- Imitation Windows Notepad

Posted on Aug. 8, 2022, 6:01 p.m. by 李雅琪
Category: The code of life Tag: c#

Functional specifications

The notepad in addition to the implementation of Windows notepad part of the function, but also can arbitrarily change the font type, size and color, and in the status bar display time.

In order to facilitate the user's operation, a toolbar is also placed on the form of the program.

The source code to show

Interface design and other operations reference: C#+Winform notepad program

1. Set the flag bit

bool b = false; // Determine whether the file is new 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 s = true; // Check whether the file is saved
// True indicates that the file is already saved. False indicates that the file is not saved. The default value is true
Copy the code

2. The menu

2.1 file

Methods provided by SaveFileDialog OpenFileDialog RichTextBox

2.1.1 new

/ / new
        / / open | current file from the disk is not empty, then choose whether to save
            / / save
                // If the file is opened from disk and its contents are modified, the file is saved in the open path
                // If the file is new and not empty, SaveFileDialog selects a path to save it
                // Set the state to save
            / / no save
                // Empty the RichTextBox Text and set the state to New
        private void tsmiNew_Click(object sender, EventArgs e)
        {
            if (b == true|| rtxtNotepad.Text.Trim() ! ="")
            {
                if (s == false)
                {
                    string result;
                    result =
                        MessageBox
                            .Show("File not saved. Do you want to save it?"."Save file",
                            MessageBoxButtons.YesNoCancel)
                            .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.1.2 open

The previous section of code is the same as "New".

/ / open
        // Call the new method
        // Use OpenFileDialog to select open files
        // Set state for disk open  save
        private void tsmiOpen_Click(object sender, EventArgs e)
        {
            tsmiNew_Click (sender, e);
            odlgNotepad.RestoreDirectory = true;
            if( (odlgNotepad.ShowDialog() == DialogResult.OK)  odlgNotepad.FileName ! =""
            )
            {
                rtxtNotepad.LoadFile(odlgNotepad.FileName);
                b = true;
            }
            s = true;
        }
Copy the code
2.1.3 save
/ / save
        // If the file is opened from disk and its contents are modified, it is saved directly
        // If the file is new and not empty, SaveFileDialog selects a path and saves it
        // Set the state to save
        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);
                s = true;
                b = true; odlgNotepad.FileName = sdlgNotepad.FileName; }}Copy the code
2.1.4 save as
/ / save as
        // Save after SaveFileDialog selects the path
        // Set the state to save
        private void tsmiSaveAs_Click(object sender, EventArgs e)
        {
            if (sdlgNotepad.ShowDialog() == DialogResult.OK)
            {
                rtxtNotepad.SaveFile(sdlgNotepad.FileName);
                s = true; }}Copy the code
2.1.5 exit
/ / exit
        private void tsmiClose_Click(object sender, EventArgs e)
        {
            tsmiNew_Click(sender, e);
            Application.Exit();
        }
Copy the code

2.2 the editor

All use the methods provided by RichTextBox

2.2.1 undo
/ / cancel
        private void tsmiUndo_Click(object sender, EventArgs e)
        {
            rtxtNotepad.Undo();
        }
Copy the code
2.2.2 copy
/ / copy
        private void tsmiCopy_Click(object sender, EventArgs e)
        {
            rtxtNotepad.Copy();
        }
Copy the code
2.2.3 shear
/ / shear
        private void tsmiCut_Click(object sender, EventArgs e)
        {
            rtxtNotepad.Cut();
        }
Copy the code
2.2.4 paste
/ / paste
        private void tsmiPaste_Click(object sender, EventArgs e)
        {
            rtxtNotepad.Paste();
        }
Copy the code
2.2.5 selection
/ / all
        private void tsmiSelectAll_Click(object sender, EventArgs e)
        {
            rtxtNotepad.SelectAll();
        }
Copy the code
2.2.6 date
// Use system.datetime. Now to get the current date and time
        private void tsmiDate_Click(object sender, EventArgs e)
        {
            rtxtNotepad.AppendText(System.DateTime.Now.ToString());
        }
Copy the code

2.3 format

2.3.1 Wrap
// wrap
        // Change the WordWrap property of the RichTextBox according to the Checked property
        private void tsmiAuto_Click(object sender, EventArgs e)
        {
            if (tsmiAuto.Checked == false)
            {
                tsmiAuto.Checked = true;
                rtxtNotepad.WordWrap = true;
            }
            else
            {
                tsmiAuto.Checked = false;
                rtxtNotepad.WordWrap = false; }}Copy the code
2.3.2 font

FontDialog

/ / font - FontDialog
        // According to the FontDialog Color and Font properties,
        // Change the SelectionColor and SelectionFont properties of the RichTextBox
        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

2.4 check the

Against 2.4.1 toolbar
// Toolbar display hidden
        // Change the Visible property of the ToolStrip according to the Checked property
        // Modify the Location and Height attributes of the RichTextBox
        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 height of the menu is 24
                point = new Point(0.24);
                tsmiToolStrip.Checked = false;
                tlsNotepad.Visible = false;
                // Sets the upper-left corner position of the multi-format text box
                rtxtNotepad.Location = point;
                // After hiding the toolbar, increase the height of the text box
                rtxtNotepad.Height += tlsNotepad.Height;
            }
            else
            {
                // when displaying the toolbar, the position of the upper left corner of the multi-format text box is (0,49),
                // Since the height of the toolbar is 25 and the height of the menu is 24, it is 49
                point = new Point(0.49);
                tsmiToolStrip.Checked = true;
                tlsNotepad.Visible = true; rtxtNotepad.Location = point; rtxtNotepad.Height -= tlsNotepad.Height; }}Copy the code
2.4.2 the status bar

The StatusStrip is below the RichTextBox and does not affect the Location property of the RichTextBox (upper left position).

// Status bar display is hidden
        // Change StatusStrip Visible property according to Checked property
        // Change the Height of the RichTextBox
        // StatusStrip below RichTextBox does not affect the Location property of RichTextBox (upper left position)
        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

2.5 help

2.5.1 About Notepad
2.5.1.1 child form

Win10 with notepad help link.

// Close the help window
        private void btnOK_Click(object sender, EventArgs e)
        {
            this.Close();
        }
// Open help link (select win10 with notepad help link)
        private void LinkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process.Start("https://cn.bing.com/search?q=get+help+with+notepad+in+windows+10filters=guid:%224466414-en-dia%22%20lang:%22en%22form =T00032ocid=HelpPane-BingIA");
        }
Copy the code
2.5.1.2 parent form
// Displays a help window
        private void tsmiAbout_Click(object sender, EventArgs e)
        {
            frmAbout ob_FrmAbout = new frmAbout();
            ob_FrmAbout.Show();
        }
Copy the code

2.6 the toolbar

The index bit starts at 0, and the delimiter also occupies the index bit.

// Click the item index number from the toolbar to call the corresponding method under "Edit" in the menu
        private void tlsNotepad_ItemClicked(
            object sender,
            ToolStripItemClickedEventArgs e
        )
        {
            int n;

            n = tlsNotepad.Items.IndexOf(e.ClickedItem);
            switch (n)
            {
                case 0:
                    tsmiSave_Click (sender, e);
                    break;
                case 1:
                    tsmiOpen_Click (sender, e);
                    break;
                case 2:
                    tsmiSave_Click (sender, e);
                    break;
                case 4:
                    tsmiCopy_Click (sender, e);
                    break;
                case 5:
                    tsmiCut_Click (sender, e);
                    break;
                case 6:
                    tsmiPaste_Click (sender, e);
                    break;
                case 8:
                    tsmiAbout_Click (sender, e);
                    break; }}Copy the code

2.7 Event Triggering

2.7.1 TextChanged event for the RichTextBox control

// The TextChanged event of the RichTextBox control
        // The text is modified and the state is set to unsaved
        private void rtxNotepad_TextChanged(object sender, EventArgs e)
        {
            s = false;
        }
Copy the code

2.7.2 SizeChanged Event for the Form control

The AutoSize attribute of Label must be set to False; otherwise, Width cannot be changed.

// SizeChanged event for the Form control
        // Adjust the label width in the status bar when the form size changes
        // The AutoSize attribute of Label must be set to False
        private void frmNotepad_SizeChanged(object sender, EventArgs e)
        {
            tssLbl1.Width = Width / 2;
            tssLbl2.Width = Width / 3;
        }
Copy the code

2.7.3 Tick events of the Timer control

The Interval attribute is expressed in milliseconds.

// The Tick event of the Timer control
        // Use system.datetime. Now to refresh the status bar time every second
        // Set the Interval attribute (unit: ms) to 1000
        private void tmrNotepad_Tick(object sender, EventArgs e)
        {
            tssLbl2.Text = System.DateTime.Now.ToString();
        }
Copy the code

A functional test

file

The editor

format

To view

help

Window size changes

tips

  • Through this assignment, I realized the importance of requirement analysis in software design, and had a deeper understanding of the "events" learned before, and also felt the effect of reasonable naming on increasing readability (Hungarian nomenclature).

.cs class files. Source code is written here, mainly look at the code here.

.resx resource file, which stores resources.

Csproj C# project file, using VS to open this file can directly open the project, automatically generated.

Csproj. user is a configuration file that is automatically generated and records information such as the project generation path and project startup program.

.designer.cs design file, automatically generated to initialize the controls on the form (in the function InitializeComponent()).

Aspx is a web page file where the HTML code is written.

  • This procedure still has some limitations: Windows notepad in the search and replace functions; Use default toolbar instead of right-click menu, not convenient enough; Provided documents are not prompted to save before exiting the program (this is done by calling tsmiNew_Click(sender, e) before application.exit ()).

  • Difficulties encountered:

  1. The form design part of the menu is tedious;

  2. When adjusting the Width of the status bar Label, the AutoSize property of Label must be set to False; otherwise, Width cannot be changed.

  3. Clarify the criteria for the save operation and the establishment of two flag bits (see code comments).

Repository

Giteee

Search
About
mo4tech.com (Moment For Technology) is a global community with thousands techies from across the global hang out!Passionate technologists, be it gadget freaks, tech enthusiasts, coders, technopreneurs, or CIOs, you would find them all here.