用C#创建一个简单的文本编辑器

来源:百度文库 编辑:神马文学网 时间:2024/05/02 02:10:22
学习用C#创建一个简单的文本编辑器——Shawn&Cheney Editor。 1.创建一个工程,命名为SimpleEditor
2.把生成的文件Form1.cs重命名为SimpleEditorForm
3.设置窗体属性:
Text:Shawn&Cheney Editor
Icon:选择一个自定义图标
4.给窗体添加一个菜单栏和一个richTextBox控件
设置richTextBox控件属性如下
属性

Name
TextBoxEdit
Text
Multiline
True
Dock
Fill
ScollBars
Both
AcceptsReturn
True
AcceptsTab
True
5.给菜单项添加“新建”、“打开”、“保存”、“另存为”项目,分别设置好各自属性。
6.首先在SimpleEditorForm类中定义一个成员变量filename,用来接收文件
public partial class SimpleEditorForm : Form
{
private string filename = "Untitled";
…………
}
7.修改SimpleEditorForm构造函数,使其接收一个文件名参数
public SimpleEditorForm(string filename)
{
InitializeComponent();
if (filename != null)
{
this.filename = filename;
OpenFile();
}
//如果接收到一个不为null的参数,就给前面定义的成员变量filename赋值
//然后调用OpenFile函数,这个函数接下来将会给出定义。
}
8.修改Program.cs中的main函数,使得main()可以给SimpleEditorForm传递filename参数
static void Main(string[] args)
{
string filename = null;
if (args.Length != 0)
filename = args[0];
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new SimpleEditorForm(filename));
}
//如果本程序执行时带有参数(即给定了文件名),则这里会把这个文件名传递给SimpleEditorForm
//否则,将传递一个空值
9.给出OpenFile()方法的实现,将OpenFile()作为SimpleEditorForm的成员函数
public partial class SimpleEditorForm : Form
{
………………
protected void OpenFile()
{
try
{
TextBoxEdit.Clear();
TextBoxEdit.Text = File.ReadAllText(filename);
}
catch (IOException ex)
{
MessageBox.Show(ex.Message, "Simple Editor",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
………………
}
由于这里使用了File类,所以必须添加using System.IO命名空间
10.给菜单项“新建”添加处理程序
private void MenuItemNew_Click(object sender, EventArgs e)
{
filename = "Untitled"; //新建的文件文件名应该是Untitled
TextBoxEdit.Clear();   //调用Clear()方法清空文本框
}
现在,已经可以通过这个程序来打开文件了
要给应用程序添加命令行参数,可以通过下面的方法:
在Solution Explorer中右击项目,点击“属性”,点击“调试”,输入命令行参数
本例中输入F:\CSharpPro\my6thPro\SimpleEditor\SimpleEditor\SimpleEditorForm.cs
运行程序,可以看到文件已经被打开了

下面我们接着说如何实现“打开”、“保存”、“另存为”菜单项的功能。
1.创建“打开”菜单项处理程序
首先将工具箱中的OpenFileDialog空间拖动到编辑区底部灰色区域,然后设置其属性:
Name:dlgOpenFile
Filter:Text Documents (*.txt)|*.txt|ShawnCheney Documents (*.shawney)|*.shawnney|All Files|*.*
FilterIndex:2
然后双击菜单项中的“打开”项,添加如下代码:
private void MenuItemOpen_Click(object sender, EventArgs e)
{
if (dlgOpenFile.ShowDialog() == DialogResult.OK)
{
filename = dlgOpenFile.FileName;
OpenFile();
}
}
//dlgOpenFile.ShowDialog() 将会打开“打开”对话框,
//如果点击了OK,则读取 dlgOpenFile.FileName属性,获得文件名
//最后调用OpenFile()方法打开文件。
2.保存文件
与前面类似,拖动SaveFileDialog控件到编辑区底部灰色区域,设置其属性
Name:dlgSaveFile
Filter:Text Documents (*.txt)|*.txt|ShawnCheney Documents (*.shawney)|*.shawnney
FilterIndex:2
给“另存为”菜单项添加事件处理程序
private void MenuItemSaveAs_Click(object sender, EventArgs e)
{
if (dlgSaveFile.ShowDialog() == DialogResult.OK)
{
filename = dlgSaveFile.FileName;
SaveFile();
}
}
//与前面类似,如果对话框返回OK值,则从dlgSaveFile.FileName获得文件名
//然后调用SaveFile()方法保存文件。
创建SaveFile()方法,由于保存的时候可能会产生很多问题,因此,放在try块内!
private void SaveFile()
{
try
{
File.WriteAllText(filename, TextBoxEdit.Text);
}
catch (IOException ex)
{
MessageBox.Show(ex.Message, "Shawn&Cheney Editor",
MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
}
}
至此,“另存为”功能实现了。
为什么不为“保存”按钮添加上面那个处理程序呢?
原因在于,如果用户点击“保存”按钮时,当前的文档不是一个用户新建的文档,或者说当前文档时一个已经拥有文件名的文档,那么是不需要用户输入文件名的!而“另存为”按钮,则无论什么情况都需要用户输入文件名。所以“另存为”按钮相对来说逻辑比较简单。
3.下面来实现“保存”按钮。
private void MenuItemSave_Click(object sender, EventArgs e)
{
if (filename == "Untitled")
{
MenuItemSaveAs_Click(sender, e);
//如果当前文档文件名是Untitled,点击“保存”和点击“另存为”效果一样
//所以调用“另存为”按钮的处理函数MenuItemSaveAs_Click()函数。
}
else
{
SaveFile();
//否则就直接保存。
}
}
到此为止,一个具有新建打开保存功能的简单文本编辑器就做好了。