如何用C#编写文本编辑器
首先,打开你的开发工具Microsoft visual studio 2005。文件——》新建项目——》项目类型选择visualc#,模板选择windows 应用程序,到此我们已经创建好一个window窗体了!
接着修改窗体的属性,窗体名字修改为EditorForm,窗体的text属性修改为你要的命名。在工具箱了选择MenuStrip控件如图,把菜单拖到窗体去!
然后选择插入标准项
插入完菜单之后,再从工具箱里拖一个RichTextBox到菜单下面,然后选择停靠父容器,最后一个简单的编辑器的界面出来了!如图:
-----------------------------------------------------------------------------------------------------------------------------------------
好了,界面我们已经设计好了,现在我们来实现功能了!首先从工具箱里面拖一个叫做openFileDialog的控件,这是一个用于打开文件的对话框来的,把这个拖到窗体,然后点击一下OpenFileDialog选定此控件,设置Filter属性默认文本(*.txt)|*.txt|富文本文件(*.rtf)|*.rtf 。当然以后功能扩充后,可以打开别的文件,例如设置Filter的属性为括号里的(数据库文件(*.dat)|*.dat|图像文件(*jpg;*bmp;*gif)|*jpg;*bmp;*gif|文本文件(*.txt)|*.txt|所有文件(*.*)|*.*)。(注意,次数是用于打开文件对话框对要打开的文件进行筛选的,我们这里只是做简单的文本编译器,所以支持的格式分别是.txt 和.rtf格式的文本),最后双击菜单的打开菜单出现
private void 打开OToolStripMenuItem_Click(object sender, EventArgs e)
{
//确定用户选择了文件
if (this.openFileDialog.ShowDialog() == DialogResult.OK)
{
//得到用户选择的文件路径和文件名
path = this.openFileDialog.FileName;
//读取文件内容
this.LoadFile();
}
}
private void LoadFile()
{
if (this.openFileDialog.FilterIndex == 1)
{
//纯文本文件
this.richTextBox1.LoadFile(path, RichTextBoxStreamType.PlainText);
}
if (this.openFileDialog.FilterIndex == 2)
{
this.richTextBox1.LoadFile(path, RichTextBoxStreamType.RichText);
}
}
我们要在里面写代码实现把PC机上的文件打开到我们的编辑器中进行简单的文字编译了,但是在写代码前,我们要定义一个全局变量,用于记录是否文件是新建的,还是打开来编辑的,我们要把这个变量定义为 string path = null ;
附上所有代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace SimpleEditor
{
public partial class EditorForm : Form
{
string path = null;
string isSaved = "n";
int initLenglt = 0;
public EditorForm(string path)
{
this.path = path;
InitializeComponent();
}
private void 新建NToolStripMenuItem_Click(object sender, EventArgs e)
{
if (this.richTextBox1.TextLength > 0 && (isSaved.Equals("n")))
{
if (MessageBox.Show("文件没有保存是否新建文件?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
{
this.richTextBox1.Clear();
this.Text = "简单的编辑器";
this.initLenglt = richTextBox1.TextLength;
}
}
else
{
this.richTextBox1.Clear();
this.Text = "简单的编辑器";
this.initLenglt = richTextBox1.TextLength;
}
}
private void 打开OToolStripMenuItem_Click(object sender, EventArgs e)
{
//确定用户选择了文件
if (this.openFileDialog.ShowDialog() == DialogResult.OK)
{
//得到用户选择的文件路径和文件名
path = this.openFileDialog.FileName;
//读取文件内容
this.OpenFile();
}
}
private void OpenFile()
{
try
{
string extName = this.path.Substring(this.path.LastIndexOf("."));
if (extName.ToLower().Equals(".txt"))
{
//纯文本文件
this.richTextBox1.LoadFile(path, RichTextBoxStreamType.PlainText);
}
if (extName.ToLower().Equals(".rtf"))
{
this.richTextBox1.LoadFile(path, RichTextBoxStreamType.RichText);
}
this.Text = "文本编辑器" + path;
this.isSaved = "y";
}
catch (Exception ex)
{
MessageBox.Show("请选择合适文件,打开失败");
}
}
private void 保存SToolStripMenuItem_Click(object sender, EventArgs e)
{
if(string.IsNullOrEmpty(this.path))
{
this.saveFileDialog1.Title = "保存为";
if (this.saveFileDialog1.ShowDialog() == DialogResult.OK)
{
this.path = this.saveFileDialog1.FileName;
SaveFile();
}
}
else
{
string extNanme=path.Substring(this.path.LastIndexOf("."));
if (extNanme.ToLower().Equals(".txt"))
{
this.richTextBox1.SaveFile(this.path, RichTextBoxStreamType.PlainText);
}
if(extNanme.ToLower().Equals(".rtf"))
{
this.richTextBox1.SaveFile(this.path, RichTextBoxStreamType.RichText);
}
}
this.initLenglt = richTextBox1.TextLength;
}
private void SaveFile()
{
try
{
if (this.saveFileDialog1.FilterIndex == 1)
{
//纯文本文件
this.richTextBox1.SaveFile(path, RichTextBoxStreamType.PlainText);
}
if (this.saveFileDialog1.FilterIndex == 2)
{
this.richTextBox1.SaveFile(path, RichTextBoxStreamType.RichText);
}
this.Text = "文本编辑器" + path;
this.initLenglt = richTextBox1.TextLength;
this.isSaved = "y";
}
catch (Exception ex)
{ }
}
private void 另存为AToolStripMenuItem_Click(object sender, EventArgs e)
{
if (this.saveFileDialog1.ShowDialog() == DialogResult.OK)
{
this.path = this.saveFileDialog1.FileName;
SaveFile();
}
}
private void EditorForm_Load(object sender, EventArgs e)
{
this.剪切TToolStripMenuItem.Enabled = false;
this.复制CToolStripMenuItem.Enabled = false;
if(!string.IsNullOrEmpty(this.path))
{
this.OpenFile();
}
}
private void 字体ToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
string extName = this.path.Substring(this.path.LastIndexOf("."));
if (extName.ToLower().Equals(".txt"))
{
this.SettxtFont();
}
if (extName.ToLower().Equals(".rtf"))
{
this.SetRichtextFont();
}
}
catch(Exception ex)
{
}
}
private void SettxtFont()
{
if (this.fontDialog1.ShowDialog() == DialogResult.OK)
{
Font font = this.fontDialog1.Font;
this.richTextBox1.Font = font;
}
}
private void SetRichtextFont()
{
if (this.fontDialog1.ShowDialog() == DialogResult.OK)
{
Font font = this.fontDialog1.Font;
this.richTextBox1.SelectionFont = font;
}
}
private void 字体颜色ToolStripMenuItem_Click(object sender, EventArgs e)
{
string extName = this.path.Substring(this.path.LastIndexOf("."));
if (extName.ToLower().Equals(".rtf"))
{
if (this.colorDialog1.ShowDialog() == DialogResult.OK)
{
Color color = this.colorDialog1.Color;
this.richTextBox1.SelectionColor = color;
}
}
}
private void 剪切TToolStripMenuItem_Click(object sender, EventArgs e)
{
if(richTextBox1.SelectionLength>0)
{
this.richTextBox1.Cut();
}
}
private void 粘贴PToolStripMenuItem_Click(object sender, EventArgs e)
{
this.richTextBox1.Paste();
}
private void 复制CToolStripMenuItem_Click(object sender, EventArgs e)
{
if (richTextBox1.SelectionLength > 0)
{
this.richTextBox1.Copy();
}
}
private void richTextBox1_SelectionChanged(object sender, EventArgs e)
{
if (richTextBox1.SelectionLength > 0)
{
this.剪切TToolStripMenuItem.Enabled = true;
this.复制CToolStripMenuItem.Enabled = true;
}
else
{
this.剪切TToolStripMenuItem.Enabled = false;
this.复制CToolStripMenuItem.Enabled = false;
}
}
private void 编辑EToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void 全选AToolStripMenuItem_Click(object sender, EventArgs e)
{
this.richTextBox1.SelectedText.ToString();
}
private void 退出XToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
private void EditorForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (this.richTextBox1.TextLength > this.initLenglt)
{
if (MessageBox.Show("文件没有保存是否退出?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.No)
{
e.Cancel = true;
}
}
}
private void 关于AToolStripMenuItem_Click(object sender, EventArgs e)
{
AboutBox aboutbox = new AboutBox();
aboutbox.ShowDialog();
}
private void 撤消UToolStripMenuItem_Click(object sender, EventArgs e)
{
this.richTextBox1.Undo();
}
}
}