⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jishiben.cs

📁 改程序实现了记事本的一些基本操作
💻 CS
📖 第 1 页 / 共 2 页
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Drawing.Printing;

namespace jishiben
{
    public partial class jishiben : Form
    {
        public static int replacecount = 0;
        private searcher s;
        private replace r;
        private Editgoto gt;
        private string filename,fname;
        private bool filesaved = false;
        private bool textChanged = false;
        private bool finded = false;
        private string findstring, sourstring;
        private static string[] lines;
        private static int selects = 0;
        public static int Selects
        {
            get
            {
                return selects;
            }
            set
            {
                selects = value;
            }
        }
        public static string[] Lines
        {
            get
            {
                return lines;
            }
        }
        public jishiben()
        {
            InitializeComponent();
        }    
        private void Filesaveas()
        {
            SaveFileDialog MyDialog = new SaveFileDialog();
            MyDialog.InitialDirectory = "c:\\";
            MyDialog.FileName = "MyText";
            MyDialog.Filter = "Save files (*.txt)|*.txt|All files (*.*)|*.*";
            if (MyDialog.ShowDialog() == DialogResult.OK)
            {
                this.filename = MyDialog.FileName;
                Writefile();
            }
        }
        private void Writefile()
        {
            StreamWriter writer = new StreamWriter(filename, false, System.Text.Encoding.Default);
            writer.Write(tBtext.Text);
            writer.Close();
            char[] c = filename.ToCharArray();
            int length = 0;
            for (int i = filename.Length-1; i > 0; i--)
            {
                if(c[i]=='\\')
                { 
                    length=i+1;
                    break;
                }     
            }
            fname = filename.Remove(0,length);
            this.Text = fname;
            filesaved = true;
        }
        
        private void Fileopen()
        {
            OpenFileDialog MyDialog = new OpenFileDialog();
            MyDialog.DefaultExt = "txt";
            MyDialog.Filter = "Open files (*.txt)|*.txt";
            if (MyDialog.ShowDialog() == DialogResult.OK)
            {
                tBtext.Clear();
                this.filename = MyDialog.FileName;
                Readfile();
                filesaved = true;
                textChanged = false;
                tSMEdit_Undo.Enabled = false;
            }
        }
        private void Readfile()
        {
            StreamReader reader = new StreamReader(filename, System.Text.Encoding.Default);
            tBtext.Text = reader.ReadToEnd();
            reader.Close();
            char[] c = filename.ToCharArray();
            int length = 0;
            for (int i = filename.Length - 1; i > 0; i--)
            {
                if (c[i] == '\\')
                {
                    length = i + 1;
                    break;
                }
            }
            fname = filename.Remove(0, length);
            this.Text = fname;
            filesaved = true;
        }

        private void tSMFile_new_Click(object sender, EventArgs e)
        {
            if (tBtext.Text != "" && filesaved==false)
            {
                DialogResult a;
                a = MessageBox.Show("文件没有保存,你要保存吗?", "没有保存", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
                if (a == DialogResult.Yes)
                {
                    Filesaveas();
                }
                else
                    if (a == DialogResult.No)
                    {
                        tBtext.Clear();
                        this.Text = "新建 记事本";
                        this.filename = "";
                    }
            }
            if (filesaved || this.Text == "记事本")
            {
                tBtext.Clear();
                this.Text = "新建 记事本";
                this.filename = "";
            }
        }

        private void tSMFile_open_Click(object sender, EventArgs e)
        {
            Fileopen();
        }

        private void tSMFile_save_Click(object sender, EventArgs e)
        {
            if (filename == null)
            {
                Filesaveas();
            }
            else
            {
                Writefile();
            }
        }

        private void tSMFile_saveas_Click(object sender, EventArgs e)
        {
            Filesaveas();
        }

        private void tSMFile_quit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void tSMFile_print_Click(object sender, EventArgs e)
        {
            PrintDocument txt = new PrintDocument();
            txt.DocumentName = this.filename;
            PrintDialog MyDialog = new PrintDialog();
            MyDialog.Document = txt;
            try
            {
                MyDialog.ShowDialog();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void tSMFile_pagesetup_Click(object sender, EventArgs e)
        {
            PrintDocument txt = new PrintDocument();
            txt.DocumentName = this.filename;
            PrintPreviewDialog MyDialog = new PrintPreviewDialog();
            MyDialog.Document = txt; 
            try
            {
                MyDialog.ShowDialog();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            
        }

        private void tSMEdit_Undo_Click(object sender, EventArgs e)
        {
            tBtext.Undo();
        }

        private void tSMEdit_cut_Click(object sender, EventArgs e)
        {
            tBtext.Cut();
        }

        private void tSMEdit_copy_Click(object sender, EventArgs e)
        {
            tBtext.Copy();
        }

        private void tSMEdit_patse_Click(object sender, EventArgs e)
        {
            tBtext.Paste();
        }

        private void tSMEdit_delet_Click(object sender, EventArgs e)
        {
            tBtext.SelectedText = "";
        }

        private void tSMEdit_search_Click(object sender, EventArgs e)
        {
            s = new searcher();
            s.Search += new searchEventHandler(searcher_Search);
            s.Owner = this;
            s.Show();
        }

        private void tSMEdit_chooseall_Click(object sender, EventArgs e)
        {
            tBtext.SelectAll();
        }

        private void tSMEdit_time_Click(object sender, EventArgs e)
        {
            DateTime now = DateTime.Now;
            tBtext.SelectedText = now.ToString();
        }

        private void tSMSearch_statusbar_Click(object sender, EventArgs e)
        {
            tSMSearch_statusbar.Checked = !tSMSearch_statusbar.Checked;
            if (tSMSearch_statusbar.Checked)
            {
                sSstatusbar.Visible = true;
            }
            else
            {
                sSstatusbar.Visible = false;
            }
        }

        private void tSMFormat_autoline_Click(object sender, EventArgs e)
        {
            tSMFormat_autoline.Checked = !tSMFormat_autoline.Checked;
            if (tSMFormat_autoline.Checked)
            {
                tBtext.WordWrap = true;
                this.tSMEdit_goto.Enabled = false;
            }
            else
            {
                tBtext.WordWrap = false;
                this.tSMEdit_goto.Enabled = true;
                lines = tBtext.Lines;
                //MessageBox.Show(tBtext.Lines[5].Length.ToString());
                //MessageBox.Show(tBtext.SelectionStart.ToString());
                tBtext.ScrollBars = RichTextBoxScrollBars.ForcedBoth;
            }
        }

        private void tSMFormat_font_Click(object sender, EventArgs e)
        {
            FontDialog MyDialog = new FontDialog();
            if (MyDialog.ShowDialog() == DialogResult.OK)
            {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -