• 简单的记事本功能实现代码--(流读取器)


    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 WindowsFormsApplication2
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void 剪切TToolStripMenuItem_Click(object sender, EventArgs e)
            {
                textBox1.Cut();
            }
    
            private void 复制CToolStripMenuItem_Click(object sender, EventArgs e)
            {
                textBox1.Copy();
            }
    
            private void 粘贴PToolStripMenuItem_Click(object sender, EventArgs e)
            {
                textBox1.Paste();
            }
    
            private void 全选AToolStripMenuItem_Click(object sender, EventArgs e)
            {
                textBox1.SelectAll();
            }
    
            private void 撤消UToolStripMenuItem_Click(object sender, EventArgs e)
            {
                textBox1.Undo();
            }
    
            private string FileName;
    
            private void 打开OToolStripMenuItem_Click(object sender, EventArgs e)
            {
                //创建打开文件对话框:帮我们去硬盘上查找文件路径
               // OpenFileDialog openfiledialog1 = new OpenFileDialog();//创建对象
                openFileDialog1.Filter = "文本文件|*.txt|所有文件|*.*";//过滤
                DialogResult isok = openFileDialog1.ShowDialog();//显示对话框
                if (isok == DialogResult.OK)//通过对话框的返回值来判断是否点的打开按钮
                { 
                    //获取文件路径
                    string filename = openFileDialog1.FileName;
                    FileName = filename;
                    //根据文件路径去读取文件:使用流来读取
                    //FileStream fs = new FileStream(filename,FileMode.Open);
                    //StreamReader sr = new StreamReader(fs,Encoding.Default);//流读取器--Encoding.Default 选择默认编码
                    StreamReader sr = new StreamReader(filename,Encoding.Default);
                    string text = sr.ReadToEnd();//读取
                    sr.Close();
                   // fs.Close();
    
                    textBox1.Text = text;
                }
            }
    
            private void 保存SToolStripMenuItem_Click(object sender, EventArgs e)
            {
                if (FileName != "")
                {
                    FileStream fs = new FileStream(FileName, FileMode.Create);
                    StreamWriter sw = new StreamWriter(fs, Encoding.Default);//流写入
                    sw.Write(textBox1.Text);
                    sw.Close();
                    fs.Close();
                }
                else
                {
                    SaveFileDialog savefiledialog1 = new SaveFileDialog();
                    savefiledialog1.Filter = "文本文件|*.txt|所有文件|*.*";
                    DialogResult isok = savefiledialog1.ShowDialog();
                    if (isok == DialogResult.OK)
                    {
                        string filename = savefiledialog1.FileName;
    
                        FileStream fs = new FileStream(filename, FileMode.Create);
                        StreamWriter sw = new StreamWriter(fs, Encoding.Default);//流写入
                        sw.Write(textBox1.Text);
                        sw.Close();
                        fs.Close();
                    }
                }
            }
    
            private void 退出XToolStripMenuItem_Click(object sender, EventArgs e)
            {
    
                this.Close();
            }
    
            private void 查找ToolStripMenuItem_Click(object sender, EventArgs e)
            {
                Form2 f = new Form2(textBox1.SelectedText,this);
                f.Owner = this;
                f.Show();
            }
    
    
            private void 替换ToolStripMenuItem_Click(object sender, EventArgs e)
            {
               
            }
    
            private void 打印预览VToolStripMenuItem_Click(object sender, EventArgs e)
            {
                printPreviewDialog1.ShowDialog();
            }
    
            private void 打印PToolStripMenuItem_Click(object sender, EventArgs e)
            {
                printDialog1.Document = printDocument1;//指定打印对话框所对应的打印对象
                DialogResult isok = printDialog1.ShowDialog();
                if (isok == DialogResult.OK)
                {
                    printDocument1.Print();//打印文档
                }
            }
    
            private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
            {
                //设置打印内容
                Font f = new System.Drawing.Font("宋体",6);
                e.Graphics.DrawString(textBox1.Text,f,System.Drawing.Brushes.Black,new PointF(0,0));
    
            }
        }
    }

    使用流读取器注意命名空间的引用

  • 相关阅读:
    kubernetes概述
    pygame--图形模块
    iPhone 上你可能还不知道的小技巧
    使用 JdbcTemplate 查询数据时报错:列名无效(已解决)
    【分享】怎样做架构师?
    关于 oh-my-zsh 插件的使用(以 Sublime Text 为例)
    VI/VIM 无法使用系统剪贴板(clipboard)?(Ubuntu&Mac OS X已解决)
    Ubuntu 下 Sublime 无法输入中文?(已解决)
    VIM 的帮助文档在哪里?看这里。
    推荐一款好用的文件/文件夹对比工具 —— Beyond Compare
  • 原文地址:https://www.cnblogs.com/nwj-0613/p/5235723.html
Copyright © 2020-2023  润新知