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

 

posted @ 2015-07-15 17:15  Yusarin  阅读(241)  评论(0编辑  收藏  举报