彩票走势图

logo DevExpress WinForm中文手册

XtraMessageBox


立即下载DevExpress WinForms

DevExpress消息框(XtraMessageBox)是一个完全可定制的控件,扩展了标准Windows表单消息框的功能。它的高级功能包括:DevExpress皮肤,HTML启发的文本格式,以及HTML和CSS模板。

DevExpress WinForms帮助文档

显示消息框

调用静态(在VB中共享)XtraMessageBox.Show方法来显示XtraMessageBox,方法重载允许您指定标题、文本、按钮、图标和其他外观设置。下面的代码示例防止在用户单击No时关闭应用程序。

下面的示例演示如何显示应用程序退出确认消息:

C#:

private void Form1_FormClosing(object sender, FormClosingEventArgs e) {
// Displays a message box prompting the user to confirm whether they wish to exit the application.
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)
' Displays a message box prompting the user to confirm whether they wish to exit the application.
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属性来设置自动关闭超时(以毫秒为单位)。启用AutoCloseOptions.ShowTimerOnDefaultButton选项来显示默认对话框按钮上的倒计时,要选择默认按钮,请使用DefaultButtonIndex属性。

下面的示例演示了如何启用自动关闭选项:

C#:

private void Form1_FormClosing(object sender, FormClosingEventArgs e) {
// Creates and initializes an object with message box settings.
XtraMessageBoxArgs args = new XtraMessageBoxArgs() {
// Sets the caption of the message box.
Caption = "Confirmation",
// Sets the message of the message box.
Text = "Do you want to close the application?",
// Sets the buttons of the message box.
Buttons = new DialogResult[] { DialogResult.Yes, DialogResult.No },
// Sets the auto-close options of the message box.
AutoCloseOptions = new AutoCloseOptions() {
// Sets the delay before the message box automatically closes.
Delay = 5000,
// Displays the timer on the default button.
ShowTimerOnDefaultButton = true
}
};
// Displays the message box and checks if a user clicked "No".
if(XtraMessageBox.Show(args) == DialogResult.No)
e.Cancel = true;
}

VB.NET:

Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As FormClosingEventArgs)
' Creates and initializes an object with message box settings.
Dim args As XtraMessageBoxArgs = New XtraMessageBoxArgs() With {
' Sets the caption of the message box.
.Caption = "Confirmation",
' Sets the message of the message box.
.Text = "Do you want to close the application?",
' Sets the buttons of the message box.
.Buttons = New DialogResult() {DialogResult.Yes, DialogResult.No},
' Sets the auto-close options of the message box.
.AutoCloseOptions = New AutoCloseOptions() With {
' Sets the delay before the message box automatically closes.
.Delay = 5000,
' Displays the timer on the default button.
.ShowTimerOnDefaultButton = True
}
}
' Displays the message box and checks if a user clicked "No".
If XtraMessageBox.Show(args) Is DialogResult.No Then e.Cancel = True
End Sub

下面的屏幕截图说明了结果:

对话框的倒计时

屏蔽消息框

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

DevExpress 消息框

下面的代码示例演示了如何在XtraMessageBox消息框中使用 Do not show this message again复选框。

C#:

private void Form1_Load(object sender, EventArgs e) {
// Creates a new XtraMessageBoxArgs instance.
XtraMessageBoxArgs args = new XtraMessageBoxArgs() {
// Sets the caption of the message box.
Caption = "This is a trial version",
// Sets the message text to display in the message box.
Text = "You are using a trial version. The trial period expires in 30 days.",
// Sets buttons to display in the message box.
Buttons = new DialogResult[] { DialogResult.OK },
// Enables the "Do not show this message again" check box in the message box.
DoNotShowAgainCheckBoxVisible = true,
// Aligns the button(s) to the far right of the message box.
ButtonAlignment = DevExpress.Utils.HorzAlignment.Far,
};
// Attaches event handlers for when the message box is loaded and closed.
args.Load += Args_Load;
args.Closed += Args_Closed;
XtraMessageBox.Show(args);
}

// This method is called when the message box is closed.
private void Args_Closed(object sender, XtraMessageBoxClosedArgs e) {
// Saves the message box settings to the registry.
e.SaveToRegistry();
}

// This method is called when the message box is loaded.
private void Args_Load(object sender, XtraMessageBoxLoadArgs e) {
// Restores the message box settings from the registry.
e.RestoreFromRegistry();
}

VB.NET:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' Creates a new XtraMessageBoxArgs instance.
Dim args As New XtraMessageBoxArgs() With {
' Sets the caption of the message box.
.Caption = "This is a trial version",
' Sets the message text to display in the message box.
.Text = "You are using a trial version. The trial period expires in 30 days.",
' Sets the button(s) to display in the message box.
.Buttons = {DialogResult.OK},
' Enables the "Do not show this message again" check box in the message box.
.DoNotShowAgainCheckBoxVisible = True,
' Aligns the button(s) to the far right of the message box.
.ButtonAlignment = DevExpress.Utils.HorzAlignment.Far
}
' Attaches event handlers for when the message box is loaded and closed.
AddHandler args.Load, AddressOf Args_Load
AddHandler args.Closed, AddressOf Args_Closed
XtraMessageBox.Show(args)
End Sub

' This method is called when the message box is closed.
Private Sub Args_Closed(sender As Object, e As XtraMessageBoxClosedArgs)
' Saves the message box settings to the registry.
e.SaveToRegistry()
End Sub

' This method is called when the message box is loaded.
Private Sub Args_Load(sender As Object, e As XtraMessageBoxLoadArgs)
' Restores the message box settings from the registry.
e.RestoreFromRegistry()
End Sub

XtraMessageBoxArgs对象在消息显示或关闭时触发Load和Closed事件。处理这些事件来保存和恢复XtraMessageBoxEventArgs.Visible属性,该属性返回用户是否选中了该复选框。

使用SaveToRegistry()和RestoreFromRegistry()方法将Visible属性的值保存并加载到(或从)注册表中,您可以将此值存储在本地变量、本地存储文件或数据库中。

使用XtraMessageBoxLoadArgs.ShowMessage()方法和XtraMessageBoxArgs.Load事件强制显示消息,即使用户选择永远不会再看到它。

更改消息框图标

使用XtraMessageBoxArgs.Icon属性在消息框中显示自定义图标。

DevExpress 消息框

下面的示例演示如何在消息框中显示图标,在本例中,图标是从本地存储加载的。

C#:

private void Form1_FormClosing(object sender, FormClosingEventArgs e) {
// Creates and initializes an object with message box settings.
XtraMessageBoxArgs args = new XtraMessageBoxArgs() {
// Sets the caption of the message box.
Caption = "Confirmation",
// Sets the message of the message box.
Text = "Do you want to close the application?",
// Sets the buttons of the message box.
Buttons = new DialogResult[] { DialogResult.Yes, DialogResult.No },
// Sets the icon of the message box.
Icon = new Icon(@"C:\img\warning-16.ico"),
// Sets the auto-close options of the message box.
AutoCloseOptions = new AutoCloseOptions() {
// Sets the delay before the message box automatically closes.
Delay = 5000,
// Displays the timer on the default button.
ShowTimerOnDefaultButton = true
}
};
// Displays the message box and checks if a user clicked "No".
if(XtraMessageBox.Show(args) == DialogResult.No)
e.Cancel = true;
}

VB.NET:

Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As FormClosingEventArgs)
' Creates and initializes an object with message box settings.
Dim args As XtraMessageBoxArgs = New XtraMessageBoxArgs() With {
' Sets the caption of the message box.
.Caption = "Confirmation",
' Sets the message of the message box.
.Text = "Do you want to close the application?",
' Sets the buttons of the message box.
.Buttons = New DialogResult() {DialogResult.Yes, DialogResult.No},
' Sets the icon of the message box.
.Icon = New Icon("C:\img\warning-16.ico"),
' Sets the auto-close options of the message box.
.AutoCloseOptions = New AutoCloseOptions() With {
' Sets the delay before the message box automatically closes.
.Delay = 5000,
' Displays the timer on the default button.
.ShowTimerOnDefaultButton = True
}
}
' Displays the message box and checks if a user clicked "No".
If XtraMessageBox.Show(args) Is DialogResult.No Then e.Cancel = True
End Sub

下面的示例在消息框中显示来自SvgImageCollection的SVG图像:

DevExpress 消息框

C#:

private void Form1_FormClosing(object sender, FormClosingEventArgs e) {
// Creates and initializes an object with message box settings.
XtraMessageBoxArgs args = new XtraMessageBoxArgs() {
// Sets the caption of the message box.
Caption = "Warning",
// Sets the message of the message box.
Text = "Do you want to install this software?",
// Sets the buttons of the message box.
Buttons = new DialogResult[] { DialogResult.Yes, DialogResult.No },
// Sets the icon of the message box from the collection.
ImageOptions = new MessageBoxImageOptions() {
SvgImage = svgImageCollection1[0],
SvgImageSize = new Size(24, 24)
}
};
// Displays the message box and checks if a user clicked "No".
if(XtraMessageBox.Show(args) == DialogResult.Yes) {
e.Cancel = true;
}
}

VB.NET:

Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
' Creates and initializes an object with message box settings.
Dim args As New XtraMessageBoxArgs() With {
' Sets the caption of the message box.
.Caption = "Warning",
' Sets the message of the message box.
.Text = "Do you want to install this software?",
' Sets the buttons of the message box.
.Buttons = {DialogResult.Yes, DialogResult.No},
' Sets the icon of the message box from the collection.
.ImageOptions = New MessageBoxImageOptions() With {
.SvgImage = svgImageCollection1(0),
.SvgImageSize = New Size(24, 24)
}
}
' Displays the message box and checks if a user clicked "No".
If XtraMessageBox.Show(args) = DialogResult.Yes Then
e.Cancel = True
End If
End Sub

自定义消息框按钮

下面的例子处理显示事件并在消息框按钮中显示SVG图标:

DevExpress 消息框

C#:

// Initializes an XtraMessageBoxArgs object to hold message box arguments.
XtraMessageBoxArgs args = new XtraMessageBoxArgs();
// Sets the message box caption.
args.Caption = "Message";
// Sets the message box text.
args.Text = "Buttons in this message box show custom images.";
// Sets message box buttons.
args.Buttons = new DialogResult[] { DialogResult.OK, DialogResult.Cancel, DialogResult.Retry};
// Attaches an event handler to message box showing event.
args.Showing += Args_Showing;
// Shows the message box with the specified arguments.
XtraMessageBox.Show(args);

// The event handler for the message box Showing event.
private void Args_Showing(object sender, XtraMessageShowingArgs e) {
// Loops through all controls in the message box.
foreach (var control in e.MessageBoxForm.Controls) {
// Checks if a control is a SimpleButton.
SimpleButton button = control as SimpleButton;
if (button != null) {
// Sets the size of the button image to 16x16.
button.ImageOptions.SvgImageSize = new Size(16, 16);
// Sets a custom image for each button.
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:

' Initializes an XtraMessageBoxArgs object to hold message box arguments.
Dim args As XtraMessageBoxArgs = New XtraMessageBoxArgs
' Sets the message box caption.
args.Caption = "Message"
' Sets the message box text.
args.Text = "Buttons in this message box show custom images."
' Sets message box buttons.
args.Buttons = New DialogResult() {DialogResult.OK, DialogResult.Cancel, DialogResult.Retry}
' Attaches an event handler to the message box Showing event.
args.Showing = (args.Showing + Args_Showing)
' Shows the message box with the specified arguments.
XtraMessageBox.Show(args)

' The event handler for the message box Showing event.
Private Sub Args_Showing(ByVal sender As Object, ByVal e As XtraMessageShowingArgs)
' Loops through all controls in the message box.
For Each control In e.MessageBoxForm.Controls
' Checks if a control is a SimpleButton.
Dim button As SimpleButton = CType(control,SimpleButton)
If (Not (button) Is Nothing) Then
' Sets the size of the button image to 16x16.
button.ImageOptions.SvgImageSize = New Size(16, 16)
' Sets a custom image for each button.
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
End Sub

自定义外观和字体设置

下面的代码示例演示了如何增加消息框按钮的高度和更改字体设置。

DevExpress 消息框

C#:

// Creates a new instance of XtraMessageBoxArgs.
XtraMessageBoxArgs args = new XtraMessageBoxArgs();
// Sets the caption for the message box.
args.Caption = "Message";
// Sets the text for the message box.
args.Text = "This message has custom font settings.";
// Sets the buttons for the message box.
args.Buttons = new DialogResult[] { DialogResult.OK};
// Subscribes to the Showing event of the message box.
args.Showing += Args_Showing;

// Shows the message box and converts the result to a string.
XtraMessageBox.Show(args).ToString();

// The event handler for the Showing event of the message box.
private void Args_Showing(object sender, XtraMessageShowingArgs e) {
// Makes the message box caption bold.
e.Form.Appearance.FontStyleDelta = FontStyle.Bold;
// Increases the font size and height of the OK button.
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:

' Creates a new instance of XtraMessageBoxArgs.
Dim args As New XtraMessageBoxArgs()
' Sets the caption for the message box.
args.Caption = "Message"
' Sets the text for the message box.
args.Text = "This message has custom font settings."
' Sets the buttons for the message box.
args.Buttons = New DialogResult() {DialogResult.OK}
' Subscribes to the Showing event of the message box.
AddHandler args.Showing, AddressOf Args_Showing

' Shows the message box and converts the result to a string.
XtraMessageBox.Show(args).ToString()

' The event handler for the message box Showing event.
Private Sub Args_Showing(ByVal sender As Object, ByVal e As XtraMessageShowingArgs)
' Makes the message box caption bold.
e.Form.Appearance.FontStyleDelta = FontStyle.Bold
' Increases the font size and height of the OK button.
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支持HTML启发的文本格式,下面的代码示例演示了如何使用HTML标记来格式化消息框的内容:

DevExpress 消息框

C#:

private void Form1_FormClosing(object sender, FormClosingEventArgs e) {
// Creates a new instance of the XtraMessageBoxArgs class.
XtraMessageBoxArgs args = new XtraMessageBoxArgs();

// Sets AllowHtmlText to true to allow HTML formatting in the message box text.
args.AllowHtmlText = DefaultBoolean.True;

// Sets the size of the image displayed in the message box.
svgImageCollection1.ImageSize = new Size(24, 24);

// Sets the SVG images to be displayed in the message box.
args.HtmlImages = svgImageCollection1;

// Sets the caption of the message box.
args.Caption = "Error";

// Sets the message to be displayed in the message box.
args.Text = "<p align = center><image=actions_deletecircled><br><br>" +
"<b>Error</b><br>" +
"Oops, something went wrong.<br>" +
"Please try again later.</p>";

// Sets the buttons to be displayed in the message box.
args.Buttons = new DialogResult[] { DialogResult.Retry, DialogResult.Cancel };

// Displays the message box with the specified arguments.
XtraMessageBox.Show(args);
}

VB.NET:

Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As FormClosingEventArgs)
' Creates a new instance of the XtraMessageBoxArgs class.
Dim args As New XtraMessageBoxArgs()

' Sets AllowHtmlText to true to allow HTML formatting in the message box text.
args.AllowHtmlText = DefaultBoolean.True

' Sets the size of the image displayed in the message box.
svgImageCollection1.ImageSize = New Size(24, 24)

' Sets the SVG images to be displayed in the message box.
args.HtmlImages = svgImageCollection1

' Sets the caption of the message box.
args.Caption = "Error"

' Sets the message to be displayed in the message box.
args.Text = "<p align = center><image=actions_deletecircled><br><br>" & _
"<b>Error</b><br>" & _
"Oops, something went wrong.<br>" & _
"Please try again later.</p>"

' Sets the buttons to be displayed in the message box.
args.Buttons = New DialogResult() {DialogResult.Retry, DialogResult.Cancel}

' Displays the message box with the specified arguments.
XtraMessageBox.Show(args)
End Sub

更改按钮对齐方式

使用XtraMessage.BoxButtonsAlignment static(在VB中共享)属性来指定按钮对齐方式。

下面的代码示例演示了如何将消息框按钮向右对齐。

C#:

XtraMessageBox.ButtonsAlignment = HorizontalAlignment.Right;

VB.NET:

XtraMessageBox.ButtonsAlignment = HorizontalAlignment.Right
扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP