原創|行業資訊|編輯:龔雪|2020-04-14 09:49:36.290|閱讀 390 次
概述:本文將主要為大家介紹DevExpress WinForms產品組合中高價值實用程序控件——Message Boxes,WinForms Message Box控件將在即將發布的v20.1中發布!
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
下載DevExpress v19.2完整版 DevExpress v19.2漢化資源獲取
DevExpress Winforms Controls 內置140多個UI控件和庫,完美構建流暢、美觀且易于使用的應用程序。想要體驗?點擊下載>>
Message Boxes是DevExpress WinForms產品組合中高價值實用程序控件的完美示例。由于大多數應用程序都是通過消息框與用戶進行通信的,所以WinForms Message Box控件將在即將發布的v20.1中出現,本文主要將為大家闡述此功能。
DevExpress XtraMessageBox對象與默認的WinForms消息相對應,XtraMessageBox的主要優勢在于其使用DevExpress應用程序皮膚的功能,盡管這是一個重要的優勢,但這絕不是XtraMessageBox的唯一優勢。
雖然您可以使用標準方法顯示消息(將文本字符串和按鈕設置為靜態XtraMessageBox.Show()方法重載),但您也可以創建XtraMessageBoxArgs對象并將其作為唯一的Show方法參數傳遞。 此對象允許您合并其他操作,例如您可以顯示嵌入式計時器到期時自動關閉的消息。
XtraMessageBoxArgs args = new XtraMessageBoxArgs(); args.AutoCloseOptions.Delay = 5000; args.Caption = "Auto-close message"; args.Text = "This message closes automatically after 5 seconds."; args.Buttons = new DialogResult[] { DialogResult.OK, DialogResult.Cancel }; //show a countdown on a default button args.AutoCloseOptions.ShowTimerOnDefaultButton = true; XtraMessageBox.Show(args);
XtraMessageBoxArgs.Showing事件允許您在屏幕上顯示消息表單之前對其進行訪問和修改,您可以將其用于各種各樣的事情,例如限制消息寬度。
XtraMessageBoxArgs args = new XtraMessageBoxArgs(); args.Caption = "A very long message"; args.Text = "A message box attempts to show all of its text content in one line. " + "If you do not limit the form size, this message will be very long and thin. " + "Set the maximum form width and the form will wrap this long text."; args.Buttons = new DialogResult[] { DialogResult.OK, DialogResult.Cancel}; args.Showing += Args_Showing; XtraMessageBox.Show(args); private void Args_Showing(object sender, XtraMessageShowingArgs e) { e.Form.MaximumSize = new Size(600, 600); }
...或自定義消息按鈕(更改標題或向其提供矢量圖像)。
XtraMessageBoxArgs args = new XtraMessageBoxArgs(); args.Caption = "A message with icons"; args.Text = "This message displays custom SVG images in its buttons"; args.Buttons = new DialogResult[] { DialogResult.OK, DialogResult.Cancel, DialogResult.Retry }; args.Showing += Args_Showing; XtraMessageBox.Show(args); void Args_Showing(object sender, XtraMessageShowingArgs e) { foreach (var control in e.Form.Controls) { SimpleButton button = control as SimpleButton; if (button != null) { button.ImageOptions.SvgImageSize = new Size(16, 16); button.ImageOptions.ImageToTextAlignment = ImageAlignToText.LeftCenter; //button.Height = 25; switch (button.DialogResult) { case (DialogResult.OK): button.ImageOptions.SvgImage = svgImageCollection1[0]; break; case (DialogResult.Cancel): button.ImageOptions.SvgImage = svgImageCollection1[1]; break; case (DialogResult.Retry): button.ImageOptions.SvgImage = svgImageCollection1[2]; break; } } } }
使用v20.1,您將能夠輕松地在消息中包含"Do not show this message again" 復選框,如果您想在項目中加入此功能,只需將布爾值XtraMessageBoxArgs.DoNotShowAgainCheckBoxVisible設置為true,復選框文本也是可自定義的。
XtraMessageBoxArgs args = new XtraMessageBoxArgs(); args.Caption = "Message"; args.Text = "You are using a trial version. The trial period expires in 30 days"; args.DoNotShowAgainCheckBoxVisible = true; args.DoNotShowAgainCheckBoxText = "Do not remind me again"; XtraMessageBox.Show(args);
此復選框本身不會執行任何操作,當消息出現在屏幕上(或被消除)時,它將引發Load和Closed事件。 您需要處理這些事件來存儲和檢索e.Visible屬性值,此屬性值指定用戶是否選擇隱藏消息。如果Load事件參數收到e.Visible屬性值為false,則該消息被取消。
args.Load += Args_Load; args.Closed += Args_Closed; void Args_Closed(object sender, XtraMessageBoxClosedArgs e) { //save e.Visible to a database or a local storage file } void Args_Load(object sender, XtraMessageBoxLoadArgs e) { //retireve the value from a database or a local storage file e.Visible = value; }
與e.Visible一起,您還可以存儲e.DialogResult屬性值,它對應于關閉消息時使用的最后一個已知DialogResult。如果消息被抑制,則可以使用此值,以便Show方法返回上一個用戶選擇,而不是DialogResult.None。
void Args_Load(object sender, XtraMessageBoxLoadArgs e) { e.Visible = _restoredVisibleValue_; if (!e.Visible) { //restore the e.DialogResult property e.DialogResult = _restoredDialogResultValue_; } }
對于那些不想手動保存和還原這些參數的用戶,為您提供將其存儲在注冊表中的選項。 為此,請在Closed事件上調用SaveToRegistry方法,并在加載時調用RestoreFromRegistry。
void Args_Closed(object sender, XtraMessageBoxClosedArgs e) { e.SaveToRegistry(); } void Args_Load(object sender, XtraMessageBoxLoadArgs e) { e.RestoreFromRegistry(); }
此代碼將DialogResult和Visible鍵保存在Computer \ HKEY_CURRENT_USER \ Software \ X \ Y路徑下,其中:
最后即使用戶選擇隱藏消息,也可以強制顯示消息。 為此請在Load事件處理程序中調用e.ShowMessage方法,布爾方法參數指定是否應選中"Do not show again" 復選框。
DevExpress Dashboard控件實操公開課4月即將開啟,
DevExpress技術交流群:540330292 歡迎一起進群討論
掃描關注DevExpress中文網微信公眾號,及時獲取最新動態及最新資訊
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自:慧都網