Search code examples
c#messagebox

C# - Use form instead of messagebox


I am working on an application for work and I need a customized messagebox to appear. I have created a simple form called Alert.cs that I have styled the way I want and added one button with a click method of this.Close(). now I want it to behave exactly like a standard messagebox.show(). I have the form showing but when I use the standard messagebox.show("text of alert") it waits to continue operation until the user click 'OK', this is what I want the form to do.


Solution

  • You will need to implement a static method for your Alert class if you want the exact MessageBox-like behaviour.

    public static DialogResult Show(string text)
    {
        Alert form = new Alert(text);
        return form.ShowDialog();
    }
    

    Now you can use the form by calling:

    Alert.Show("my message");