8

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.

Jake Sankey
  • 4,977
  • 12
  • 39
  • 53

4 Answers4

12

Use Form.ShowDialog();. This allows the form to act the same way as a MessageBox in the sense that it retains focus until closed.

Kyle Rosendo
  • 25,001
  • 7
  • 80
  • 118
6

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");
kor_
  • 1,510
  • 1
  • 16
  • 36
  • Just what I was looking for! This saves me from needing to create a temporary instance variable just to call it :) – Kaitlyn Sep 05 '15 at 14:34
3

You can use a modal windows form. Something like

Form frm = new Form();
frm.ShowDialog(this);

See Form.ShowDialog Method

Shows the form as a modal dialog box with the currently active window set as its owner.

Displaying Modal and Modeless Windows Forms

rahul
  • 184,426
  • 49
  • 232
  • 263
0

You don't write how you currently display your Alert Form, but calling

alert.ShowDialog();

instead of alert.Show() should do the trick.

The ShowDialog that takes an owner is an even better alternative:

alert.ShowDialog(owner);
Mark Seemann
  • 225,310
  • 48
  • 427
  • 736