C#.net制作一个简单的记事本

时间:2024-03-13 08:52:34

用C#.net一个简单的记事本

用到的控件有RichTextBox(有格式文本框控件),Menutrip(菜单控件),toolStrip(工具栏控件),statusStrip(状态栏控件),timer(计时器控件).
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 WindowsFormsApp12
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string filename; //设置全局变量,后面用来存储文件的路径
bool saveflag = true; //设置一个用于判断的标志
private void mnufile_new_Click(object sender, EventArgs e)//新建按钮
{
if (saveflag == true) //判断是否执行了保存操作
{
if (richTextBox1.Modified)
{
if (MessageBox.Show(“内容已经修改,是否保存?”, “警告”, MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK)
mnufile_save_Click(sender, e);
}
}

        richTextBox1.Clear();                       //将richtext中的文本清空
    }

    private void mnufile_open_Click(object sender, EventArgs e)
    {
        OpenFileDialog OpenFileDialog = new OpenFileDialog();//openfiledialog用于创建标准对话框,提示用户打开文件
        OpenFileDialog.Filter = "文本文件|*.txt|word文档|*.doc";//确定打开文件的类型
        if (OpenFileDialog.ShowDialog() == DialogResult.OK)
        {
            filename = OpenFileDialog.FileName;
            StreamReader sr = new StreamReader(filename, Encoding.GetEncoding("GB2312"));//Encoding表示对文件的打开格式的限定
            richTextBox1.Text = sr.ReadToEnd();//ReadToEnd()表示读取从当前位置到结尾的所有字符
            sr.Close();
        }

    }

    private void mnufile_save_Click(object sender, EventArgs e)//文件的保存
    {
        if (filename == null)                               //如果路径为null,则执行另存方法
        {
            mnufile_saveas_Click(sender, e);
        }
        string result = richTextBox1.Text.ToString();
        if (result == "" || result.Length == 0)
        {
            MessageBox.Show("保存文本为空", "警告");
        }
        else
        {
            FileStream fs = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.Write);
            StreamWriter Sw = new StreamWriter(fs, Encoding.GetEncoding("GB2312"));//通过指定字符编码方式可以实现对汉字的支持,否则在用记事本打开查看会出现乱码 
            Sw.Write(result);
            Sw.Flush();
            Sw.Close();
            Console.Beep();
            MessageBox.Show("保存成功!", "通知");
        }
        saveflag = false;
    }

    private void mnufile_saveas_Click(object sender, EventArgs e)
    {
        SaveFileDialog saveFileDialog = new SaveFileDialog();
        saveFileDialog.Title = "请选择要保存的路径";//设置保存文件对话框的标题
        saveFileDialog.Filter = "文本文件|*.txt|word文件|*.doc|所有文件|*.*";//设置保存的格式
        if (saveFileDialog.ShowDialog() == DialogResult.OK)
        {
            string result = richTextBox1.Text;
            filename = saveFileDialog.FileName;
            StreamWriter Sw = new StreamWriter(filename);
            Sw.Write(result);
            Sw.Flush();
            Sw.Close();
        }
    }

    private void mnufile_exit_Click(object sender, EventArgs e)
    {
        this.Close();                           //关闭form
    }

    private void mnuedit_undo_Click(object sender, EventArgs e)
    {
        if (richTextBox1.CanUndo)
            richTextBox1.Undo();                //撤销上一步的操作

    }

    private void mnuedit_copy_Click(object sender, EventArgs e)
    {
        richTextBox1.Copy();                    //复制选中的内容
    }

    private void mnuedit_cut_Click(object sender, EventArgs e)
    {
        richTextBox1.Cut();                     //剪切选中的内容
    }

    private void mnuedit_paste_Click(object sender, EventArgs e)
    {
        richTextBox1.Paste();                       //粘贴在剪切板中的内容
    }

    private void mnuedit_clear_Click(object sender, EventArgs e)
    {
        richTextBox1.Clear();                       //将richtext中的所有文本清空
    }

    private void mnuview_tool_Click(object sender, EventArgs e)
    {
        toolStrip1.Visible = true;                  //设置工具栏可见

    }

    private void mnuview_stat_Click(object sender, EventArgs e)
    {
        statusStrip1.Visible = true;
    }

    private void mnu_font_Click(object sender, EventArgs e)
    {
        FontDialog font = new FontDialog();         //设置字体
        font.ShowColor = true;
        font.Color = richTextBox1.SelectionColor;
        font.Font = richTextBox1.SelectionFont;
        if (font.ShowDialog() == DialogResult.OK)
        {
            richTextBox1.SelectionFont = font.Font;
            richTextBox1.SelectionColor = font.Color;
        }
    }

    private void mnu_wordwrap_Click(object sender, EventArgs e)
    {
        mnu_wordwrap.Checked = richTextBox1.WordWrap;
    }

    private void mnuform_color_Click(object sender, EventArgs e)
    {
        ColorDialog color = new ColorDialog();          //设置字体的颜色
        color.AllowFullOpen = true;
        color.AnyColor = true;
        color.Color = richTextBox1.SelectionColor;
        if (color.ShowDialog() == DialogResult.OK)
            richTextBox1.SelectionColor = color.Color;
    }

    private void Form1_AutoSizeChanged(object sender, EventArgs e)
    {
        MessageBox.Show("你在修改窗体的大小!");      //修改窗口大小是进行提示
    }

    private void toolStripMenuItem1_Click(object sender, EventArgs e)
    {
        richTextBox1.Copy();
    }

    private void toolStripMenuItem2_Click(object sender, EventArgs e)
    {
        richTextBox1.Paste();
    }

    private void toolStripMenuItem3_Click(object sender, EventArgs e)
    {
        richTextBox1.Cut();
    }

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

    private void timer1_Tick(object sender, EventArgs e)
    {
        statu2.Text = DateTime.Now.ToString();
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    private void openbutton_Click(object sender, EventArgs e)
    {
        mnufile_open_Click(sender, e);
    }

    private void copybutton_Click(object sender, EventArgs e)
    {
        mnuedit_copy_Click(sender, e);
    }

    private void cutbutton_Click(object sender, EventArgs e)
    {
        mnuedit_cut_Click(sender, e);
    }

    private void savebutton_Click(object sender, EventArgs e)
    {
        mnufile_save_Click(sender, e);
    }

    private void newbutton_Click(object sender, EventArgs e)
    {
        mnufile_new_Click(sender, e);
    }

    private void Form1_FormClosed(object sender, FormClosedEventArgs e)
    {
        if (richTextBox1.Modified)
        {
            if (MessageBox.Show("文件还没有保存,是否退出", "警告", MessageBoxButtons.OKCancel) == DialogResult.Cancel)    //退出程序时进行警告
                MessageBox.Show("老夫无能为力了!");

        }
    }
}

}
C#.net制作一个简单的记事本