Question

I am seeking to override the System.Windows.Forms.MessageBox.Show() method to save modifying a monolithic codebase to use a new messagebox function.

I am using the Citrix Mobility SDK to instrument a legacy C# Windows application to facilitate more intuitive functionality on a mobile device such as iPad or Nexus tablet.

I wish to display a local messagebox on the device, and to do so, I need to replace all calls to MessageBox.Show in the application with a CustomMessageBox.Show function. The codebase is enormous, and I would rather not modify every file to use CustomMessageBox.Show as it would be a large and dangerous job, as well as resulting in tight coupling between the SDK and the application.

Thus, is there a way to override MessageBox.Show so that I can reimplement the function for use with this third party SDK, as it would be the most elegant option?

Was it helpful?

Solution

No, you cannot override MessageBox.Show because it is a static method.

OTHER TIPS

Given that you cannot override MessageBox.Show, there is still a solution that would allow it to work for mobile and non-mobile cases. The easiest way is to create a class that has members that mimic what MessageBox.Show can allow. In this class, it is aware of whether or not mobile is active. If it is not active, then it uses MessageBox.Show. Otherwise it uses CustomMessageBox.Show. Here is an example member function I have used for error messages in a recent sample.

/// <summary>
/// Show a error dialog box
/// </summary>
/// <param name="errorDialogText"></param>
public void ShowErrorDialog(String errorDialogText)
{
    if (!Visible || !IsMobile())
    {
        ErrorDialog.Show(errorDialogText);
    }
    else
    {
        ShowStatus(errorDialogText);
    }
}

ErrorDialog is a wrapper class for MessageBox. Note that IsMobile() knows that the mobile device is present or not.

public class ErrorDialog
{
    //
    // When an exception happens, we show the message here
    //
    public static void Show(String text)
    {
        MessageBox.Show(text, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }

    //
    // When an exception happens, we show the message here
    //
    public static void Show(Exception ex)
    {
        MessageBox.Show(ex.GetType().ToString() + ":" + ex.Message + ex.Source, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        MessageBox.Show(ex.StackTrace, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }

}

This is just abstraction. By creating a new function, you can wrap the two other functions. Inside the wrapper function, you can decide which one will be called. You will still have the pain of updating all the MessageBox.Show references but at least it will be under your control and you could change the abstracted function at any time in the future for whatever reason.

My example does not exactly match what you describe. Either you could create the function in the derived Form class or you could create an object and call the function from that object.

I work on the SDK at Citrix and am happy to help. There are a number of new posts on http://citrixblogger.org that might help out on other issues.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top