XtraMessageBox
- 4 minutes to read
The XtraMessageBox replaces standard Windows Forms message boxes which do not support DevExpress skins.
Show Message Boxes
To display an XtraMessageBox, call the static XtraMessageBox.Show method. The sample code below prevents an application from closing if a user clicks “No”.
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;
}
}
Tip
You can use the XtraDialog class to display messages with a more complex layout.
Auto-Close Message Boxes
The Show(XtraMessageBoxArgs args) method overload allows you to show a message box that closes automatically. The method parameter is the XtraMessageBoxArgs class object and provides the AutoCloseOptions.Delay property that allows you to set the auto-close timeout (in milliseconds).
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();
The first message box button (“OK” in the sample above) is a default button - if a user presses “Enter” or the auto-closing timer expires, this button is considered clicked, and the message box returns the corresponding DialogResult value. This button also displays the countdown timer for auto-closing messages.
You can modify the XtraMessageBoxArgs.DefaultButtonIndex to select the default button and disable the AutoCloseOptions.ShowTimerOnDefaultButton setting to hide the countdown timer.
// change the default button
args.DefaultButtonIndex = 1;
// set to false to hide the countdown
args.AutoCloseOptions.ShowTimerOnDefaultButton = true;
Additional Customization
Invoke message boxes using the Show(XtraMessageBoxArgs args) method overload and handle this method parameter’s Showing event to perform message box customizations.
Modify message box buttons
The code below illustrates how to add custom icons to message box buttons. In this example, icons are vector images stored in the external SvgImageCollection.
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;
}
}
}
}
Change appearance and font settings
The example below increases the message box button’s height and font size, and makes the text bold.
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;
}
}
Change button alignment
Use the XtraMessageBox.ButtonsAlignment static (Shared in VB) property to specify the button alignment. The example below aligns message box buttons at the right.