Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a custom MessageBox?

I'm trying to make a custom message box with my controls.

public static partial class Msg : Form
{
    public static void show(string content, string description)
    {

    }
}

Actually I need to place some controls (a gridview) in this form and I have to apply my own theme for this window, so I don't want to use MessageBox. I want to call this from my other forms like

Msg.show(parameters);

I don't wish to create an object for this form.

I know I can't inherit from Form class because it isn't static. But I wonder how MessageBox is implemented, because it is static. It is being called like MessageBox.show("Some message!");

Now I'm getting an error because inheritance is not allowed:

Static class 'MyFormName' cannot derive from type 'System.Windows.Forms.Form'. Static classes must derive from object

Screenshot of my form

How MessageBox is implemented then?

like image 613
Sen Jacob Avatar asked Aug 03 '11 20:08

Sen Jacob


People also ask

How do I customize my message box?

The code for the custom message box is quite simple. First we need to declare a variable called Button_id , this variable will hold a number either 1 or 2. If the user clicks the OK button, this variable will be set to 1 and if the user clicks the Cancel button the variable will be set to 2.

How do you insert a message box in Visual Basic?

The MsgBox function displays a message box and waits for the user to click a button and then an action is performed based on the button clicked by the user.


1 Answers

Your form class needs not to be static. In fact, a static class cannot inherit at all.

Instead, create an internal form class that derives from Form and provide a public static helper method to show it.

This static method may be defined in a different class if you don't want the callers to even “know” about the underlying form.

/// <summary>
/// The form internally used by <see cref="CustomMessageBox"/> class.
/// </summary>
internal partial class CustomMessageForm : Form
{
    /// <summary>
    /// This constructor is required for designer support.
    /// </summary>
    public CustomMessageForm ()
    {
        InitializeComponent(); 
    } 

    public CustomMessageForm (string title, string description)
    {
        InitializeComponent(); 

        this.titleLabel.Text = title;
        this.descriptionLabel.Text = description;
    } 
}

/// <summary>
/// Your custom message box helper.
/// </summary>
public static class CustomMessageBox
{
    public static void Show (string title, string description)
    {
        // using construct ensures the resources are freed when form is closed
        using (var form = new CustomMessageForm (title, description)) {
            form.ShowDialog ();
        }
    }
}

Side note: as Jalal points out, you don't have to make a class static in order to have static methods in it. But I would still separate the “helper” class from the actual form so the callers cannot create the form with a constructor (unless they're in the same assembly of course).

like image 105
Dan Abramov Avatar answered Sep 28 '22 06:09

Dan Abramov