/
MessageBox.cs
236 lines (223 loc) · 8.9 KB
/
MessageBox.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
using System;
namespace Eto.Forms
{
/// <summary>
/// Message box buttons for methods of <see cref="MessageBox"/>
/// </summary>
/// <remarks>
/// This defined which buttons to show on the message box.
/// If there are different buttons you require, you can use <see cref="Dialog"/> instead to create your own.
/// </remarks>
public enum MessageBoxButtons
{
/// <summary>
/// Only a single OK button
/// </summary>
OK,
/// <summary>
/// OK and Cancel buttons
/// </summary>
OKCancel,
/// <summary>
/// Yes and no buttons
/// </summary>
YesNo,
/// <summary>
/// Yes, no, and cancel buttons
/// </summary>
YesNoCancel
}
/// <summary>
/// Message box type, to define the appearance of a <see cref="MessageBox"/>
/// </summary>
/// <remarks>
/// Usually the icon shown on the message box will change depending on this value.
/// </remarks>
public enum MessageBoxType
{
/// <summary>
/// Informational message box, usually signified a lower case 'i' icon
/// </summary>
Information,
/// <summary>
/// Warning message box, usually signified by an exclamation mark icon
/// </summary>
Warning,
/// <summary>
/// Error message box, usually signified by a stop sign
/// </summary>
Error,
/// <summary>
/// Question message box, usually signified by a question mark icon
/// </summary>
Question
}
/// <summary>
/// Message box default button selection for a <see cref="MessageBox"/>
/// </summary>
/// <remarks>
/// This enumeration specifies the default button of the message box, which is usually focussed when the message
/// box is shown, and selectable when the user presses the Return key.
/// </remarks>
public enum MessageBoxDefaultButton
{
/// <summary>
/// Automatically select the default button, preferring the negative form first such as <see cref="Cancel"/>, then <see cref="No"/>.
/// </summary>
Default,
/// <summary>
/// The OK button is default
/// </summary>
OK,
/// <summary>
/// The Yes button is default
/// </summary>
Yes = OK,
/// <summary>
/// The No button is default
/// </summary>
No,
/// <summary>
/// The Cancel button is default
/// </summary>
Cancel
}
/// <summary>
/// Methods to show a standard message box with display text, buttons, and typically an icon indicating the type
/// </summary>
[Handler(typeof(IHandler))]
public static class MessageBox
{
/// <summary>
/// Shows a message box, blocking input to all windows of the application until closed
/// </summary>
/// <param name="text">Text for the body of the message box</param>
/// <param name="type">Type of message box</param>
public static DialogResult Show(string text, MessageBoxType type = MessageBoxType.Information)
{
return Show(null, text, null, type);
}
/// <summary>
/// Shows a message box, blocking input to all windows of the application until closed
/// </summary>
/// <param name="text">Text for the body of the message box</param>
/// <param name="caption">Caption for the title bar or heading of the message box</param>
/// <param name="type">Type of message box</param>
public static DialogResult Show(string text, string caption, MessageBoxType type = MessageBoxType.Information)
{
return Show(null, text, caption, type);
}
/// <summary>
/// Shows a message box, blocking only the window of the specified <paramref name="parent"/>
/// </summary>
/// <param name="parent">Parent control that triggered the message box</param>
/// <param name="text">Text for the body of the message box</param>
/// <param name="type">Type of message box</param>
public static DialogResult Show(Control parent, string text, MessageBoxType type = MessageBoxType.Information)
{
return Show(parent, text, null, type);
}
/// <summary>
/// Shows a message box, blocking only the window of the specified <paramref name="parent"/>
/// </summary>
/// <param name="parent">Parent control that triggered the message box</param>
/// <param name="text">Text for the body of the message box</param>
/// <param name="type">Type of message box</param>
/// <param name="caption">Caption for the title bar or heading of the message box</param>
public static DialogResult Show(Control parent, string text, string caption, MessageBoxType type = MessageBoxType.Information)
{
return Show(parent, text, caption, MessageBoxButtons.OK, type);
}
/// <summary>
/// Shows a message box, blocking input to all windows of the application until closed
/// </summary>
/// <param name="text">Text for the body of the message box</param>
/// <param name="type">Type of message box</param>
/// <param name="buttons">Buttons to show on the message box</param>
/// <param name="defaultButton">Button to set focus to by default</param>
public static DialogResult Show(string text, MessageBoxButtons buttons, MessageBoxType type = MessageBoxType.Information, MessageBoxDefaultButton defaultButton = MessageBoxDefaultButton.Default)
{
return Show((Control)null, text, buttons, type, defaultButton);
}
/// <summary>
/// Shows a message box, blocking input to all windows of the application until closed
/// </summary>
/// <param name="text">Text for the body of the message box</param>
/// <param name="type">Type of message box</param>
/// <param name="buttons">Buttons to show on the message box</param>
/// <param name="defaultButton">Button to set focus to by default</param>
/// <param name="caption">Caption for the title bar or heading of the message box</param>
public static DialogResult Show(string text, string caption, MessageBoxButtons buttons, MessageBoxType type = MessageBoxType.Information, MessageBoxDefaultButton defaultButton = MessageBoxDefaultButton.Default)
{
return Show((Control)null, text, caption, buttons, type, defaultButton);
}
/// <summary>
/// Shows a message box, blocking only the window of the specified <paramref name="parent"/>
/// </summary>
/// <param name="parent">Parent control that triggered the message box</param>
/// <param name="text">Text for the body of the message box</param>
/// <param name="type">Type of message box</param>
/// <param name="buttons">Buttons to show on the message box</param>
/// <param name="defaultButton">Button to set focus to by default</param>
public static DialogResult Show(Control parent, string text, MessageBoxButtons buttons, MessageBoxType type = MessageBoxType.Information, MessageBoxDefaultButton defaultButton = MessageBoxDefaultButton.Default)
{
return Show(parent, text, null, buttons, type, defaultButton);
}
/// <summary>
/// Shows a message box, blocking only the window of the specified <paramref name="parent"/>
/// </summary>
/// <param name="parent">Parent control that triggered the message box</param>
/// <param name="text">Text for the body of the message box</param>
/// <param name="caption">Caption for the title bar or heading of the message box</param>
/// <param name="type">Type of message box</param>
/// <param name="buttons">Buttons to show on the message box</param>
/// <param name="defaultButton">Button to set focus to by default</param>
public static DialogResult Show(Control parent, string text, string caption, MessageBoxButtons buttons, MessageBoxType type = MessageBoxType.Information, MessageBoxDefaultButton defaultButton = MessageBoxDefaultButton.Default)
{
var mb = Platform.Instance.Create<IHandler>();
mb.Text = text;
mb.Caption = caption;
mb.Type = type;
mb.Buttons = buttons;
mb.DefaultButton = defaultButton;
return mb.ShowDialog(parent);
}
/// <summary>
/// Handler interface for the <see cref="MessageBox"/>
/// </summary>
public interface IHandler
{
/// <summary>
/// Gets or sets the body text of the message box.
/// </summary>
/// <value>The body text.</value>
string Text { get; set; }
/// <summary>
/// Gets or sets the caption for the title bar or heading of the message box.
/// </summary>
/// <value>The caption.</value>
string Caption { get; set; }
/// <summary>
/// Gets or sets the type of message box, usually changing the icon displayed.
/// </summary>
/// <value>The type of message box.</value>
MessageBoxType Type { get; set; }
/// <summary>
/// Gets or sets which buttons to show on the message box.
/// </summary>
/// <value>The buttons to show.</value>
MessageBoxButtons Buttons { get; set; }
/// <summary>
/// Gets or sets the default button, or <see cref="MessageBoxDefaultButton.Default"/> to automatically select either Cancel or No if displayed.
/// </summary>
/// <value>The default button.</value>
MessageBoxDefaultButton DefaultButton { get; set; }
/// <summary>
/// Shows the dialog.
/// </summary>
/// <returns>The dialog result.</returns>
/// <param name="parent">Optional parent. If specified, the parent's window should be blocked from input. If null, all windows should be blocked.</param>
DialogResult ShowDialog(Control parent);
}
}
}