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

C# 發現之旅第十四講:VB.NET 腳本引擎(上)─ 視窗全域物件與使用者互動方法

     編輯:關於C語言

Window 全域物件互動方法

XVBAWindowObject 定義多種使用者介面互動功能,均受 UserInteractive 屬性控制(ASP.NET/Windows Service 環境下自動停用)。

核心輔助方法


private string GetDisplayText(object obj) => obj == null ? "[null]" : Convert.ToString(obj);

訊息方塊系列


public void Alert(object text) {
    if (!bolUserInteractive) return;
    MessageBox.Show(myParentWindow, GetDisplayText(text), SystemName, MessageBoxButtons.OK, MessageBoxIcon.Information);
}

public void AlertError(object text) {
    if (!bolUserInteractive) return;
    MessageBox.Show(myParentWindow, GetDisplayText(text), SystemName, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}

public bool Confirm(object text) {
    if (!bolUserInteractive) return false;
    return MessageBox.Show(myParentWindow, GetDisplayText(text), SystemName, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes;
}

輸入方塊與檔案/資料夾選擇


public string Prompt(object caption, object defaultValue) {
    if (!bolUserInteractive) return null;
    return dlgInputBox.InputBox(myParentWindow, GetDisplayText(caption), SystemName, GetDisplayText(defaultValue));
}

public string BrowseFile(object caption, object filter) {
    using (OpenFileDialog dlg = new OpenFileDialog()) {
        dlg.CheckFileExists = true;
        if (caption != null) dlg.Title = GetDisplayText(caption);
        if (filter != null) dlg.Filter = GetDisplayText(filter);
        return dlg.ShowDialog(myParentWindow) == DialogResult.OK ? dlg.FileName : null;
    }
}

public string BrowseFolder(object caption) {
    using (FolderBrowserDialog dlg = new FolderBrowserDialog()) {
        if (caption != null) dlg.Description = GetDisplayText(caption);
        dlg.RootFolder = Environment.SpecialFolder.MyComputer;
        return dlg.ShowDialog(myParentWindow) == DialogResult.OK ? dlg.SelectedPath : null;
    }
}

設計重點

  • 統一透過 GetDisplayText 處理 null 與型別轉換
  • 所有 UI 方法檢查 bolUserInteractive,非互動環境直接返回預設值
  • 建議腳本使用 Alert 而非 VB 內建 MsgBox,前者受引擎控制
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved