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 = "";
腳本 (demo.vbs)
↓ 呼叫 document.Text / document.Save() 等
DocumentClass (封裝層)
↓ 委派
frmMain.Function_Save() 等核心方法
↓ 直接操作
txtEditor (TextBox 控制項)
工具列按鈕同樣呼叫 Function_* 方法,腳本與 UI 共用同一組業務邏輯,確保行為一致。