顯示消息框
Window全局對象還定義了一些函數用於顯示一些消息對話框實現用戶互換。主要代 碼為
/// <summary>
/// 將對象轉化為用於顯示的文本
/// </summary>
/// <param name="objData">要轉換的對象 </param>
/// <returns>顯示的文本</returns>
private string GetDisplayText(object objData)
{
if (objData == null)
return "[null]";
else
return Convert.ToString(objData);
}
/// <summary>
/// 顯示消息框
/// </summary>
/// <param name="objText">提示信息的文本</param>
public void Alert(object objText)
{
if (bolUserInteractive == false)
return;
System.Windows.Forms.MessageBox.Show(
myParentWindow,
GetDisplayText(objText),
SystemName,
System.Windows.Forms.MessageBoxButtons.OK,
System.Windows.Forms.MessageBoxIcon.Information);
}
/// <summary>
/// 顯示錯誤消息框
/// </summary>
/// <param name="objText">提示信息的文本</param>
public void AlertError(object objText)
{
if (bolUserInteractive == false)
return;
System.Windows.Forms.MessageBox.Show(
myParentWindow,
GetDisplayText(objText),
SystemName,
System.Windows.Forms.MessageBoxButtons.OK,
System.Windows.Forms.MessageBoxIcon.Exclamation);
}
/// <summary>
/// 顯示一個提示信息框,並返回用戶的選擇
/// </summary>
/// <param name="objText">提示的文本 </param>
/// <returns>用戶是否確認的信息</returns>
public bool ConFirm(object objText)
{
if (bolUserInteractive == false)
return false;
return (System.Windows.Forms.MessageBox.Show(
myParentWindow,
GetDisplayText(objText),
SystemName,
System.Windows.Forms.MessageBoxButtons.YesNo,
System.Windows.Forms.MessageBoxIcon.Question)
== System.Windows.Forms.DialogResult.Yes);
}
/// <summary>
/// 顯示一個信息輸入框共用戶輸入
/// </summary>
/// <param name="objCaption">輸入信息的提示</param>
/// <param name="objDefault">默認值</param>
/// <returns>用戶輸入的信 息</returns>
public string Prompt(object objCaption, object objDefault)
{
if (bolUserInteractive == false)
return null;
return dlgInputBox.InputBox(
myParentWindow,
GetDisplayText(objCaption),
SystemName,
GetDisplayText(objDefault));
}
/// <summary>
/// 顯示一個文本選擇對話框
/// </summary>
/// <param name="objCaption">對話框標題 </param>
/// <param name="objFilter">文件過濾器 </param>
/// <returns>用戶選擇的文件名,若用戶取消選擇則返回空引 用</returns>
public string BrowseFile(object objCaption, object objFilter)
{
using (System.Windows.Forms.OpenFileDialog dlg
= new System.Windows.Forms.OpenFileDialog())
{
dlg.CheckFileExists = true;
if (objCaption != null)
{
dlg.Title = this.GetDisplayText(objCaption);
}
if (objFilter != null)
dlg.Filter = GetDisplayText (objFilter);
if (dlg.ShowDialog(myParentWindow) == System.Windows.Forms.DialogResult.OK)
return dlg.FileName;
}
return null;
}
/// <summary>
/// 顯示一個文件夾選擇對話框
/// </summary>
/// <param name="objCaption">對話框標題</param>
/// <returns>用戶選擇了一個文件夾則返回該路徑,否則返回空引用</returns>
public string BrowseFolder(object objCaption)
{
using (System.Windows.Forms.FolderBrowserDialog dlg
= new System.Windows.Forms.FolderBrowserDialog())
{
if (objCaption != null)
{
dlg.Description = this.GetDisplayText(objCaption);
}
dlg.RootFolder = System.Environment.SpecialFolder.MyComputer;
if (dlg.ShowDialog(myParentWindow) == System.Windows.Forms.DialogResult.OK)
return dlg.SelectedPath;
else
return null;
}
}