Back to Devexpress

IMessageBoxService Interface

officefileapi-devexpress-dot-xtraspreadsheet-dot-services-41e49260.md

latest9.4 KB
Original Source

IMessageBoxService Interface

A service that enables you to display a custom message box when a runtime error occurs.

Namespace : DevExpress.XtraSpreadsheet.Services

Assembly : DevExpress.Spreadsheet.v25.2.Core.dll

NuGet Package : DevExpress.Spreadsheet.Core

Declaration

csharp
public interface IMessageBoxService
vb
Public Interface IMessageBoxService

Remarks

The example below shows how to create and register a custom service. Create a class that implements the IMessageBoxService interface and use the SpreadsheetControl.ReplaceService<T> method to register the custom service instead of the default one.

csharp
MyMessageBoxService svc = new MyMessageBoxService(spreadsheetControl1, spreadsheetControl1.LookAndFeel);
spreadsheetControl1.ReplaceService<DevExpress.XtraSpreadsheet.Services.IMessageBoxService>(svc);
vb
Dim svc As New MyMessageBoxService(spreadsheetControl1, spreadsheetControl1.LookAndFeel)
spreadsheetControl1.ReplaceService(Of DevExpress.XtraSpreadsheet.Services.IMessageBoxService)(svc)
csharp
using System.Windows.Forms;
using DevExpress.LookAndFeel;
using DevExpress.XtraEditors;
using DevExpress.Spreadsheet;
using DevExpress.Portable;
// ...

class MyMessageBoxService : DevExpress.XtraSpreadsheet.Services.IMessageBoxService
    {
        readonly Control control;
        readonly UserLookAndFeel lookAndFeel;

        public MyMessageBoxService(Control control, UserLookAndFeel lookAndFeel)
        {
            this.control = control;
            this.lookAndFeel = lookAndFeel;
        }

        // To test: select a range of cells with and without data validation settings
        // and click Data Validation command to invoke the Data Validation dialog.
        public PortableDialogResult ShowYesNoCancelMessage(string message) {
            string myMessage = "Aktion auswählen\n\n" + message;
            return (PortableDialogResult)XtraMessageBox.Show(lookAndFeel, control, myMessage, Application.ProductName, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information);
        }

        // To test: enter incorrect data into a cell with a data validation rule applied.
        public PortableDialogResult ShowDataValidationDialog(string message, string title, DataValidationErrorStyle errorStyle) {
            string myMessage = "Der eingegebene Wert ist ungültig.\n\n" + message;
            MessageBoxButtons buttons;
            MessageBoxIcon icon;
            if(errorStyle == DataValidationErrorStyle.Stop) {
                buttons = MessageBoxButtons.RetryCancel;
                icon = MessageBoxIcon.Error;
            } else if(errorStyle == DataValidationErrorStyle.Warning) {
                buttons = MessageBoxButtons.YesNoCancel;
                icon = MessageBoxIcon.Warning;
            } else {
                buttons = MessageBoxButtons.OKCancel;
                icon = MessageBoxIcon.Information;
            }
            return (PortableDialogResult)XtraMessageBox.Show(lookAndFeel, control, myMessage, title, buttons, icon);
        }

        // To test: click the Test button or try to set the row height to 1000.
        public PortableDialogResult ShowMessage(string message, string title, PortableMessageBoxIcon icon) {
            string myMessage = "Ein Fehler tritt auf.\n\n" + message;
            var myIcon = (MessageBoxIcon)icon;
            return (PortableDialogResult)XtraMessageBox.Show(lookAndFeel, control, myMessage, title, MessageBoxButtons.OK, myIcon);
        }

        // To test: drag and drop cells to another location trying to overwrite the content of the destination cells.
        public bool ShowOkCancelMessage(string message)
        {
            string myMessage = "Möchten Sie diesen Vorgang wirklich ausführen?\n\n" + message;
            return PortableDialogResult.OK == (PortableDialogResult)XtraMessageBox.Show(lookAndFeel, control, myMessage, Application.ProductName, MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
        }

        // To test: try to delete a defined name in the Name Manager dialog.
        public bool ShowYesNoMessage(string message)
        {
            string myMessage = "Entscheidung treffen. Ja oder Nein?\n\n" + message;
            return PortableDialogResult.Yes == (PortableDialogResult)XtraMessageBox.Show(lookAndFeel, control, myMessage, Application.ProductName, MessageBoxButtons.YesNo, MessageBoxIcon.Question);
        }
    }
vb
Imports DevExpress.LookAndFeel
Imports DevExpress.XtraEditors
Imports DevExpress.Spreadsheet
Imports DevExpress.Portable
' ...

    Friend Class MyMessageBoxService
        Implements DevExpress.XtraSpreadsheet.Services.IMessageBoxService

        Private ReadOnly control As Control
        Private ReadOnly lookAndFeel As UserLookAndFeel

        Public Sub New(ByVal control As Control, ByVal lookAndFeel As UserLookAndFeel)
            Me.control = control
            Me.lookAndFeel = lookAndFeel
        End Sub

        ' To test: select a range of cells with and without data validation settings
        ' and click Data Validation command to invoke the Data Validation dialog.
        Public Function ShowYesNoCancelMessage(ByVal message As String) As PortableDialogResult Implements DevExpress.XtraSpreadsheet.Services.IMessageBoxService.ShowYesNoCancelMessage
            Dim myMessage As String = "Aktion auswählen" & ControlChars.Lf & ControlChars.Lf & message
            Return CType(XtraMessageBox.Show(lookAndFeel, control, myMessage, Application.ProductName, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information), PortableDialogResult)
        End Function

        ' To test: enter incorrect data into a cell with a data validation rule applied.
        Public Function ShowDataValidationDialog(ByVal message As String, ByVal title As String, ByVal errorStyle As DataValidationErrorStyle) As PortableDialogResult Implements DevExpress.XtraSpreadsheet.Services.IMessageBoxService.ShowDataValidationDialog
            Dim myMessage As String = "Der eingegebene Wert ist ungültig." & ControlChars.Lf & ControlChars.Lf & message
            Dim buttons As MessageBoxButtons
            Dim icon As MessageBoxIcon
            If errorStyle = DataValidationErrorStyle.Stop Then
                buttons = MessageBoxButtons.RetryCancel
                icon = MessageBoxIcon.Error
            ElseIf errorStyle = DataValidationErrorStyle.Warning Then
                buttons = MessageBoxButtons.YesNoCancel
                icon = MessageBoxIcon.Warning
            Else
                buttons = MessageBoxButtons.OKCancel
                icon = MessageBoxIcon.Information
            End If
            Return CType(XtraMessageBox.Show(lookAndFeel, control, myMessage, title, buttons, icon), PortableDialogResult)
        End Function

        ' To test: click the Test button or try to set the row height to 1000.
        Public Function ShowMessage(ByVal message As String, ByVal title As String, ByVal icon As PortableMessageBoxIcon) As PortableDialogResult Implements DevExpress.XtraSpreadsheet.Services.IMessageBoxService.ShowMessage
            Dim myMessage As String = "Ein Fehler tritt auf." & ControlChars.Lf & ControlChars.Lf & message
            Dim myIcon = CType(icon, MessageBoxIcon)
            Return CType(XtraMessageBox.Show(lookAndFeel, control, myMessage, title, MessageBoxButtons.OK, myIcon), PortableDialogResult)
        End Function

        ' To test: drag and drop cells to another location trying to overwrite the content of the destination cells.
        Public Function ShowOkCancelMessage(ByVal message As String) As Boolean Implements DevExpress.XtraSpreadsheet.Services.IMessageBoxService.ShowOkCancelMessage
            Dim myMessage As String = "Möchten Sie diesen Vorgang wirklich ausführen?" & ControlChars.Lf & ControlChars.Lf & message
            Return PortableDialogResult.OK = CType(XtraMessageBox.Show(lookAndFeel, control, myMessage, Application.ProductName, MessageBoxButtons.OKCancel, MessageBoxIcon.Information), PortableDialogResult)
        End Function

        ' To test: try to delete a defined name in the Name Manager dialog.
        Public Function ShowYesNoMessage(ByVal message As String) As Boolean Implements DevExpress.XtraSpreadsheet.Services.IMessageBoxService.ShowYesNoMessage
            Dim myMessage As String = "Entscheidung treffen. Ja oder Nein?" & ControlChars.Lf & ControlChars.Lf & message
            Return PortableDialogResult.Yes = CType(XtraMessageBox.Show(lookAndFeel, control, myMessage, Application.ProductName, MessageBoxButtons.YesNo, MessageBoxIcon.Question), PortableDialogResult)
    End Function

    End Class

See Also

IMessageBoxService Members

DevExpress.XtraSpreadsheet.Services Namespace