首頁
編程語言
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 = "";

所有方法直接轉發給 txtEditor 控制項,DocumentClass 再封裝一次供腳本呼叫,形成兩層委派架構。

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