簡單的記事本功能實現代碼--(流讀取器)

來源:互聯網
上載者:User

標籤:

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));        }    }}

使用流讀取器注意命名空間的引用

簡單的記事本功能實現代碼--(流讀取器)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.