666

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace Notepad__
{
    public partial class form1 : Form
    {
        public form1()
        {
            InitializeComponent();
        }


        private Boolean insert;


        private void Form1_Load(object sender, EventArgs e)
        {
            insert = true;
            this.toolStripStatusLabel1.Text = "就绪";
            this.toolStripStatusLabel2.Text = "插入";
            this.toolStripStatusLabel1.Text = DateTime.Now.ToLongTimeString();
        }


        private void tsMenuItemFile_Click(object sender, EventArgs e)
        {
            //根据RichTextBox中有文本,
            //决定新建和打开菜单项是否可用
            if (rtextContent.Text != "")
            {
                tsMenuItemNew.Enabled = true;
                tsMenuItemSave.Enabled = true;
            }
            else
            {
                tsMenuItemNew.Enabled = false;
                tsMenuItemSave.Enabled = false;
            }
        }


        private void tsMenuItemSave_Click(object sender, EventArgs e)
        {
            //改写状态栏第一个窗格的文本
            this.toolStripStatusLabel1.Text = "保存文件";
            //创建SaveFileDialog实例
            SaveFileDialog SaveFileDlg = new SaveFileDialog();
            //设置文件类型过滤器
            SaveFileDlg.Filter = "文本文件(*.txt)|*.txt";
            //创建文件时提示
            SaveFileDlg.CreatePrompt = true;
            //自动加上扩展名
            SaveFileDlg.AddExtension = true;
            //单击"OK",选择了要打开的文件
            if (SaveFileDlg.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    //获取要保存的文件名
                    string fileName = SaveFileDlg.FileName;
                    //建立StreaWrite实例
                    StreamWriter writer = new StreamWriter(fileName);
                    //打开文件,将RichTextBox中的文本写入文件
                    writer.Write(rtextContent.Text);
                    //关闭流
                    writer.Close();
                }
                catch (Exception Exc)
                {
                    MessageBox.Show(Exc.Message);
                }
            }
            //改写状态栏第一个窗格的文本
            this.toolStripStatusLabel1.Text = "正在编辑";
        }


        private void tsMenuItemNew_Click(object sender, EventArgs e)
        {
            //如果RichTextBox中有文本
            if (rtextContent.Text != "")
            {
                //提示用户是否保存文件
                DialogResult result = MessageBox.Show("要保存正在编辑的文件否?", "保存文件",
                    MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
                if (result == DialogResult.Yes)
                { //回答“Yes”就保存文件
                    tsMenuItemSave_Click(null, null);
                    rtextContent.Clear();
                }
                if (result == DialogResult.No)
                    rtextContent.Clear();
            }
            //改写状态栏第一个窗格的文本
            toolStripStatusLabel1.Text = "就绪";
        }


        private void tsMenuItemOpen_Click(object sender, EventArgs e)
        {
            //改写状态栏第一个窗格的文本
            toolStripStatusLabel1.Text = "打开文件";
            //如果RichTextBox中有文本
            if (rtextContent.Text != "")
            {
                //提示用户是否保存文件
                DialogResult result = MessageBox.Show("要保存正在编辑的文件否?", "保存文件",
                MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
                //回答“Yes”就保存文件
                if (result == DialogResult.Yes)
                    tsMenuItemSave_Click(null, null);
            }
            //创建一个OpenFileDialog实例
            OpenFileDialog OpenFileDlg = new OpenFileDialog();
            //设置文件类型过滤器
            OpenFileDlg.Filter = "文本文件(*.txt)|*.txt";
            //不允许选择多个文件
            OpenFileDlg.Multiselect = false;
            //单击"OK",选择了要打开的文件
            if (OpenFileDlg.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    //获取要打开的文件名
                    string fileName = OpenFileDlg.FileName;
                    //建立文件信息对象
                    FileInfo f = new FileInfo(fileName);
                    //打开文件,将文本读入流中
                    StreamReader reader = f.OpenText();
                    //将流中的文本输出到RichTextBox中
                    rtextContent.Text = reader.ReadToEnd();
                    //关闭流
                    reader.Close();
                }
                catch (Exception Exc)
                {
                    MessageBox.Show(Exc.Message);
                }
            }
            //改写状态栏第一个窗格的文本
            if (rtextContent.Text.Length == 0)
            {
                toolStripStatusLabel1.Text = "就绪";
            }
            else
            {
                toolStripStatusLabel1.Text = "正在编辑";
            }
        }


        private void tsMenuItemExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }


        private void tsMenuItemEdit_Click(object sender, EventArgs e)
        {
            //根据剪贴板中有无格式为Text的数据,
            //决定tsMenuItemPaste菜单项是否可用
            if (Clipboard.GetDataObject().GetDataPresent(DataFormats.Text))
                tsMenuItemPaste.Enabled = true;
            else
                tsMenuItemPaste.Enabled = false;
            //根据文本框中是否选择了文本,
            //决定tsMenuItemCut,tsMenuItemCopy,tsMenuItemDelete是否可用
            if (rtextContent.SelectionLength > 0)
            {
                tsMenuItemCut.Enabled = true;
                tsMenuItemCopy.Enabled = true;
                tsMenuItemDelete.Enabled = true;
            }
            else
            {
                tsMenuItemCut.Enabled = false;
                tsMenuItemCopy.Enabled = false;
                tsMenuItemDelete.Enabled = false;
            }
            //根据文本框中是否可执行撤消操作
            //决定tsMenuItemUndo是否可用
            if (rtextContent.CanUndo == true)
                tsMenuItemUndo.Enabled = true;
            else
                tsMenuItemUndo.Enabled = false;
        }


        private void tsMenuItemUndo_Click(object sender, EventArgs e)
        {
            //当RichTextBox可以进行撤消操作时
            if (rtextContent.CanUndo == true)
            {
                //调用TextBox.Undo()方法,撤消上一次操作
                rtextContent.Undo();
                //清除缓冲区中有关撤消操作的信息
                rtextContent.ClearUndo();
            }
        }


        private void tsMenuItemCut_Click(object sender, EventArgs e)
        {
            //如果RichTextBox中选择的文本非空
            if (rtextContent.SelectedText != "")
            {
                //调用TextBox.Cut()方法,剪切选中的文本
                rtextContent.Cut();
            }
        }


        private void tsMenuItemCopy_Click(object sender, EventArgs e)
        {
            //如果RichTextBox中选择的文本长度大于0
            if (rtextContent.SelectionLength > 0)
            {
                //调用TextBox.Copy()方法,复制选中的文本
                rtextContent.Copy();
            }
        }


        private void tsMenuItemPaste_Click(object sender, EventArgs e)
        {
            //如果剪贴板中有数据,并且该数据的格式是文本Text
            if (Clipboard.GetDataObject().GetDataPresent(DataFormats.Text) == true)
            {
                //调用TextBox.Paste()方法,将剪贴板的文本粘贴到文本框
                rtextContent.Paste();
            }
        }


        private void tsMenuItemDelete_Click(object sender, EventArgs e)
        {
            //移除字符的起点
            int start = rtextContent.SelectionStart;
            //移除字符的个数
            int count = rtextContent.SelectionLength;
            //移除选择的字符
            rtextContent.Text = rtextContent.Text.Remove(start, count);
            //将光标置于原先选择的字符初
            rtextContent.SelectionStart = start;
        }


        private void tsMenuItemAll_Click(object sender, EventArgs e)
        {
            rtextContent.SelectAll();
        }


        private void tsMenuItemFont_Click(object sender, EventArgs e)
        {
            FontDialog FontDlg = new FontDialog();
            //使字体对话框上显示颜色选择框
            FontDlg.ShowColor = true;
            if (FontDlg.ShowDialog() == DialogResult.OK)
            {
                //改变文本框中的字体和颜色
                rtextContent.Font = FontDlg.Font;
                rtextContent.ForeColor = FontDlg.Color;
            }
        }


        private void tsMenuItemColor_Click(object sender, EventArgs e)
        {
            ColorDialog ColorDlg = new ColorDialog();
            if (ColorDlg.ShowDialog(this) == DialogResult.OK)
            {
                //改变文本框中文字的颜色
                rtextContent.ForeColor = ColorDlg.Color;
            }
        }


        private void rtextContent_MouseDown(object sender, MouseEventArgs e)
        {
            //若按下鼠标右键
            if (e.Button.ToString() == "Right")
            {
                Point pt = new Point(e.X, e.Y);
                //显示快捷菜单
                contextMenuStrip1.Show(rtextContent, pt);
            }
        }


        private void contextMenuStrip1_Opened(object sender, EventArgs e)
        {
            //根据剪贴板中有无格式为Text的数据,
            //决定contextMenuItemPaste菜单项是否可用
            if (Clipboard.GetDataObject().GetDataPresent(DataFormats.Text))
                contextMenuItemPaste.Enabled = true;
            else
                contextMenuItemPaste.Enabled = false;
            //根据文本框中是否选择了文本,
            //决定contextMenuItemCut,contextMenuItemCopy,contextMenuItemDelete是否可用
            if (rtextContent.SelectionLength > 0)
            {
                contextMenuItemCut.Enabled = true;
                contextMenuItemCopy.Enabled = true;
                contextMenuItemDelete.Enabled = true;
            }
            else
            {
                contextMenuItemCut.Enabled = false;
                contextMenuItemCopy.Enabled = false;
                contextMenuItemDelete.Enabled = false;
            }
            //根据文本框中是否可执行撤消操作
            //决定contextMenuItemUndo是否可用
            if (rtextContent.CanUndo == true)
                contextMenuItemUndo.Enabled = true;
            else
                contextMenuItemUndo.Enabled = false;
        }


        private void contextMenuItemUndo_Click(object sender, EventArgs e)
        {
            tsMenuItemUndo_Click(null, null);
        }


        private void contextMenuItemCut_Click(object sender, EventArgs e)
        {
            tsMenuItemCut_Click(null, null);
        }


        private void contextMenuItemCopy_Click(object sender, EventArgs e)
        {
            tsMenuItemCopy_Click(null, null);
        }


        private void contextMenuItemPaste_Click(object sender, EventArgs e)
        {
            tsMenuItemPaste_Click(null, null);
        }


        private void contextMenuItemDelete_Click(object sender, EventArgs e)
        {
            tsMenuItemDelete_Click(null, null);
        }


        private void tsButtonNew_Click(object sender, EventArgs e)
        {
            tsMenuItemNew_Click(null, null);
        }


        private void tsButtonOpen_Click(object sender, EventArgs e)
        {
            tsMenuItemOpen_Click(null, null);
        }


        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            tsMenuItemSave_Click(null, null);
        }


        private void tsButtonUndo_Click(object sender, EventArgs e)
        {
            tsMenuItemUndo_Click(null, null);
        }


        private void tsButtonCut_Click(object sender, EventArgs e)
        {
            tsMenuItemCut_Click(null, null);
        }


        private void tsButtonCopy_Click(object sender, EventArgs e)
        {
            tsMenuItemCopy_Click(null, null);
        }


        private void tsButtonPaste_Click(object sender, EventArgs e)
        {
            tsMenuItemPaste_Click(null, null);
        }


        private void tsButtonDel_Click(object sender, EventArgs e)
        {
            tsMenuItemDelete_Click(null, null);
        }


        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            //如果RichTextBox中有文本
            if (rtextContent.Text != "")
            {
                //提示用户是否保存文件
                DialogResult result = MessageBox.Show("要保存正在编辑的文件否?", "保存文件",
                    MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
                if (result == DialogResult.Yes)
                { //回答“Yes”就保存文件
                    tsMenuItemSave_Click(null, null);
                }
            }
        }


        private void rtextContent_KeyDown(object sender, KeyEventArgs e)
        {
            //若按下了“Insert”键
            if (e.KeyValue == 45)
            {
                //若当前为插入状态
                if (insert == true)
                {
                    //插入标记改为false
                    insert = false;
                    //修改状态栏第二个窗格的文本
                    toolStripStatusLabel2.Text = "改写";
                }
                //若当前为改写状态
                else
                {
                    //插入标记改为true
                    insert = true;
                    //修改状态栏第二个窗格的文本
                    toolStripStatusLabel2.Text = "插入";
                }
            }
        }


        private void timer1_Tick(object sender, EventArgs e)
        {
            //将系统当前时间显示在状态栏的第三个窗格中
            this.toolStripStatusLabel3.Text = DateTime.Now.ToLongTimeString();
        }


        private void rtextContent_TextChanged(object sender, EventArgs e)
        {
            //修改状态栏第一个窗格的文本
            if (rtextContent.Text.Length == 0)
            {
                toolStripStatusLabel1.Text = "就绪";
            }
            else
            {
                toolStripStatusLabel1.Text = "正在编辑";
            }
        }


        private void Form1_Load_1(object sender, EventArgs e)
        {


        }


    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值