VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > c#编程 >
  • 用C#写一个记事本 二

注释部分需自行添加
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;//提供了关于文件、数据流python基础教程
的读取和写入操作
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;//提供了用于与事件日志、性能计数器和系统进程进行交互c#教程的类

1.新建文件:

private void 新建NToolStripMenuItem_Click(object sender, EventArgs e)
{
    if (txtBox.Modified == true)
    {
        DialogResult dr = MessageBox.Show("文件发生变化,是否更改保存?", "注意", MessageBoxButtons.YesNoCancel);
        if (dr == DialogResult.Yes)
        {
            保存SToolStripMenuItem_Click(sender, e);
            return;
        }
        else if (dr == DialogResult.Cancel)
        {
            return;
        }
        txtBox.Clear();
        this.Text = "NewNotepad";
    }
    else
    {
        txtBox.Clear();
        this.Text = "NewNotepad";
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

2.打开:

  private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
    {
        if (openFileDialog.ShowDialog() == DialogResult.OK)
        {
            filename = openFileDialog.FileName;
            OpenFile();
        }
    }
    protected void OpenFile()
    {
        try
        {
            txtBox.Clear();
            txtBox.Text = File.ReadAllText(filename);
        }
        catch
        { MessageBox.Show("Error!"); }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

3.保存:

private void 保存SToolStripMenuItem_Click(object sender, EventArgs e)
{
    try
    {
        StreamWriter sw = File.AppendText(Application.ExecutablePath);
        sw.Write(txtBox.Text);
        sw.Dispose();
    }
    catch
    {
        SaveFileDialog sf = new SaveFileDialog();
        sf.DefaultExt = "*.txt";
        sf.Filter = "文本文档(.txt)|*.txt";
        if (sf.ShowDialog() == DialogResult.OK)
        {
            StreamWriter sw = File.AppendText(sf.FileName);
            sw.Write(txtBox.Text);
            sw.Dispose();
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

4.另存为:

    private void 另存为ToolStripMenuItem_Click(object sender, EventArgs e)
    {
        string name;
        //SaveFileDialog类
        SaveFileDialog save = new SaveFileDialog();
        //过滤器
        save.Filter = "*.txt|*.TXT|(*.*)|*.*";
        //显示
        if (save.ShowDialog() == DialogResult.OK)
        {
            name = save.FileName;
            FileInfo info = new FileInfo(name);
            //info.Delete();
            StreamWriter writer = info.CreateText();
            writer.Write(txtBox.Text);
            writer.Close();
        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

5.打印:


    private void 打印PToolStripMenuItem_Click(object sender, EventArgs e)
    {
        //显示允许用户选择打印机的选项及其它打印选项的对话框
        this.printDialog.Document = this.printDocument;
        this.printDialog.PrinterSettings = this.pageSetupDialog.PrinterSettings;
        //向打印机发送打印指令
        if (this.printDialog.ShowDialog() == DialogResult.OK)
        {
            try
            {
                this.printDocument.Print();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "错误信息!", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
 


相关教程