前沿拓展:
dialogresult
兩種都可以,不過(guò)后面這種更簡(jiǎn)單和常用一些,原因很簡(jiǎn)單,比如說(shuō)一個(gè)OpenFileDialog,當(dāng)用戶去選擇文件,如果用戶點(diǎn)了取消怎么辦,如果用前一種方法,那代碼很可能是
dialog.那獲別格不負(fù)寬向愿ShowDialog();
if( dialog.FileName != "") //這 內(nèi)置140多個(gè)UI控件和庫(kù),完美構(gòu)建流暢、美觀且易于使用的應(yīng)用程序。
Message Boxes是DevExpress WinForms產(chǎn)品組合中高價(jià)值實(shí)用程序控件的完美示例。由于大多數(shù)應(yīng)用程序都是通過(guò)消息框與用戶進(jìn)行通信的,所以WinForms Message Box控件將在即將發(fā)布的v20.1中出現(xiàn),本文主要將為大家闡述此功能。
DevExpress XtraMessageBox對(duì)象與默認(rèn)的WinForms消息相對(duì)應(yīng),XtraMessageBox的主要優(yōu)勢(shì)在于其使用DevExpress應(yīng)用程序皮膚的功能,盡管這是一個(gè)重要的優(yōu)勢(shì),但這絕不是XtraMessageBox的唯一優(yōu)勢(shì)。
雖然您可以使用標(biāo)準(zhǔn)方法顯示消息(將文本字符串和按鈕設(shè)置為靜態(tài)XtraMessageBox.Show()方法重載),但您也可以創(chuàng)建XtraMessageBoxArgs對(duì)象并將其作為唯一的Show方法參數(shù)傳遞。 此對(duì)象允許您合并其他**作,例如您可以顯示嵌入式計(jì)時(shí)器到期時(shí)自動(dòng)關(guān)閉的消息。
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**允許您在屏幕上顯示消息表單之前對(duì)其進(jìn)行訪問(wèn)和修改,您可以將其用于各種各樣的事情,例如限制消息寬度。
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);
}
…或自定義消息按鈕(更改標(biāo)題或向其提供矢量圖像)。
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" 復(fù)選框,如果您想在項(xiàng)目中加入此功能,只需將布爾值XtraMessageBoxArgs.DoNotShowAgainCheckBoxVisible設(shè)置為true,復(fù)選框文本也是可自定義的。
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);
此復(fù)選框本身不會(huì)執(zhí)行任何**作,當(dāng)消息出現(xiàn)在屏幕上(或被消除)時(shí),它將引發(fā)Load和Closed**。 您需要處理這些**來(lái)存儲(chǔ)和檢索e.Visible屬性值,此屬性值指定用戶是否選擇隱藏消息。如果Load**參數(shù)收到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一起,您還可以存儲(chǔ)e.DialogResult屬性值,它對(duì)應(yīng)于關(guān)閉消息時(shí)使用的最后一個(gè)已知DialogResult。如果消息被抑制,則可以使用此值,以便Show方法返回上一個(gè)用戶選擇,而不是DialogResult.None。
void Args_Load(object sender, XtraMessageBoxLoadArgs e) {
e.Visible = _restore**isibleValue_;
if (!e.Visible)
{
//restore the e.DialogResult property
e.DialogResult = _restoredDialogResultValue_;
}
}
對(duì)于那些不想手動(dòng)保存和還原這些參數(shù)的用戶,為您提供將其存儲(chǔ)在注冊(cè)表中的選項(xiàng)。 為此,請(qǐng)?jiān)贑losed**上調(diào)用SaveToRegistry方法,并在加載時(shí)調(diào)用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路徑下,其中:
XtraMessageBox.RegistryPath屬性的X – value;或未設(shè)置該屬性,則為Path.Combine(Application.CompanyName,Application.ProductName,“ Messages”);XtraMessageBoxArgs.RegistryKey屬性的Y – value,或自動(dòng)生成的ID。
最后即使用戶選擇隱藏消息,也可以強(qiáng)制顯示消息。 為此請(qǐng)?jiān)贚oad**處理程序中調(diào)用e.ShowMessage方法,布爾方法參數(shù)指定是否應(yīng)選中"Do not show again" 復(fù)選框。
拓展知識(shí):
dialogresult
//
// 摘要:
// 獲取或設(shè)置一個(gè)值,該值在單擊按鈕時(shí)返回到父窗體。
//
// 返回結(jié)果:
// System.Windows.Forms.DialogResult 值之一。默認(rèn)值為 None。
//
// 異常:
// System.ComponentModel.InvalidEnumArgumentException:
// 分配的值不是 System.Windows.Forms.DialogResult 值之一。
本回答被網(wǎng)友采納
原創(chuàng)文章,作者:九賢生活小編,如若轉(zhuǎn)載,請(qǐng)注明出處:http:///74154.html