一个简单的记事本

public partial class Form1 : Form
    {
        public int num=0;
        public Form1()
        {
            InitializeComponent();
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
         
            string nums;
            num++;
            nums=num.ToString();
            Form2 frm = new Form2();
            frm.MdiParent=this;//设置form1为form2的父窗口;
            ToolStripMenuItem newWindowItem = new ToolStripMenuItem(frm.Text +nums+".txt");
            窗口HToolStripMenuItem.DropDownItems.Add(newWindowItem);
           
            frm.Show();

        }

        private void 新建ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string nums;
            num++;
            nums = num.ToString();
            Form2 frm = new Form2();
            ToolStripMenuItem newWindowItem = new ToolStripMenuItem(frm.Text + nums + ".txt");
            窗口HToolStripMenuItem.DropDownItems.Add(newWindowItem);
            frm.MdiParent = this;
            frm.Show();
        }

        private void 帮助主题ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.LayoutMdi(MdiLayout.Cascade);
        }

        private void 关于记事本ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.LayoutMdi(MdiLayout.TileVertical);
        }

        private void 水平平铺IToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.LayoutMdi(MdiLayout.TileHorizontal);
        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void toolStripButton2_Click(object sender, EventArgs e)//打开文件对话框
        {
            openFileDialog1.Title = "我的记事本---打开文本对话框";
            string dir = @"C:/";//设置目录
            openFileDialog1.InitialDirectory = dir;
            string filter = "文本文件(*.txt)|*.txt";//设置过虑器
            openFileDialog1.Filter = filter;
            if (openFileDialog1.ShowDialog() == DialogResult.Cancel)
            {
                return;

            }
            else
            {
                Form2 frm = new Form2();
                frm.Text = openFileDialog1.FileName;
                StreamReader sr = new StreamReader(openFileDialog1.OpenFile());

                frm.richTextBox1.Text = sr.ReadToEnd();
                frm.MdiParent = this;
                frm.Show();
           
            }
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            if (this.MdiChildren.Length == 0)
            {
                return;
            }
            saveFileDialog1.Title = "我的记事本---打开保存对话框";
            string filter = "文本文件(*.txt)|*.txt";
            saveFileDialog1.Filter = filter;
            string dir = @"C:/";
            saveFileDialog1.InitialDirectory = dir;

            if (saveFileDialog1.ShowDialog() == DialogResult.Cancel)
            {
                return;

            }
            else//存在问题:写不进文件内容
            {
                StreamWriter sw = new StreamWriter(saveFileDialog1.FileName);

                Form2 frm = (Form2)ActiveMdiChild;
                sw.Write(frm.richTextBox1.Text);
                sw.Close();

            }

       
        }

        private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            openFileDialog1.Title = "我的记事本---打开文本对话框";
            string dir = @"C:/";//设置目录
            openFileDialog1.InitialDirectory = dir;
            string filter = "文本文件(*.txt)|*.txt";//设置过虑器
            openFileDialog1.Filter = filter;
            if (openFileDialog1.ShowDialog() == DialogResult.Cancel)
            {
                return;

            }
            else
            {
                Form2 frm = new Form2();
                frm.Text = openFileDialog1.FileName;
                StreamReader sr = new StreamReader(openFileDialog1.OpenFile());

                frm.richTextBox1.Text = sr.ReadToEnd();
                frm.MdiParent = this;
                frm.Show();

            }

        }

        private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.MdiChildren.Length == 0)
            {
                return;
            }
            saveFileDialog1.Title = "我的记事本---打开保存对话框";
            string filter = "文本文件(*.txt)|*.txt";
            saveFileDialog1.Filter = filter;
            string dir = @"C:/";
            saveFileDialog1.InitialDirectory = dir;

            if (saveFileDialog1.ShowDialog() == DialogResult.Cancel)
            {
                return;

            }
           else
            {
                StreamWriter sw = new StreamWriter(saveFileDialog1.FileName);
          
                Form2 frm = (Form2)ActiveMdiChild;
                sw.Write(frm.richTextBox1.Text);
                sw.Close();

            }
        }
        private string text = string.Empty;

        private void printDocument1_BeginPrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
      
            text = ((Form2)ActiveMdiChild).richTextBox1.Text;
          
        }

        private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
           
            e.Graphics.DrawString(text, new Font("Arial", 10), Brushes.Black, 20, 20);
        }

        private void toolStripButton4_Click(object sender, EventArgs e)//打印按钮
        {
            if (MdiChildren.Length == 0)
                return;
            if (printDialog1.ShowDialog() == DialogResult.Cancel)
                return;
            else
                printDocument1.Print();


           
        }

        private void toolStripButton5_Click(object sender, EventArgs e)//打印预览按钮
        {
            if (MdiChildren.Length == 0)
            {
                return;
            }

            printPreviewDialog1.Document = printDocument1;
            printPreviewDialog1.ShowDialog();
        }

        private void toolStripButton10_Click(object sender, EventArgs e)
        {
            fontDialog1.ShowDialog();
        }

        private void toolStripButton11_Click(object sender, EventArgs e)
        {
            colorDialog1.ShowDialog();
        }

       
      

       
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值