C#编写的记事本

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;

namespace WindowsFormsApplication2
{
    public partial class 记事本 : Form
    {

        private bool SaveBefore;

        public 记事本()
        {
            InitializeComponent();
        }

        private void 记事本_Load(object sender, EventArgs e)
        {
            this.Text = "无标题-记事本";
            this.Tag = "";
            SaveBefore = true;
            转到GToolStripMenuItem.Enabled = false;
            if (richTextBox1.Text != "")
            {
                查找FToolStripMenuItem.Enabled = true;
                查找下一个NToolStripMenuItem.Enabled = true;
            }
            else if (richTextBox1.Text == "")
            {
                查找FToolStripMenuItem.Enabled = false;
                查找下一个NToolStripMenuItem.Enabled = false;
            }
        }

        private void 记事本_FormClosing(object sender, FormClosingEventArgs e)
        {
            try
            {
                if (this.Tag.ToString() == "")
                {
                    Convert.ToDouble(this.Tag.ToString());
                }
                if (SaveBefore == false)
                {
                    DialogResult dr = MessageBox.Show("文字已改变,您是否要保存到/r/n" + this.Tag.ToString(), "提示", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information);
                    if (dr == DialogResult.Yes)
                    {
                        this.richTextBox1.SaveFile(this.Tag.ToString(), RichTextBoxStreamType.PlainText);
                        e.Cancel = false;
                    }
                    else if (dr == DialogResult.No)
                    {
                        e.Cancel = false;
                    }
                    else
                    {
                        e.Cancel = true;
                    }

                }
                else
                {
                    e.Cancel = false;
                }
            }
            catch
            {
                if (this.richTextBox1.Text != "")
                {
                    DialogResult dr = MessageBox.Show("文字已改变,您是否要保存到/r/n无标题", "提示", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information);
                    if (dr == DialogResult.Yes)
                    {
                        if (sfd.ShowDialog() == DialogResult.OK)
                        {
                            this.richTextBox1.SaveFile(sfd.FileName, RichTextBoxStreamType.PlainText);
                            e.Cancel = false;
                        }
                        else
                        {
                            e.Cancel = true;
                        }
                    }
                    else if (dr == DialogResult.No)
                    {
                        e.Cancel = false;
                    }
                    else
                    {
                        e.Cancel = true;
                    }
                }
                else
                {
                    e.Cancel = false;
                }
            }
        }

        private void 新建NToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.Tag.ToString() == "")
                {
                    Convert.ToDouble(this.Tag.ToString());
                }
                if (SaveBefore == false)
                {
                    DialogResult dr = MessageBox.Show("文字已改变,您是否要保存到/r/n" + this.Tag.ToString(), "提示", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information);
                    if (dr == DialogResult.Yes)
                    {
                        this.richTextBox1.SaveFile(this.Tag.ToString(), RichTextBoxStreamType.PlainText);
                            this.richTextBox1.Text = "";
                            this.Text = "无标题-记事本";
                            this.Tag = "";
                            SaveBefore = true;
                    }
                    else if (dr == DialogResult.No)
                    {
                        this.richTextBox1.Text = "";
                        this.Text = "无标题-记事本";
                        this.Tag = "";
                        SaveBefore = true;
                    }
                    else
                    {
                        return;
                    }
                }
                else
                {
                    this.richTextBox1.Text = "";
                    this.Text = "无标题-记事本";
                    this.Tag = "";
                    SaveBefore = true;
                }
            }
            catch
            {
                if (this.richTextBox1.Text != "")
                {
                    DialogResult dr = MessageBox.Show("文字已改变,您是否要保存到/r/n无标题", "提示", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information);
                    if (dr == DialogResult.Yes)
                    {
                        if (sfd.ShowDialog() == DialogResult.OK)
                        {
                            this.richTextBox1.SaveFile(sfd.FileName, RichTextBoxStreamType.PlainText);
                            this.richTextBox1.Text = "";
                            this.Text = "无标题-记事本";
                            this.Tag = "";
                            SaveBefore = true;
                        }
                        else
                        {
                            return ;
                        }
                    }
                    else if (dr == DialogResult.No)
                    {
                        this.richTextBox1.Text = "";
                        this.Text = "无标题-记事本";
                        this.Tag = "";
                        SaveBefore = true;
                    }
                    else
                    {
                        return;
                    }
                }
                else
                {
                    this.richTextBox1.Text = "";
                    this.Text = "无标题-记事本";
                    this.Tag = "";
                    SaveBefore = true;
                }
            }
        }

        private void 打开OToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.Tag.ToString() == "")
                {
                    Convert.ToDouble(this.Tag.ToString());
                }
                if (SaveBefore == false)
                {
                    DialogResult dr = MessageBox.Show("文字已改变,您是否要保存到/r/n" + this.Tag.ToString(), "提示", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information);
                    if (dr == DialogResult.Yes)
                    {
                        this.richTextBox1.SaveFile(this.Tag.ToString(), RichTextBoxStreamType.PlainText);
                        if (ofd.ShowDialog() == DialogResult.OK)
                        {
                            this.richTextBox1.LoadFile(ofd.FileName, RichTextBoxStreamType.PlainText);
                            this.Tag = ofd.FileName;
                            this.Text = Path.GetFileNameWithoutExtension(ofd.FileName) + "-记事本";
                            SaveBefore = true;
                        }
                        else
                        {
                            return;
                        }
                    }
                    else if (dr == DialogResult.No)
                    {
                        if (ofd.ShowDialog() == DialogResult.OK)
                        {
                            this.richTextBox1.LoadFile(ofd.FileName, RichTextBoxStreamType.PlainText);
                            this.Tag = ofd.FileName;
                            this.Text = Path.GetFileNameWithoutExtension(ofd.FileName) + "-记事本";
                            SaveBefore = true;
                        }
                        else
                        {
                            return;
                        }
                    }
                    else
                    {
                        return;
                    }
                }
                else
                {
                    if (ofd.ShowDialog() == DialogResult.OK)
                    {
                        this.richTextBox1.LoadFile(ofd.FileName, RichTextBoxStreamType.PlainText);
                        this.Tag = ofd.FileName;
                        this.Text = Path.GetFileNameWithoutExtension(ofd.FileName) + "-记事本";
                        SaveBefore = true;
                    }
                    else
                    {
                        return;
                    }
                }
            }
            catch
            {
                if (this.richTextBox1.Text != "")
                {
                    DialogResult dr = MessageBox.Show("文字已改变,您是否要保存到/r/n无标题", "提示", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information);
                    if (dr == DialogResult.Yes)
                    {
                        if (sfd.ShowDialog() == DialogResult.OK)
                        {
                            this.richTextBox1.SaveFile(sfd.FileName, RichTextBoxStreamType.PlainText);
                            SaveBefore = true;
                            if (ofd.ShowDialog() == DialogResult.OK)
                            {
                                this.richTextBox1.LoadFile(ofd.FileName, RichTextBoxStreamType.PlainText);
                                this.Tag = ofd.FileName;
                                this.Text = Path.GetFileNameWithoutExtension(ofd.FileName) + "-记事本";
                            }
                            else
                            {
                                return;
                            }
                        }
                        else
                        {
                            return;
                        }
                    }
                    else if (dr == DialogResult.No)
                    {
                        if (ofd.ShowDialog() == DialogResult.OK)
                        {
                            this.richTextBox1.LoadFile(ofd.FileName, RichTextBoxStreamType.PlainText);
                            this.Tag = ofd.FileName;
                            this.Text = Path.GetFileNameWithoutExtension(ofd.FileName) + "-记事本";
                            SaveBefore = true;
                        }
                        else
                        {
                            return;
                        }
                    }
                    else
                    {
                        return;
                    }   
                }
                else
                {
                    if (ofd.ShowDialog() == DialogResult.OK)
                    {
                        this.richTextBox1.LoadFile(ofd.FileName, RichTextBoxStreamType.PlainText);
                        this.Tag = ofd.FileName;
                        this.Text = Path.GetFileNameWithoutExtension(ofd.FileName) + "-记事本";
                        SaveBefore = true;
                    }
                    else
                    {
                        return;
                    }
                }
            }
        }

        private void 保存SToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.Tag.ToString() == "")
                {
                    Convert.ToDouble(this.Tag);
                }
                this.richTextBox1.SaveFile(this.Tag.ToString(), RichTextBoxStreamType.PlainText);
                SaveBefore = true;
            }
            catch
            {
                if (sfd.ShowDialog() == DialogResult.OK)
                {
                    this.richTextBox1.SaveFile(sfd.FileName, RichTextBoxStreamType.PlainText);
                    this.Text = Path.GetFileNameWithoutExtension(sfd.FileName) + "-记事本";
                    this.Tag = sfd.FileName;
                    SaveBefore = true;
                }
                else
                {
                    return;
                }
            }
        }

        private void 另存为AToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                this.richTextBox1.SaveFile(sfd.FileName, RichTextBoxStreamType.PlainText);
                SaveBefore = true;
                this.Tag = sfd.FileName;
                this.Text = Path.GetFileNameWithoutExtension(sfd.FileName) + "-记事本";
            }
        }

        private void 页面设置UToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //PageSetupDialog psd = new PageSetupDialog() ;
            psd.ShowDialog();
        }

        private void 打印PToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void 打印预览VToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

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

        private void 撤消UToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.richTextBox1.Undo();
        }

        private void 剪切TToolStripMenuItem_Click(object sender, EventArgs e)
        {
            richTextBox1.Cut();
        }

        private void 复制CToolStripMenuItem_Click(object sender, EventArgs e)
        {
            richTextBox1.Copy();
        }

        private void 粘贴PToolStripMenuItem_Click(object sender, EventArgs e)
        {
            richTextBox1.Paste();
        }

        private void 删除LToolStripMenuItem_Click(object sender, EventArgs e)
        {
            richTextBox1.SelectedText = "";
        }

        private void 查找FToolStripMenuItem_Click(object sender, EventArgs e)
        {
            查找 chazhao = new 查找();
            chazhao.Owner = this;
            chazhao.Show();
        }

        private void 查找下一个NToolStripMenuItem_Click(object sender, EventArgs e)
        {
            查找 chazhao = new 查找();
            chazhao.Owner = this;
            chazhao.Show();
        }

        private void 替换RToolStripMenuItem_Click(object sender, EventArgs e)
        {
            替换 tihuan = new 替换();
            tihuan.Owner = this;
            tihuan.Show();
        }

        private void 转到GToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void 全选AToolStripMenuItem_Click(object sender, EventArgs e)
        {
            richTextBox1.SelectAll();
        }

        private void 时间日期DToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DateTime Time = DateTime.Now;
            richTextBox1.Text += Time;
        }//日期注入位置不对

        private void 自动换行WToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (自动换行WToolStripMenuItem.CheckState == CheckState.Unchecked)
            {
                this.richTextBox1.WordWrap =true;
                自动换行WToolStripMenuItem.CheckState = CheckState.Checked;
            }
            else if (自动换行WToolStripMenuItem.CheckState == CheckState.Checked)
            {
                this.richTextBox1.WordWrap =false;
                自动换行WToolStripMenuItem.CheckState = CheckState.Unchecked;
            }
        }

        private void 字体FToolStripMenuItem_Click(object sender, EventArgs e)
        {
            fd.ShowDialog();
            richTextBox1.Font = fd.Font;
        }

        private void richTextBox1_Click(object sender, EventArgs e)
        {
            if (richTextBox1.SelectedText == "")
            {
                复制CToolStripMenuItem.Enabled = false;
                复制toolStripMenuItem.Enabled = false;
                剪切TToolStripMenuItem.Enabled = false;
                剪切toolStripMenuItem.Enabled = false;
                删除LToolStripMenuItem.Enabled = false;
                删除toolStripMenuItem.Enabled = false;
            }
            else
            {
                复制CToolStripMenuItem.Enabled = true;
                复制toolStripMenuItem.Enabled = true;
                剪切TToolStripMenuItem.Enabled = true;
                剪切toolStripMenuItem.Enabled = true;
                删除LToolStripMenuItem.Enabled = true;
                删除toolStripMenuItem.Enabled = true;
            }
            IDataObject data;
            data = Clipboard.GetDataObject();
            if (data.GetDataPresent(DataFormats.Text))
            {
                粘贴PToolStripMenuItem.Enabled = true;
                粘贴toolStripMenuItem.Enabled = true;
            }
            else
            {
                粘贴PToolStripMenuItem.Enabled = false;
                粘贴toolStripMenuItem.Enabled = false;
            }

            if (richTextBox1.Text != "")
            {
                查找FToolStripMenuItem.Enabled = true;
                查找下一个NToolStripMenuItem.Enabled = true;
            }
            else
            {
                查找FToolStripMenuItem.Enabled = false;
                查找下一个NToolStripMenuItem.Enabled = false;
            }
        }

        private void richTextBox1_TextChanged(object sender, EventArgs e)
        {
            SaveBefore = false;
            if (richTextBox1.Text != "")
            {
                查找FToolStripMenuItem.Enabled = true;
                查找下一个NToolStripMenuItem.Enabled = true;
            }
            else if (richTextBox1.Text == "")
            {
                查找FToolStripMenuItem.Enabled = false;
                查找下一个NToolStripMenuItem.Enabled = false;
            }
        }

        private void 查看帮助HToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("这是一个仿照windows的记事本应用程序");
        }

        private void 关于记事本AToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("计算器---作者:陈林---联系方式QQ:772622524");                   

        }


    }
}

 

 

记事本的打印功能没做

大家多多指证!

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值