主窗体中还定义了诸如Function_New,Function_Open,Function_Save等等函数用于实现 对文档的新建、打开文件和保存文件等处理。而工具条上的按钮就是调用这些功能函数。定 义这些功能函数的代码如下
/// <summary>
/// 执行新建文档
/// </summary>
public bool Function_New()
{
if (QuerySave())
{
txtEditor.Text = "";
txtEditor.Modified = false;
strFileName = null;
return true;
}
return false;
}
/// <summary>
/// 执行打开文件操作
/// </summary>
public bool Function_Open()
{
if (QuerySave() == false)
{
return false ;
}
using (OpenFileDialog dlg = new OpenFileDialog())
{
dlg.Filter = "文本文件 (*.txt)|*.txt|所有文件|*.*";
dlg.CheckPathExists = true;
if (dlg.ShowDialog(this) == DialogResult.OK)
{
System.IO.StreamReader reader = new System.IO.StreamReader(
dlg.FileName, System.Text.Encoding.GetEncoding("gb2312"));
txtEditor.Text = reader.ReadToEnd();
reader.Close ();
strFileName = dlg.FileName;
txtEditor.Modified = false;
return true;
}
}
return false;
}
/// <summary>
/// 执行保存文档操作
/// </summary>
/// <returns>操作是否成功</returns>
public bool Function_Save()
{
if (strFileName == null)
{
using (SaveFileDialog dlg = new SaveFileDialog())
{
dlg.Filter = "文本文件(*.txt)|*.txt|所有文件|*.*";
dlg.CheckPathExists = true;
dlg.OverwritePrompt = true;
if (dlg.ShowDialog(this) == DialogResult.OK)
{
strFileName = dlg.FileName;
}
else
{
return false;
}
}
}
System.IO.StreamWriter writer = new System.IO.StreamWriter(
strFileName, false, System.Text.Encoding.GetEncoding( "gb2312" ));
writer.Write (this.txtEditor.Text);
writer.Close();
this.txtEditor.Modified = false;
return true;
}
/// <summary>
/// 执行另存为操作
/// </summary>
public bool Function_SaveAs()
{
using (SaveFileDialog dlg = new SaveFileDialog())
{
dlg.Filter = " 文本文件(*.txt)|*.txt|所有文件|*.*";
dlg.CheckPathExists = true;
dlg.OverwritePrompt = true;
if (dlg.ShowDialog(this) == DialogResult.OK)
{
strFileName = dlg.FileName;
System.IO.StreamWriter writer = new System.IO.StreamWriter(
strFileName, false, System.Text.Encoding.GetEncoding ("gb2312"));
writer.Write(this.txtEditor.Text);
writer.Close();
this.txtEditor.Modified = false;
return true;
}
}
return false;
}
/// <summary>
/// 执行全选操作
/// </summary>
public void Function_SelectAll()
{
txtEditor.SelectAll ();
}
/// <summary>
/// 执行剪切操作
/// </summary>
public void Function_Cut()
{
txtEditor.Cut();
}
/// <summary>
/// 执行复制操作
/// </summary>
public void Function_Copy()
{
txtEditor.Copy();
}
/// <summary>
/// 执行粘帖操作
/// </summary>
public void Function_Paste()
{
txtEditor.Paste();
}
/// <summary>
/// 执行删除操作
/// </summary>
public void Function_Delete()
{
txtEditor.SelectedText = "";
}