首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> 關於C語言

C# 發現之旅第十四講:VB.NET 腳本引擎(下)─ 主表單核心功能方法與腳本整合架構

     編輯:關於C語言

主表單核心功能方法


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 共用同一組業務邏輯,確保行為一致。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved