XtraMessageBox
DevExpress消息框(XtraMessageBox)是一個(gè)完全可定制的控件,擴(kuò)展了標(biāo)準(zhǔn)Windows表單消息框的功能。它的高級(jí)功能包括:DevExpress皮膚,HTML啟發(fā)的文本格式,以及HTML和CSS模板。
 
 
顯示消息框
調(diào)用靜態(tài)(在VB中共享)XtraMessageBox.Show方法來顯示XtraMessageBox,方法重載允許您指定標(biāo)題、文本、按鈕、圖標(biāo)和其他外觀設(shè)置。下面的代碼示例防止在用戶單擊No時(shí)關(guān)閉應(yīng)用程序。
下面的示例演示如何顯示應(yīng)用程序退出確認(rèn)消息:
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類讓更復(fù)雜的布局顯示消息。
自動(dòng)關(guān)閉消息框
使用XtraMessageBox.Show(XtraMessageBoxArgs)方法顯示自動(dòng)關(guān)閉消息框,這個(gè)方法接受一個(gè)帶有消息框設(shè)置作為參數(shù)的XtraMessageBoxArgs對(duì)象。
使用AutoCloseOptions.Delay屬性來設(shè)置自動(dòng)關(guān)閉超時(shí)(以毫秒為單位)。啟用AutoCloseOptions.ShowTimerOnDefaultButton選項(xiàng)來顯示默認(rèn)對(duì)話框按鈕上的倒計(jì)時(shí),要選擇默認(rèn)按鈕,請(qǐng)使用DefaultButtonIndex屬性。
下面的示例演示了如何啟用自動(dòng)關(guān)閉選項(xiàng):
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
下面的屏幕截圖說明了結(jié)果:
 
 
屏蔽消息框
啟用XtraMessageBoxArgs.DoNotShowAgainCheckBoxVisible屬性,在消息框中顯示Do not show this message again復(fù)選框。
 
 
下面的代碼示例演示了如何在XtraMessageBox消息框中使用 Do not show this message again復(fù)選框。
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對(duì)象在消息顯示或關(guān)閉時(shí)觸發(fā)Load和Closed事件。處理這些事件來保存和恢復(fù)XtraMessageBoxEventArgs.Visible屬性,該屬性返回用戶是否選中了該復(fù)選框。
使用SaveToRegistry()和RestoreFromRegistry()方法將Visible屬性的值保存并加載到(或從)注冊(cè)表中,您可以將此值存儲(chǔ)在本地變量、本地存儲(chǔ)文件或數(shù)據(jù)庫中。
使用XtraMessageBoxLoadArgs.ShowMessage()方法和XtraMessageBoxArgs.Load事件強(qiáng)制顯示消息,即使用戶選擇永遠(yuǎn)不會(huì)再看到它。
更改消息框圖標(biāo)
使用XtraMessageBoxArgs.Icon屬性在消息框中顯示自定義圖標(biāo)。
 
 
下面的示例演示如何在消息框中顯示圖標(biāo),在本例中,圖標(biāo)是從本地存儲(chǔ)加載的。
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圖像:
 
 
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圖標(biāo):
 
 
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
自定義外觀和字體設(shè)置
下面的代碼示例演示了如何增加消息框按鈕的高度和更改字體設(shè)置。
 
 
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啟發(fā)的文本格式,下面的代碼示例演示了如何使用HTML標(biāo)記來格式化消息框的內(nèi)容:
 
 
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
更改按鈕對(duì)齊方式
使用XtraMessage.BoxButtonsAlignment static(在VB中共享)屬性來指定按鈕對(duì)齊方式。
下面的代碼示例演示了如何將消息框按鈕向右對(duì)齊。
C#:
XtraMessageBox.ButtonsAlignment = HorizontalAlignment.Right;
VB.NET:
XtraMessageBox.ButtonsAlignment = HorizontalAlignment.Right

 QQ交談
QQ交談 在線咨詢
在線咨詢 
                 
                
 渝公網(wǎng)安備
            50010702500608號(hào)
渝公網(wǎng)安備
            50010702500608號(hào)
             
            
 客服熱線
客服熱線