Skip to main content
A newer version of this page is available. .

XtraMessageBox

  • 4 minutes to read

The XtraMessageBox replaces standard Windows Forms message boxes which do not support DevExpress skins.

DXMessageBox Collage

Show Message Boxes

To display an XtraMessageBox, call the static (Shared in VB) XtraMessageBox.Show method. The method overloads allow you to specify the message’s caption, icon, text, buttons, etc. 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 XtraMessageBox.Show(XtraMessageBoxArgs) 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.

MessageBox - CountDown

You can modify the DefaultButtonIndex property 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 XtraMessageBox.Show(XtraMessageBoxArgs) 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.

MessageBox - Custom Icons

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.

MessageBox - Custom Font

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.

XtraMessageBox.ButtonsAlignment = HorizontalAlignment.Right;