C#记事本
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void 打开OToolStripMenuItem_Click(object sender, EventArgs e)
{
OpenFileDialog myDlg = new OpenFileDialog();
myDlg.CheckFileExists = true;
myDlg.Filter = "文本文件(*.txt)|*.txt|所有文件(*.*)|*.*";
if (myDlg.ShowDialog() == DialogResult.OK)
{
this.richTextBox1.LoadFile(myDlg.FileName, RichTextBoxStreamType.PlainText);
this.statusStrip1.Text = myDlg.FileName;
}
}
private void 新建NToolStripMenuItem_Click(object sender, EventArgs e)
{
if (MessageBox.Show("要保存现有文件吗", "提示对话框", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
== DialogResult.Yes)
{
保存SToolStripMenuItem_Click(sender, e);
}
else
{
this.richTextBox1.Text = "";
}
}
private void 保存SToolStripMenuItem_Click(object sender, EventArgs e)
{
SaveFileDialog savefile = new SaveFileDialog();
savefile.Filter = "文本文件(*.txt)|*.txt|所有文件(*.*)|*.*";
if (savefile.ShowDialog() == DialogResult.OK)
{
this.richTextBox1.SaveFile (savefile .FileName ,RichTextBoxStreamType.PlainText );
}
}
private void 全选AToolStripMenuItem_Click(object sender, EventArgs e)
{
this.richTextBox1.SelectAll();
}
private void 粘贴PToolStripMenuItem_Click(object sender, EventArgs e)
{
this.richTextBox1.Paste();
}
private void 复制CToolStripMenuItem_Click(object sender, EventArgs e)
{
this.richTextBox1.Copy();
}
private void 剪切TToolStripMenuItem_Click(object sender, EventArgs e)
{
this.richTextBox1.Cut();
}
private void 重复RToolStripMenuItem_Click(object sender, EventArgs e)
{
this.richTextBox1.Redo();
}
private void 撤消UToolStripMenuItem_Click(object sender, EventArgs e)
{
this.richTextBox1.Undo();
}
private void 新建NToolStripButton_Click(object sender, EventArgs e)
{
if (MessageBox.Show("要保存现有文件吗", "提示对话框", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
== DialogResult.Yes)
{
保存SToolStripMenuItem_Click(sender, e);
}
else
{
this.richTextBox1.Text = "";
}
}
private void 打开OToolStripButton_Click(object sender, EventArgs e)
{
OpenFileDialog myDlg = new OpenFileDialog();
myDlg.CheckFileExists = true;
myDlg.Filter = "文本文件(*.txt)|*.txt|所有文件(*.*)|*.*";
if (myDlg.ShowDialog() == DialogResult.OK)
{
this.richTextBox1.LoadFile(myDlg.FileName, RichTextBoxStreamType.PlainText);
this.statusStrip1.Text = myDlg.FileName;
}
}
private void 复制ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.richTextBox1.Copy();
}
private void 粘贴ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.richTextBox1.Paste();
}
private void 剪切ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.richTextBox1.Cut();
}
private void 复制CToolStripButton_Click(object sender, EventArgs e)
{
this.richTextBox1.Copy();
}
private void 粘贴PToolStripButton_Click(object sender, EventArgs e)
{
this.richTextBox1.Paste();
}
private void 剪切UToolStripButton_Click(object sender, EventArgs e)
{
this.richTextBox1.Cut();
}
private void 保存SToolStripButton_Click(object sender, EventArgs e)
{
保存SToolStripMenuItem_Click(sender, e);
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (MessageBox.Show("要保存现有文件吗", "提示对话框", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
== DialogResult.Yes)
{
保存SToolStripMenuItem_Click(sender, e);
}
}
private void 自定义CToolStripMenuItem_Click(object sender, EventArgs e)
{
if (richTextBox1.WordWrap == true)
{
richTextBox1.WordWrap = false;
}
else
{
richTextBox1.WordWrap = true;
}
}
private void 字字颜色ToolStripMenuItem_Click(object sender, EventArgs e)
{
ColorDialog colorDialog = new ColorDialog();
colorDialog.AllowFullOpen = true;
colorDialog.FullOpen = true;
colorDialog.ShowHelp = true;
colorDialog.Color = Color.Black;//初始化当前文本框中的字体颜色,当用户在ColorDialog对话框中点击"取消"按钮恢复原来的值
if (colorDialog.ShowDialog() != DialogResult.Cancel)
{
this.richTextBox1 .SelectionColor = colorDialog.Color; //将当前选定的文字改变颜色
}
richTextBox1.Focus();
}
private void 字体ToolStripMenuItem_Click(object sender, EventArgs e)
{
FontDialog font = new FontDialog();
font.Color = richTextBox1.ForeColor;
font.AllowScriptChange = true;
if (font.ShowDialog() != DialogResult.Cancel)
{
richTextBox1.SelectionFont = font.Font;//将当前选定的文字改变字体
}
richTextBox1.Focus();
}
}
}