public bool Function_New() {
if (QuerySave()) { txtEditor.Text = ""; txtEditor.Modified = false; strFileName = null; return true; }
return false;
}
public bool Function_Open() {
if (!QuerySave()) return false;
using (OpenFileDialog dlg = new OpenFileDialog()) {
dlg.Filter = "文字檔 (*.txt)|*.txt|所有檔案|*.*";
dlg.CheckPathExists = true;
if (dlg.ShowDialog(this) == DialogResult.OK) {
using (StreamReader rd = new StreamReader(dlg.FileName, Encoding.GetEncoding("gb2312"))) {
txtEditor.Text = rd.ReadToEnd();
}
strFileName = dlg.FileName; txtEditor.Modified = false; return true;
}
}
return false;
}
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) return false;
strFileName = dlg.FileName;
}
}
using (StreamWriter wr = new StreamWriter(strFileName, false, Encoding.GetEncoding("gb2312"))) {
wr.Write(txtEditor.Text);
}
txtEditor.Modified = false; return true;
}
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;
using (StreamWriter wr = new StreamWriter(strFileName, false, Encoding.GetEncoding("gb2312"))) {
wr.Write(txtEditor.Text);
}
txtEditor.Modified = false; return true;
}
}
return false;
}
public void Function_SelectAll() => txtEditor.SelectAll();
public void Function_Cut() => txtEditor.Cut();
public void Function_Copy() => txtEditor.Copy();
public void Function_Paste() => txtEditor.Paste();
public void Function_Delete() => txtEditor.SelectedText = "";
所有方法直接轉發給 txtEditor 控制項,DocumentClass 再封裝一次供腳本呼叫,形成兩層委派架構。