SoFunction
Updated on 2025-03-07

Sample code to implement the function of Notepad using C#

Knowledge points:

openFileDialog1 is a control in a Windows Forms application that implements the function of the file selection dialog box. It is an instance of the class.

The OpenFileDialog control allows the user to browse the file system and select the file to be opened. It provides some properties and methods for setting and controlling the appearance and behavior of the file selection dialog.

In the code, openFileDialog1 is an instance of the OpenFileDialog control created in the form designer. By setting the properties of this control, you can define the initial directory where the file dialog box is opened, the file type that can be displayed, etc.

It has the following properties and methods:

property:

  • InitialDirectory: Gets or sets the initial directory where the file dialog box is opened.
  • Filter: Gets or sets the file type that can be displayed by opening the file dialog box.
  • FileName: Gets or sets the full path of the file to be opened by the user.
  • Multiselect: Gets or sets a value indicating whether multiple files are allowed to be selected.
  • Title: Gets or sets the title of the opening file dialog box.
  • CheckFileExists: Gets or sets a value indicating whether to check whether the file entered by the user exists.
  • CheckPathExists: Gets or sets a value indicating whether to check whether the path entered by the user exists.

method:

ShowDialog: Shows the Open File dialog box and waits for the user to select the file. Returns a DialogResult value, indicating the user's operation result.

OpenFile: Opens the file selected by the user and returns a Stream object to read the contents of the file.

int index = (@"\");

The LastIndexOf method is used to find the last occurrence of a specified character or string in the string.

@"\" represents the backslash character \. In C#, the @ symbol needs to be used to represent escaped characters in the string.

(@"\") Returns the last backslash character \ position in the filename string.

The index variable is used to store the position of the last backslash character.

filename = (index + 1);

The Substring method is used to extract substrings from strings.

index + 1 means to extract substrings from the next character at the index position.

(index + 1) Returns a substring starting from the index + 1 position to the end of the string.

The filename variable is assigned to the extracted file name (excluding paths).

= filename + "My Notepad";

Represents the title bar text of the current window.

filename + "My Notepad" splice the extracted file name with the string "My Notepad".

= filename + "My Notepad" Sets the spliced ​​string to the title of the window.

The SaveFileDialog class is a Windows Forms control used to display the Save File dialog box. It provides some properties and methods for configuring and manipulating the behavior and results of the save file dialog.

The following are some common properties and methods of the SaveFileDialog class:

property:

FileName: Gets or sets the default file name in the Save File dialog box.

Filter: Get or set the file type that can be displayed in the Save File dialog box.

InitialDirectory: Gets or sets the initial directory of the save file dialog box.

Title: Gets or sets the title of the Save File dialog box.

OverwritePrompt: Gets or sets a value indicating whether an overwrite prompt is displayed when the file selected by the user already exists.

CreatePrompt: Gets or sets a value indicating whether a creation prompt is displayed when the file name entered by the user does not exist.

DefaultExt: Gets or sets the default file extension.

method:

ShowDialog(): Displays the Save File dialog box and returns a DialogResult value to represent the user's operation result.

Dispose(): Release the resource used by the SaveFileDialog object.

Implement code

using System;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
 
namespace NoteBook45
{
    public partial class Form1 : Form
    {
        bool isFirstSave = true;
        public Form1()
        {
            InitializeComponent();
        }
 
        private void NewToolStripMenuItem_Click(object sender, EventArgs e)//New Violent Construction        {
            ();
             = "Unt title-My Notepad";
        }
 
        private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
 
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {
             = "Unt title-My Notepad";
             = false;
            Automatic line wrap = true;
        }
 
        private void quitToolStripMenuItem_Click(object sender, EventArgs e)//Escape violently        {
            ();
        }
 
        private void RevokeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ();
        }
 
        private void copyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if ( != "")
                ();
            else
                ("No content is selected and cannot be copied!");
        }
 
        private void PasteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ();
        }
 
        private void CutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if ( != "")
                ();
            else
                ("No content is selected, no cut can be done!");
        }
 
        private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
        {
 
        }
 
        private void copyToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            copyToolStripMenuItem_Click(sender, e);
        }
 
        private void PasteToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            PasteToolStripMenuItem_Click(sender, e);
        }
 
        private void CutToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            CutToolStripMenuItem_Click(sender, e);
        }
 
        private void toolStripButtonNew_Click(object sender, EventArgs e)
        {
            NewToolStripMenuItem_Click(sender, e);
        }
 
        private void toolStripButtonOpen_Click(object sender, EventArgs e)
        {
 
        }
 
        private void toolStripButtonsave_Click(object sender, EventArgs e)
        {
 
        }
 
        private void toolStripButtoncopy_Click(object sender, EventArgs e)
        {
            copyToolStripMenuItem_Click(sender, e);
        }
 
        private void toolStripButtonPaste_Click(object sender, EventArgs e)
        {
            PasteToolStripMenuItem_Click(sender, e);
        }
 
        private void toolStripButtonCut_Click(object sender, EventArgs e)
        {
            CutToolStripMenuItem_Click(sender, e);
        }
 
        private void toolStripButtonRevoke_Click(object sender, EventArgs e)
        {
            RevokeToolStripMenuItem_Click(sender, e);
        }
 
        private void OpenToolStripMenuItem_Click(object sender, EventArgs e)// Violence opens        {
            =@"C:\";//Set the initial directory to open the file dialog box             = "Text file(*.txt)|*.TXT";//Set the file type that can be displayed when opening the file dialog box             = "";
            ();//Show the Open File dialog box            string filename = ;//Get the full path of the file to be opened by the user selected by the user            if (filename == "")
                ("The file to be opened is not selected!");
            else
            {
                (filename, );//Load the file content into the rich text box in the form of plain text                //c:\my documents\
                int index = (@"\");// Find the position of the last backslash in the file name                filename = (index + 1);// Find the main file name and extension of the file name                 = filename + "My Notepad";
            }
 
        }
 
        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (isFirstSave)
 {
     Save asToolStripMenuItem_Click(sender, e);
         isFirstSave = false;
     }
 else
     (, );
        }
 
        private void Save asToolStripMenuItem_Click(object sender, EventArgs e)
        {
             = @"c:\";
             = "Text file(*.txt)|*.TXT";
             = "";
            string filename = "";
            if (() == )
            {
                filename = ;
                (filename, );
                int index = (@"\");// Find the position of the last backslash in the file name                filename = (index + 1);// Find the main file name and extension of the file name                 = filename + "My Notepad";
            }
        }
 
        private void Automatic line wrapToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (Automatic line wrap)
                 = true;
            else
                 = false;
        }
 
        private void rtxtEdit_TextChanged(object sender, EventArgs e)
        {
 
        }
    }
}

This is the article about the example code of using C# to implement the function of Notepad. For more related content of C#, please search for my previous articles or continue browsing the related articles below. I hope you will support me in the future!