主窗體中還定義了諸如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 = "";
}