1 // Permission is hereby granted, free of charge, to any person obtaining
2 // a copy of this software and associated documentation files (the
3 // "Software"), to deal in the Software without restriction, including
4 // without limitation the rights to use, copy, modify, merge, publish,
5 // distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to
7 // the following conditions:
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
18 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 // Copyright (c) 2004 Novell, Inc.
23 // Jordi Mas i Hernandez, jordi@ximian.com
24 // Benjamin Dasnois, benjamin.dasnois@gmail.com
27 // - Complete the implementation when icons are available, Form.BorderStyle is available.
28 // - Add support for MessageBoxOptions and MessageBoxDefaultButton.
31 // $Log: MessageBox.cs,v $
32 // Revision 1.4 2004/10/15 06:30:56 ravindra
33 // - MessageBox on windows does not have min/max buttons.
34 // This change in CreateParams fixes this on Windows. We
35 // still need to implement this windowstyle behavior in
38 // Revision 1.3 2004/10/14 06:14:03 ravindra
39 // Moved InitFormSize () out of Paint method and removed unnecessary calls to Button.Show () method.
41 // Revision 1.2 2004/10/05 17:10:57 pbartok
42 // - Partial implementation by Benjamin Dasnois
44 // Revision 1.1 2004/07/26 11:41:35 jordi
45 // initial messagebox implementation
54 namespace System
.Windows
.Forms
56 public class MessageBox
58 #region Private MessageBoxForm class
59 private class MessageBoxForm
: Form
61 #region MessageBoxFrom Local Variables
63 bool size_known
=false;
64 const int space_image_text
= 20;
67 MessageBoxButtons msgbox_buttons
;
68 bool buttons_placed
= false;
70 #endregion // MessageBoxFrom Local Variables
72 #region MessageBoxForm Constructors
73 public MessageBoxForm (IWin32Window owner
, string text
, string caption
,
74 MessageBoxButtons buttons
, MessageBoxIcon icon
)
77 case MessageBoxIcon
.None
: {
82 case MessageBoxIcon
.Error
: {
86 case MessageBoxIcon
.Question
: {
90 case MessageBoxIcon
.Exclamation
: {
94 case MessageBoxIcon
.Asterisk
: {
100 msgbox_buttons
= buttons
;
102 this.Paint
+= new PaintEventHandler (MessageBoxForm_Paint
);
106 public MessageBoxForm (IWin32Window owner
, string text
, string caption
,
107 MessageBoxButtons buttons
, MessageBoxIcon icon
,
108 MessageBoxDefaultButton defaultButton
, MessageBoxOptions options
) : this (owner
, text
, caption
, buttons
, icon
)
110 // Still needs to implement defaultButton and options
113 #endregion // MessageBoxForm Constructors
115 #region Protected Instance Properties
116 protected override CreateParams CreateParams
{
118 CreateParams create_params
= base.CreateParams
;
119 create_params
.Style
= (int)WindowStyles
.WS_OVERLAPPED
;
120 create_params
.Style
|= (int)WindowStyles
.WS_CAPTION
;
121 create_params
.Style
|= (int)WindowStyles
.WS_SYSMENU
;
122 create_params
.Style
|= (int)WindowStyles
.WS_VISIBLE
;
124 return create_params
;
127 #endregion // Protected Instance Properties
129 #region MessageBoxForm Methods
130 public DialogResult
RunDialog ()
132 this.StartPosition
= FormStartPosition
.CenterScreen
;
134 if (size_known
== false) {
140 return this.DialogResult
;
144 private void MessageBoxForm_Paint (object sender
, PaintEventArgs e
)
146 e
.Graphics
.DrawString (msgbox_text
, this.Font
, new SolidBrush(Color
.Black
), textleft_up
);
149 private void InitFormsSize ()
153 // First we have to know the size of text + image
154 Drawing
.SizeF tsize
= this.DeviceContext
.MeasureString (msgbox_text
, this.Font
);
156 if (!(icon_image
== null)) {
157 tsize
.Width
+= icon_image
.Width
;
158 textleft_up
= new Point (icon_image
.Width
+ space_image_text
, 0);
160 textleft_up
= new Point (0, 0);
163 // Now we want to know the width of buttons
165 switch (msgbox_buttons
) {
166 case MessageBoxButtons
.OK
: {
171 case MessageBoxButtons
.OKCancel
: {
176 case MessageBoxButtons
.AbortRetryIgnore
: {
181 case MessageBoxButtons
.YesNoCancel
: {
186 case MessageBoxButtons
.YesNo
: {
191 case MessageBoxButtons
.RetryCancel
: {
197 // Now we choose the good size for the form
198 if (tsize
.ToSize ().Width
> tb_width
) {
199 this.Width
= tsize
.ToSize().Width
+ 10;
201 this.Width
= tb_width
;
203 this.Height
= tsize
.ToSize ().Height
+ 80;
205 // Now we set the left of the buttons
206 button_left
= (this.Width
/ 2) - (tb_width
/ 2);
210 #endregion // MessageBoxForm Methods
212 #region Functions for Adding buttons
213 private void AddButtons()
215 if (!buttons_placed
) {
216 switch (msgbox_buttons
) {
217 case MessageBoxButtons
.OK
: {
218 AddOkButton (0 + button_left
);
222 case MessageBoxButtons
.OKCancel
: {
223 AddOkButton (0 + button_left
);
224 AddCancelButton (110 + button_left
);
228 case MessageBoxButtons
.AbortRetryIgnore
: {
229 AddAbortButton (0 + button_left
);
230 AddRetryButton (110 + button_left
);
231 AddIgnoreButton (220 + button_left
);
235 case MessageBoxButtons
.YesNoCancel
: {
236 AddYesButton (0 + button_left
);
237 AddNoButton (110 + button_left
);
238 AddCancelButton (220 + button_left
);
242 case MessageBoxButtons
.YesNo
: {
243 AddYesButton (0 + button_left
);
244 AddNoButton (110 + button_left
);
248 case MessageBoxButtons
.RetryCancel
: {
249 AddRetryButton (0 + button_left
);
250 AddCancelButton (110 + button_left
);
254 buttons_placed
= true;
258 private void AddOkButton (int left
)
260 Button bok
= new Button ();
264 bok
.Top
= this.Height
- 70;
266 bok
.Click
+= new EventHandler (OkClick
);
267 this.Controls
.Add (bok
);
270 private void AddCancelButton (int left
)
272 Button bcan
= new Button ();
273 bcan
.Text
= "Cancel";
276 bcan
.Top
= this.Height
- 70;
278 bcan
.Click
+= new EventHandler (CancelClick
);
279 this.Controls
.Add (bcan
);
282 private void AddAbortButton (int left
)
284 Button babort
= new Button ();
285 babort
.Text
= "Abort";
288 babort
.Top
= this.Height
- 70;
290 babort
.Click
+= new EventHandler (AbortClick
);
291 this.Controls
.Add (babort
);
294 private void AddRetryButton(int left
)
296 Button bretry
= new Button ();
297 bretry
.Text
= "Retry";
300 bretry
.Top
= this.Height
- 70;
302 bretry
.Click
+= new EventHandler (RetryClick
);
303 this.Controls
.Add (bretry
);
306 private void AddIgnoreButton (int left
)
308 Button bignore
= new Button ();
309 bignore
.Text
= "Ignore";
312 bignore
.Top
= this.Height
- 70;
314 bignore
.Click
+= new EventHandler (IgnoreClick
);
315 this.Controls
.Add (bignore
);
318 private void AddYesButton (int left
)
320 Button byes
= new Button ();
324 byes
.Top
= this.Height
- 70;
326 byes
.Click
+= new EventHandler (YesClick
);
327 this.Controls
.Add (byes
);
330 private void AddNoButton (int left
)
332 Button bno
= new Button ();
336 bno
.Top
= this.Height
- 70;
338 bno
.Click
+= new EventHandler (NoClick
);
339 this.Controls
.Add (bno
);
343 #region Button click handlers
344 private void OkClick (object sender
, EventArgs e
)
346 this.DialogResult
= DialogResult
.OK
;
350 private void CancelClick (object sender
, EventArgs e
)
352 this.DialogResult
= DialogResult
.Cancel
;
356 private void AbortClick (object sender
, EventArgs e
)
358 this.DialogResult
= DialogResult
.Abort
;
362 private void RetryClick (object sender
, EventArgs e
)
364 this.DialogResult
= DialogResult
.Retry
;
368 private void IgnoreClick (object sender
, EventArgs e
)
370 this.DialogResult
= DialogResult
.Ignore
;
374 private void YesClick (object sender
, EventArgs e
)
376 this.DialogResult
= DialogResult
.Yes
;
380 private void NoClick (object sender
, EventArgs e
)
382 this.DialogResult
= DialogResult
.No
;
387 #endregion // Private MessageBoxForm class
391 private MessageBox ()
395 #endregion // Constructors
397 #region Public Static Methods
398 public static DialogResult
Show (string text
)
400 MessageBoxForm form
= new MessageBoxForm (null, text
, string.Empty
,
401 MessageBoxButtons
.OK
, MessageBoxIcon
.None
);
403 return form
.RunDialog ();
407 public static DialogResult
Show (IWin32Window owner
, string text
)
409 MessageBoxForm form
= new MessageBoxForm (owner
, text
, string.Empty
,
410 MessageBoxButtons
.OK
, MessageBoxIcon
.None
);
412 return form
.RunDialog ();
416 public static DialogResult
Show (string text
, string caption
)
418 MessageBoxForm form
= new MessageBoxForm (null, text
, caption
,
419 MessageBoxButtons
.OK
, MessageBoxIcon
.None
);
421 return form
.RunDialog ();
424 public static DialogResult
Show (string text
, string caption
, MessageBoxButtons buttons
)
426 MessageBoxForm form
= new MessageBoxForm (null, text
, caption
,
427 buttons
, MessageBoxIcon
.None
);
429 return form
.RunDialog ();
432 public static DialogResult
Show (IWin32Window owner
, string text
, string caption
,
433 MessageBoxButtons buttons
)
435 MessageBoxForm form
= new MessageBoxForm (owner
, text
, caption
,
436 buttons
, MessageBoxIcon
.None
);
438 return form
.RunDialog ();
441 public static DialogResult
Show (IWin32Window owner
, string text
, string caption
,
442 MessageBoxButtons buttons
, MessageBoxIcon icon
)
444 MessageBoxForm form
= new MessageBoxForm (owner
, text
, caption
,
447 return form
.RunDialog ();
451 public static DialogResult
Show (IWin32Window owner
, string text
, string caption
)
453 MessageBoxForm form
= new MessageBoxForm (owner
, text
, caption
,
454 MessageBoxButtons
.OK
, MessageBoxIcon
.None
);
456 return form
.RunDialog ();
460 public static DialogResult
Show (string text
, string caption
, MessageBoxButtons buttons
,
463 MessageBoxForm form
= new MessageBoxForm (null, text
, caption
,
466 return form
.RunDialog ();
469 public static DialogResult
Show (string text
, string caption
, MessageBoxButtons buttons
,
470 MessageBoxIcon icon
, MessageBoxDefaultButton defaultButton
)
473 MessageBoxForm form
= new MessageBoxForm (null, text
, caption
,
474 buttons
, icon
, defaultButton
, MessageBoxOptions
.DefaultDesktopOnly
);
476 return form
.RunDialog ();
480 public static DialogResult
Show (IWin32Window owner
, string text
, string caption
,
481 MessageBoxButtons buttons
, MessageBoxIcon icon
, MessageBoxDefaultButton defaultButton
)
483 MessageBoxForm form
= new MessageBoxForm (owner
, text
, caption
,
484 buttons
, icon
, defaultButton
, MessageBoxOptions
.DefaultDesktopOnly
);
486 return form
.RunDialog ();
489 public static DialogResult
490 Show (string text
, string caption
,
491 MessageBoxButtons buttons
, MessageBoxIcon icon
,
492 MessageBoxDefaultButton defaultButton
, MessageBoxOptions options
)
494 MessageBoxForm form
= new MessageBoxForm (null, text
, caption
,
495 buttons
, icon
, defaultButton
, options
);
497 return form
.RunDialog ();
500 public static DialogResult
Show (IWin32Window owner
, string text
, string caption
,
501 MessageBoxButtons buttons
, MessageBoxIcon icon
,
502 MessageBoxDefaultButton defaultButton
, MessageBoxOptions options
)
504 MessageBoxForm form
= new MessageBoxForm (owner
, text
, caption
,
505 buttons
, icon
, defaultButton
, options
);
507 return form
.RunDialog ();
509 #endregion // Public Static Methods