📄 form1.cs
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace jishiben
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private bool dirty = false;
private string filepath = "";
private void MySaving()
{
if (dirty)
{
if (MessageBox.Show("需要保存文件吗?", "提示框", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
dirty = false;
if (filepath.Length == 0)
{
filepath = saveFileDialog1.FileName;
}
StreamWriter sTmp = new StreamWriter(filepath);
sTmp.Write(richTextBox1.Text);
sTmp.Flush();
sTmp.Close();
}
}
}
}
private void newToolStripMenuItem_Click(object sender, EventArgs e)
{
MySaving();
dirty = false;
richTextBox1.Text = "";
}
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
MySaving();
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
dirty = false;
StreamReader sTmp = new StreamReader(openFileDialog1.FileName, System.Text.Encoding.ASCII);
filepath = openFileDialog1.FileName;
richTextBox1.Text = "";
richTextBox1.Text = sTmp.ReadToEnd();
}
}
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
MySaving();
}
private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
{
MySaving();
}
private void wordwrapToolStripMenuItem_Click(object sender, EventArgs e)
{
if (richTextBox1.WordWrap == true)
{
richTextBox1.WordWrap = false;
wordwrapToolStripMenuItem.Checked = false;
}
else
{
richTextBox1.WordWrap = true;
wordwrapToolStripMenuItem.Checked = true;
}
}
private void fontToolStripMenuItem_Click(object sender, EventArgs e)
{
if (fontDialog1.ShowDialog() == DialogResult.OK)
{
richTextBox1.Font = fontDialog1.Font;
}
}
private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show("记事本实例 1.0版", "关于记事本");
}
private void richTextBox1_KeyPress(object sender, KeyPressEventArgs e)
{
dirty = true;
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
MySaving();
if (dirty == false)
Close();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
MySaving();
if (dirty == false)
e.Cancel = false;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -