logo DevExpress WinForm中文手册
文档首页>>DevExpress WinForm中文手册>>消息、通知和对话框 - XtraMessageBox

消息、通知和对话框 - XtraMessageBox


点击获取DevExpress完整版下载

XtraMessageBox替换了不支持DevExpress皮肤的标准Windows表单消息框。

DevExpress WinForms帮助文档
  • 显示消息框
  • 自动关闭消息框
  • 禁止消息框
  • 附加自定义
显示消息框

若要显示XtraMessageBox,请调用静态(在VB中为Shared)XtraMessageBox.Show 方法。 该方法重载使您可以指定消息的标题、图标、文本、按钮等。如果用户单击 "No",下面的示例代码将阻止应用程序关闭。

C#


private void Form1_FormClosing(object sender, FormClosingEventArgs e) {
if (XtraMessageBox.Show("Do you want to quit the application?", "Confirmation", MessageBoxButtons.YesNo) != DialogResult.No) {
e.Cancel = true;
}
}


VB.NET


Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As FormClosingEventArgs)
If XtraMessageBox.Show("Do you want to quit the application?", "Confirmation", MessageBoxButtons.YesNo) <> DialogResult.No Then
e.Cancel = True
End If
End Sub


注意:您可以使用XtraDialog类显示具有更复杂布局的消息。

自动关闭消息框

XtraMessageBox.Show(XtraMessageBoxArgs)方法重载允许您显示自动关闭的消息框,方法参数是XtraMessageBoxArgs类对象,并提供AutoCloseOptions.Delay属性,该属性使您可以设置自动关闭超时(以毫秒为单位)。

C#


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};
XtraMessageBox.Show(args).ToString();


VB.NET


Dim args As New XtraMessageBoxArgs()
args.AutoCloseOptions.Delay = 5000
args.AutoCloseOptions.ShowTimerOnDefaultButton = True
args.DefaultButtonIndex = 0
args.Caption = "Auto-close message"
args.Text = "This message closes automatically after 5 seconds."
args.Buttons = New DialogResult() { DialogResult.OK, DialogResult.Cancel}
XtraMessageBox.Show(args).ToString()


第一个消息框按钮(上面的示例中的“ OK”)是默认按钮 - 如果用户按下“ Enter”键或自动关闭计时器到期,则认为此按钮已单击,并且消息框返回了相应的DialogResult值。 此按钮还显示自动关闭消息的倒数计时器。

DevExpress WinForms帮助文档

您可以修改DefaultButtonIndex属性以选择默认按钮,并禁用AutoCloseOptions.ShowTimerOnDefaultButton设置以隐藏倒数计时器。

C#


// change the default button
args.DefaultButtonIndex = 1;
// set to false to hide the countdown
args.AutoCloseOptions.ShowTimerOnDefaultButton = true;


VB.NET


' change the default button
args.DefaultButtonIndex = 1
' set to false to hide the countdown
args.AutoCloseOptions.ShowTimerOnDefaultButton = True


禁止消息框

您可以启用XtraMessageBoxArgs.DoNotShowAgainCheckBoxVisible 属性,以在XtraMessageBox消息框中显示"Do not show this message again"复选框。

第一个消息框按钮(上面的示例中的“ OK”)是默认按钮 - 如果用户按下“ Enter”键或自动关闭计时器到期,则认为此按钮已单击,并且消息框返回了相应的DialogResult值。 此按钮还显示自动关闭消息的倒数计时器。

DevExpress WinForms帮助文档

每当显示或关闭消息时, XtraMessageBoxArgs都会触发LoadClosed事件。 处理这些事件以保存和还原XtraMessageBoxEventArgs.Visible属性,该属性返回用户是否选中了此复选框。 您可以调用XtraMessageBoxClosedArgs.SaveToRegistryRestoreFromRegistry()方法,以将属性值保存并加载到注册表中(或从注册表中加载),或将该值存储在本地变量、本地存储中的文件、数据库等中。

通过Load事件,可以访问XtraMessageBoxLoadArgs.ShowMessage()方法,即使用户选择不再显示该消息,也可以强制显示该消息。

有关更多信息,请参见XtraMessageBoxArgs.DoNotShowAgainCheckBoxVisible

附加自定义

使用 XtraMessageBox.Show(XtraMessageBoxArgs) 方法重载调用消息框,并处理此方法参数的Showing事件以执行消息框自定义。

修改消息框按钮

下面的代码说明如何向消息框按钮添加自定义图标,在此示例中,图标是存储在外部SvgImageCollection中的矢量图像。

DevExpress WinForms帮助文档

C#


XtraMessageBoxArgs args = new XtraMessageBoxArgs();
args.Caption = "Message";
args.Text = "Buttons in this message box show have custom images.";
args.Buttons = new DialogResult[] { DialogResult.OK, DialogResult.Cancel, DialogResult.Retry};
args.Showing += Args_Showing;

private 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.Height = 25;
switch (button.DialogResult.ToString()) {
case ("OK"):
button.ImageOptions.SvgImage = svgImageCollection1[0];
break;
case ("Cancel"):
button.ImageOptions.SvgImage = svgImageCollection1[1];
break;
case ("Retry"):
button.ImageOptions.SvgImage = svgImageCollection1[2];
break;
}
}
}
}


VB.NET


Dim args As New XtraMessageBoxArgs()
args.Caption = "Message"
args.Text = "Buttons in this message box show have custom images."
args.Buttons = New DialogResult() { DialogResult.OK, DialogResult.Cancel, DialogResult.Retry}
AddHandler args.Showing, AdressOf Args_Showing

Private Sub Args_Showing(ByVal sender As Object, ByVal e As XtraMessageShowingArgs)
For Each control In e.Form.Controls
Dim button As SimpleButton = TryCast(control, SimpleButton)
If button IsNot Nothing Then
button.ImageOptions.SvgImageSize = New Size(16, 16)
'button.Height = 25;
Select Case button.DialogResult.ToString()
Case ("OK")
button.ImageOptions.SvgImage = svgImageCollection1(0)
Case ("Cancel")
button.ImageOptions.SvgImage = svgImageCollection1(1)
Case ("Retry")
button.ImageOptions.SvgImage = svgImageCollection1(2)
End Select
End If
Next control
End Sub


更改外观和字体设置

下面的示例增加消息框按钮的高度和字体大小,并使文本变为粗体。

DevExpress WinForms帮助文档

C#


XtraMessageBoxArgs args = new XtraMessageBoxArgs();
args.Caption = "Message";
args.Text = "This message has custom font settings.";
args.Buttons = new DialogResult[] { DialogResult.OK};
args.Showing += Args_Showing;
XtraMessageBox.Show(args).ToString();

private void Args_Showing(object sender, XtraMessageShowingArgs e) {
//bold message caption
e.Form.Appearance.FontStyleDelta = FontStyle.Bold;
//increased button height and font size
MessageButtonCollection buttons = e.Buttons as MessageButtonCollection;
SimpleButton btn = buttons[System.Windows.Forms.DialogResult.OK] as SimpleButton;
if (btn != null) {
btn.Appearance.FontSizeDelta = 15;
btn.Height += 10;
}
}


VB.NET


Dim args As New XtraMessageBoxArgs()
args.Caption = "Message"
args.Text = "This message has custom font settings."
args.Buttons = New DialogResult() { DialogResult.OK}
args.Showing += Args_Showing
XtraMessageBox.Show(args).ToString()

Private Sub Args_Showing(ByVal sender As Object, ByVal e As XtraMessageShowingArgs)
'bold message caption
e.Form.Appearance.FontStyleDelta = FontStyle.Bold
'increased button height and font size
Dim buttons As MessageButtonCollection = TryCast(e.Buttons, MessageButtonCollection)
Dim btn As SimpleButton = TryCast(buttons(System.Windows.Forms.DialogResult.OK), SimpleButton)
If btn IsNot Nothing Then
btn.Appearance.FontSizeDelta = 15
btn.Height += 10
End If
End Sub


更改按钮对齐方式

使用XtraMessageBox.ButtonsAlignment静态(在VB中共享)属性来指定按钮对齐方式,下面的示例将消息框按钮右对齐。

C#


XtraMessageBox.ButtonsAlignment = HorizontalAlignment.Right;


VB.NET


XtraMessageBox.ButtonsAlignment = HorizontalAlignment.Right


扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP