zoukankan      html  css  js  c++  java
  • winform记事本

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;
    
    namespace FirstForm
    {
        public partial class JiShiben : Form
        {
            public JiShiben()
            {
                InitializeComponent();
            }
    
            private void textBox1_TextChanged(object sender, EventArgs e)
            {
                if (textBox1.CanUndo)
                {
                    撤销ToolStripMenuItem.Enabled = true;
                }
            }
    
            private void 复制ToolStripMenuItem_Click(object sender, EventArgs e)
            {
                textBox1.Copy();
            }
    
            private void 粘贴ToolStripMenuItem_Click(object sender, EventArgs e)
            {
                textBox1.Paste();
            }
    
            private void 剪切ToolStripMenuItem_Click(object sender, EventArgs e)
            {
                textBox1.Cut();
            }
    
            private string bianhua;
    
            private void 撤销ToolStripMenuItem_Click(object sender, EventArgs e)
            {
                if (textBox1.CanUndo == true && bianhua == null)
                {
                    // Undo the last operation.
                    textBox1.Undo();
                    // Clear the undo buffer to prevent last action from being redone.
                    textBox1.ClearUndo();
                    // textBox1.CanUndo = false;
                    撤销ToolStripMenuItem.Enabled = false;
                }
                else
                {
                    textBox1.Select(chushi.Length,bianhua.Length);
                }
            }
    
            private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
            {
                textBox1.SelectedText = "";
            }
    
            private void 全选ToolStripMenuItem_Click(object sender, EventArgs e)
            {
                textBox1.SelectAll();
            }
    
            private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
            {
               DialogResult dr =  openFileDialog1.ShowDialog();
               if (dr == DialogResult.OK)
               {
                   string filename = openFileDialog1.FileName;
                   FileName = filename;
                   //通过读入流进行文件读取
                   StreamReader sr = new StreamReader(filename);
                   textBox1.Text = sr.ReadToEnd();
                   sr.Close();
                   chushi = textBox1.Text;
               }
            }
    
            private void 新建ToolStripMenuItem_Click(object sender, EventArgs e)
            {
                if (this.textBox1.Text.Length > 0)
                {
                   DialogResult drg= MessageBox.Show("是否进行保存?","保存对话框",MessageBoxButtons.YesNo);
                   if (DialogResult.Yes == drg)
                   {
                       if (FileName == null)
                       {
                           DialogResult dr = saveFileDialog1.ShowDialog();
                           if (dr == DialogResult.OK)
                           {
                               string filename = saveFileDialog1.FileName;
                               //写入流,可以在硬盘上创建文件,并为文件写入信息
                               StreamWriter sw = new StreamWriter(filename);
                               sw.Write(this.textBox1.Text);
                               sw.Close();
                           }
                       }
                       else
                       {
                           //写入流,可以在硬盘上创建文件,并为文件写入信息
                           StreamWriter sw = new StreamWriter(FileName);
                           sw.Write(this.textBox1.Text);
                           sw.Close();
                       }
                   }
                }
    
                FileName = null;
                this.textBox1.Text = "";
            }
            private string FileName;
            private string chushi;
    
            private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
            {
                if (FileName == null)
                {
                    DialogResult dr = saveFileDialog1.ShowDialog();
                    if (dr == DialogResult.OK)
                    {
                        string filename = saveFileDialog1.FileName;
                        //写入流,可以在硬盘上创建文件,并为文件写入信息
                        StreamWriter sw = new StreamWriter(filename);
                        sw.Write(this.textBox1.Text);
                        sw.Close();
    
                    }
                }
                else
                {
                    //写入流,可以在硬盘上创建文件,并为文件写入信息
                    StreamWriter sw = new StreamWriter(FileName);
                    sw.Write(this.textBox1.Text);
                    sw.Close();
                    bianhua = this.textBox1.Text.Substring(chushi.Length);
                }
            }
    
            private void 另存为ToolStripMenuItem_Click(object sender, EventArgs e)
            {
                saveFileDialog1.Filter = "文本文件(*.txt)|*.txt|word文件(*.doc)|*.doc";
                DialogResult dr = saveFileDialog1.ShowDialog();
                if (dr == DialogResult.OK)
                {
                    string filename = saveFileDialog1.FileName;
                    //写入流,可以在硬盘上创建文件,并为文件写入信息
                    StreamWriter sw = new StreamWriter(filename);
                    sw.Write(this.textBox1.Text);
                    sw.Close();
                }
            }
    
            private void 页面设置ToolStripMenuItem_Click(object sender, EventArgs e)
            {
                pageSetupDialog1.Document = printDocument1;//为页面设置对话框指定打印对象
                pageSetupDialog1.ShowDialog();//打开页面对话框
            }
    
            private void 打印ToolStripMenuItem_Click(object sender, EventArgs e)
            {
               DialogResult dr =  printDialog1.ShowDialog();
               if (dr == DialogResult.OK)
               {
                   printDocument1.Print();
               }
            }
    
            private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
            {
                //设置打印的画板内容
                System.Drawing.Font f = new System.Drawing.Font("宋体", 12);
                e.Graphics.DrawString(this.textBox1.Text, f, SystemBrushes.ActiveBorder, 10.0f, 0f);
            }
    
            private void 查找ToolStripMenuItem_Click(object sender, EventArgs e)
            {
                //Find ff = new Find(this.textBox1.SelectedText, this);
                //ff.Owner = this;
                //ff.Show();
            }
    
            private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
            {
                this.Close();
            }
        }
    }
  • 相关阅读:
    保持唯一性,请停止使用【python3 内置hash() 函数】
    彻底解决go get golang.org/x等包失败与VSCode golang插件安装失败问题
    Linux 任务后台运行软件【即:终端复用器】之---screen
    Ubuntu+uWSGI部署基于Django的API【鸿篇巨制,事无巨细】
    python慎用os.getcwd() ,除非你知道【文件路径与当前工作路径的区别】
    win下youtube-dl 【ERROR: requested format not available】选下载视频质量的坑--【值得一看】
    Mysql失败,异常pymysql.err.InternalError: (1366, "Incorrect string value: '\xF0\x9D\x90\xBF;......
    scrapy post payload的坑及相关知识的补充【POST传参方式的说明及scrapy和requests实现】
    mitmproxy--Cannot establish TLS with client (sni: e.crashlytics.com): TlsException("(-1, 'Unexpected EOF')",) 解决办法
    【GET TIPS】Chrome所见即所得的截图技巧
  • 原文地址:https://www.cnblogs.com/dlexia/p/4648928.html
Copyright © 2011-2022 走看看