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.
Asked
Active
Viewed 1.6k times
4 Answers
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
-
1Just like to add. You can also set the DialogResult property of the form to match that of a message box. – Jojo Sardez Mar 01 '10 at 07:08
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);
Shows the form as a modal dialog box with the currently active window set as its owner.

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