-
Notifications
You must be signed in to change notification settings - Fork 7
/
MessageBox.cs
944 lines (811 loc) · 47.3 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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
/* FlexibleMessageBox – A flexible replacement for the .NET MessageBox
*
* Author: Jörg Reichert (public@jreichert.de)
* Contributors: Thanks to: David Hall, Roink
* Version: 1.3
* Published at: http://www.codeproject.com/Articles/601900/FlexibleMessageBox
*
* Bug fixes and enhancements ©2019 by BoltBait
*/
using PaintDotNet;
using System;
using System.ComponentModel;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Windows.Forms;
namespace PdnCodeLab
{
public static class FlexibleMessageBox
{
#region Public statics
/// <summary>
/// Defines the maximum width for all FlexibleMessageBox instances in percent of the working area.
///
/// Allowed values are 0.2 - 1.0 where:
/// 0.2 means: The FlexibleMessageBox can be at most half as wide as the working area.
/// 1.0 means: The FlexibleMessageBox can be as wide as the working area.
///
/// Default is: 70% of the working area width.
/// </summary>
public static double MAX_WIDTH_FACTOR = 0.7;
/// <summary>
/// Defines the maximum height for all FlexibleMessageBox instances in percent of the working area.
///
/// Allowed values are 0.2 - 1.0 where:
/// 0.2 means: The FlexibleMessageBox can be at most half as high as the working area.
/// 1.0 means: The FlexibleMessageBox can be as high as the working area.
///
/// Default is: 90% of the working area height.
/// </summary>
public static double MAX_HEIGHT_FACTOR = 0.9;
/// <summary>
/// Defines the font for all FlexibleMessageBox instances.
///
/// Default is: SystemFonts.MessageBoxFont
/// </summary>
public static Font FONT = SystemFonts.MessageBoxFont;
#endregion
#region Public show functions
/// <summary>
/// Shows the specified message box.
/// </summary>
/// <param name="text">The text.</param>
/// <returns>The dialog result.</returns>
public static DialogResult Show(string text)
{
return FlexibleMessageBoxForm.Show(null, text, string.Empty, MessageBoxButtons.OK, MessageBoxIcon.None, MessageBoxDefaultButton.Button1);
}
/// <summary>
/// Shows the specified message box.
/// </summary>
/// <param name="owner">The owner.</param>
/// <param name="text">The text.</param>
/// <returns>The dialog result.</returns>
public static DialogResult Show(IWin32Window owner, string text)
{
return FlexibleMessageBoxForm.Show(owner, text, string.Empty, MessageBoxButtons.OK, MessageBoxIcon.None, MessageBoxDefaultButton.Button1);
}
/// <summary>
/// Shows the specified message box.
/// </summary>
/// <param name="text">The text.</param>
/// <param name="caption">The caption.</param>
/// <returns>The dialog result.</returns>
public static DialogResult Show(string text, string caption)
{
return FlexibleMessageBoxForm.Show(null, text, caption, MessageBoxButtons.OK, MessageBoxIcon.None, MessageBoxDefaultButton.Button1);
}
/// <summary>
/// Shows the specified message box.
/// </summary>
/// <param name="owner">The owner.</param>
/// <param name="text">The text.</param>
/// <param name="caption">The caption.</param>
/// <returns>The dialog result.</returns>
public static DialogResult Show(IWin32Window owner, string text, string caption)
{
return FlexibleMessageBoxForm.Show(owner, text, caption, MessageBoxButtons.OK, MessageBoxIcon.None, MessageBoxDefaultButton.Button1);
}
/// <summary>
/// Shows the specified message box.
/// </summary>
/// <param name="text">The text.</param>
/// <param name="caption">The caption.</param>
/// <param name="buttons">The buttons.</param>
/// <returns>The dialog result.</returns>
public static DialogResult Show(string text, string caption, MessageBoxButtons buttons)
{
return FlexibleMessageBoxForm.Show(null, text, caption, buttons, MessageBoxIcon.None, MessageBoxDefaultButton.Button1);
}
// Added ability to have custom button text - BoltBait
public static DialogResult Show(string text, string caption, string[] buttons)
{
return FlexibleMessageBoxForm.Show(null, text, caption, buttons, MessageBoxIcon.None, MessageBoxDefaultButton.Button1);
}
/// <summary>
/// Shows the specified message box.
/// </summary>
/// <param name="owner">The owner.</param>
/// <param name="text">The text.</param>
/// <param name="caption">The caption.</param>
/// <param name="buttons">The buttons.</param>
/// <returns>The dialog result.</returns>
public static DialogResult Show(IWin32Window owner, string text, string caption, MessageBoxButtons buttons)
{
return FlexibleMessageBoxForm.Show(owner, text, caption, buttons, MessageBoxIcon.None, MessageBoxDefaultButton.Button1);
}
// Added ability to have custom button text - BoltBait
public static DialogResult Show(IWin32Window owner, string text, string caption, string[] buttons)
{
return FlexibleMessageBoxForm.Show(owner, text, caption, buttons, MessageBoxIcon.None, MessageBoxDefaultButton.Button1);
}
/// <summary>
/// Shows the specified message box.
/// </summary>
/// <param name="text">The text.</param>
/// <param name="caption">The caption.</param>
/// <param name="buttons">The buttons.</param>
/// <param name="icon">The icon.</param>
/// <returns></returns>
public static DialogResult Show(string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon)
{
return FlexibleMessageBoxForm.Show(null, text, caption, buttons, icon, MessageBoxDefaultButton.Button1);
}
// Added ability to have custom button text - BoltBait
public static DialogResult Show(string text, string caption, string[] buttons, MessageBoxIcon icon)
{
return FlexibleMessageBoxForm.Show(null, text, caption, buttons, icon, MessageBoxDefaultButton.Button1);
}
/// <summary>
/// Shows the specified message box.
/// </summary>
/// <param name="owner">The owner.</param>
/// <param name="text">The text.</param>
/// <param name="caption">The caption.</param>
/// <param name="buttons">The buttons.</param>
/// <param name="icon">The icon.</param>
/// <returns>The dialog result.</returns>
public static DialogResult Show(IWin32Window owner, string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon)
{
return FlexibleMessageBoxForm.Show(owner, text, caption, buttons, icon, MessageBoxDefaultButton.Button1);
}
// Added ability to have custom button text - BoltBait
public static DialogResult Show(IWin32Window owner, string text, string caption, string[] buttons, MessageBoxIcon icon)
{
return FlexibleMessageBoxForm.Show(owner, text, caption, buttons, icon, MessageBoxDefaultButton.Button1);
}
/// <summary>
/// Shows the specified message box.
/// </summary>
/// <param name="text">The text.</param>
/// <param name="caption">The caption.</param>
/// <param name="buttons">The buttons.</param>
/// <param name="icon">The icon.</param>
/// <param name="defaultButton">The default button.</param>
/// <returns>The dialog result.</returns>
public static DialogResult Show(string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton)
{
return FlexibleMessageBoxForm.Show(null, text, caption, buttons, icon, defaultButton);
}
// Added ability to have custom button text - BoltBait
public static DialogResult Show(string text, string caption, string[] buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton)
{
return FlexibleMessageBoxForm.Show(null, text, caption, buttons, icon, defaultButton);
}
/// <summary>
/// Shows the specified message box.
/// </summary>
/// <param name="owner">The owner.</param>
/// <param name="text">The text.</param>
/// <param name="caption">The caption.</param>
/// <param name="buttons">The buttons.</param>
/// <param name="icon">The icon.</param>
/// <param name="defaultButton">The default button.</param>
/// <returns>The dialog result.</returns>
public static DialogResult Show(IWin32Window owner, string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton)
{
return FlexibleMessageBoxForm.Show(owner, text, caption, buttons, icon, defaultButton);
}
// Added ability to have custom button text - BoltBait
public static DialogResult Show(IWin32Window owner, string text, string caption, string[] buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton)
{
return FlexibleMessageBoxForm.Show(owner, text, caption, buttons, icon, defaultButton);
}
#endregion
#region Internal form class
/// <summary>
/// The form to show the customized message box.
/// It is defined as an internal class to keep the public interface of the FlexibleMessageBox clean.
/// </summary>
private class FlexibleMessageBoxForm : PdnBaseForm
{
#region Form-Designer generated code
/// <summary>
/// Erforderliche Designervariable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Verwendete Ressourcen bereinigen.
/// </summary>
/// <param name="disposing">True, wenn verwaltete Ressourcen gelöscht werden sollen; andernfalls False.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
/// <summary>
/// Erforderliche Methode für die Designerunterstützung.
/// Der Inhalt der Methode darf nicht mit dem Code-Editor geändert werden.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.button1 = new System.Windows.Forms.Button();
this.richTextBoxMessage = new System.Windows.Forms.RichTextBox();
this.FlexibleMessageBoxFormBindingSource = new System.Windows.Forms.BindingSource(this.components);
this.panel1 = new System.Windows.Forms.Panel();
this.pictureBoxForIcon = new System.Windows.Forms.PictureBox();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.FlexibleMessageBoxFormBindingSource)).BeginInit();
this.panel1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBoxForIcon)).BeginInit();
this.SuspendLayout();
//
// button1
//
this.button1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.button1.AutoSize = true;
this.button1.DialogResult = System.Windows.Forms.DialogResult.OK;
this.button1.Location = new System.Drawing.Point(11, 67);
this.button1.MinimumSize = new System.Drawing.Size(0, 24);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 24);
this.button1.TabIndex = 2;
this.button1.Text = "OK";
this.button1.FlatStyle = System.Windows.Forms.FlatStyle.System;
this.button1.UseVisualStyleBackColor = true;
this.button1.Visible = false;
//
// richTextBoxMessage
//
this.richTextBoxMessage.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.richTextBoxMessage.BackColor = System.Drawing.Color.White;
this.richTextBoxMessage.BorderStyle = System.Windows.Forms.BorderStyle.None;
this.richTextBoxMessage.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.FlexibleMessageBoxFormBindingSource, "MessageText", true, System.Windows.Forms.DataSourceUpdateMode.OnPropertyChanged));
this.richTextBoxMessage.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.richTextBoxMessage.Location = new System.Drawing.Point(50, 26);
this.richTextBoxMessage.Margin = new System.Windows.Forms.Padding(0);
this.richTextBoxMessage.Name = "richTextBoxMessage";
this.richTextBoxMessage.ReadOnly = true;
this.richTextBoxMessage.ScrollBars = System.Windows.Forms.RichTextBoxScrollBars.Vertical;
this.richTextBoxMessage.Size = new System.Drawing.Size(200, 20);
this.richTextBoxMessage.TabIndex = 0;
this.richTextBoxMessage.TabStop = false;
this.richTextBoxMessage.Text = "<Message>";
this.richTextBoxMessage.LinkClicked += new System.Windows.Forms.LinkClickedEventHandler(this.richTextBoxMessage_LinkClicked);
//
// panel1
//
this.panel1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.panel1.BackColor = System.Drawing.Color.White;
this.panel1.Controls.Add(this.pictureBoxForIcon);
this.panel1.Controls.Add(this.richTextBoxMessage);
this.panel1.Location = new System.Drawing.Point(-3, -4);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(268, 59);
this.panel1.TabIndex = 1;
//
// pictureBoxForIcon
//
this.pictureBoxForIcon.BackColor = System.Drawing.Color.Transparent;
this.pictureBoxForIcon.Location = new System.Drawing.Point(15, 19);
this.pictureBoxForIcon.Name = "pictureBoxForIcon";
this.pictureBoxForIcon.Size = new System.Drawing.Size(32, 32);
this.pictureBoxForIcon.TabIndex = 8;
this.pictureBoxForIcon.TabStop = false;
//
// button2
//
this.button2.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.button2.AutoSize = true;
this.button2.DialogResult = System.Windows.Forms.DialogResult.OK;
this.button2.Location = new System.Drawing.Point(92, 67);
this.button2.MinimumSize = new System.Drawing.Size(0, 24);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 24);
this.button2.TabIndex = 3;
this.button2.Text = "OK";
this.button2.FlatStyle = System.Windows.Forms.FlatStyle.System;
this.button2.UseVisualStyleBackColor = true;
this.button2.Visible = false;
//
// button3
//
this.button3.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.button3.AutoSize = true;
this.button3.DialogResult = System.Windows.Forms.DialogResult.OK;
this.button3.Location = new System.Drawing.Point(173, 67);
this.button3.MinimumSize = new System.Drawing.Size(0, 24);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(75, 24);
this.button3.TabIndex = 0;
this.button3.Text = "OK";
this.button3.FlatStyle = System.Windows.Forms.FlatStyle.System;
this.button3.UseVisualStyleBackColor = true;
this.button3.Visible = false;
//
// FlexibleMessageBoxForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(96F, 96F);
this.ClientSize = new System.Drawing.Size(260, 102);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.panel1);
this.Controls.Add(this.button1);
this.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.FlexibleMessageBoxFormBindingSource, "CaptionText", true));
this.MaximizeBox = false;
this.MinimizeBox = false;
this.MinimumSize = new System.Drawing.Size(276, 140);
this.Name = "FlexibleMessageBoxForm";
this.ShowIcon = false;
this.ShowInTaskbar = false;
this.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Auto;
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
this.Text = "<Caption>";
((System.ComponentModel.ISupportInitialize)(this.FlexibleMessageBoxFormBindingSource)).EndInit();
this.panel1.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.pictureBoxForIcon)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
}
private System.Windows.Forms.Button button1;
private System.Windows.Forms.BindingSource FlexibleMessageBoxFormBindingSource;
private System.Windows.Forms.RichTextBox richTextBoxMessage;
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.PictureBox pictureBoxForIcon;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
#endregion
#region Private constants
//These separators are used for the "copy to clipboard" standard operation, triggered by Ctrl + C (behavior and clipboard format is like in a standard MessageBox)
private const string STANDARD_MESSAGEBOX_SEPARATOR_LINES = "---------------------------\n";
private const string STANDARD_MESSAGEBOX_SEPARATOR_SPACES = " ";
private const int ButtonMargin = 12;
//These are the possible buttons (in a standard MessageBox)
private enum ButtonID { OK = 0, CANCEL, YES, NO, ABORT, RETRY, IGNORE };
//These are the buttons texts for different languages.
//If you want to add a new language, add it here and in the GetButtonText-Function
private enum TwoLetterISOLanguageID { en, de, es, it, ru };
private static readonly string[] BUTTON_TEXTS_ENGLISH_EN = { "OK", "Cancel", "&Yes", "&No", "&Abort", "&Retry", "&Ignore" }; //Note: This is also the fallback language
private static readonly string[] BUTTON_TEXTS_GERMAN_DE = { "OK", "Abbrechen", "&Ja", "&Nein", "&Abbrechen", "&Wiederholen", "&Ignorieren" };
private static readonly string[] BUTTON_TEXTS_SPANISH_ES = { "Aceptar", "Cancelar", "&Sí", "&No", "&Abortar", "&Reintentar", "&Ignorar" };
private static readonly string[] BUTTON_TEXTS_ITALIAN_IT = { "OK", "Annulla", "&Sì", "&No", "&Interrompi", "&Riprova", "&Ignora" };
private static readonly string[] BUTTON_TEXTS_RUSSIAN_RU = { "OK", "Отмена", "Да", "Нет", "Прервать", "Повтор", "Пропустить" };
#endregion
#region Private members
private MessageBoxDefaultButton defaultButton;
private int visibleButtonsCount;
private readonly TwoLetterISOLanguageID languageID = TwoLetterISOLanguageID.en;
#endregion
#region Private constructor
/// <summary>
/// Initializes a new instance of the <see cref="FlexibleMessageBoxForm"/> class.
/// </summary>
private FlexibleMessageBoxForm()
{
InitializeComponent();
//Try to evaluate the language. If this fails, the fallback language English will be used
Enum.TryParse<TwoLetterISOLanguageID>(CultureInfo.CurrentUICulture.TwoLetterISOLanguageName, out this.languageID);
this.KeyPreview = true;
this.KeyUp += FlexibleMessageBoxForm_KeyUp;
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
this.UpdateTheme();
}
#endregion
#region Private helper functions
/// <summary>
/// Gets the string rows.
/// </summary>
/// <param name="message">The message.</param>
/// <returns>The string rows as 1-dimensional array</returns>
private static string[] GetStringRows(string message)
{
if (string.IsNullOrEmpty(message)) { return null; }
// Added "Replace" in next line to fix bug where dialog box would be too narrow if text contained \t character - BoltBait
var messageRows = message.Replace("\t"," ").Split(new char[] { '\n' }, StringSplitOptions.None);
return messageRows;
}
/// <summary>
/// Gets the button text for the CurrentUICulture language.
/// Note: The fallback language is English
/// </summary>
/// <param name="buttonID">The ID of the button.</param>
/// <returns>The button text</returns>
private string GetButtonText(ButtonID buttonID)
{
var buttonTextArrayIndex = Convert.ToInt32(buttonID);
switch (this.languageID)
{
case TwoLetterISOLanguageID.de: return BUTTON_TEXTS_GERMAN_DE[buttonTextArrayIndex];
case TwoLetterISOLanguageID.es: return BUTTON_TEXTS_SPANISH_ES[buttonTextArrayIndex];
case TwoLetterISOLanguageID.it: return BUTTON_TEXTS_ITALIAN_IT[buttonTextArrayIndex];
case TwoLetterISOLanguageID.ru: return BUTTON_TEXTS_RUSSIAN_RU[buttonTextArrayIndex];
default: return BUTTON_TEXTS_ENGLISH_EN[buttonTextArrayIndex];
}
}
/// <summary>
/// Ensure the given working area factor in the range of 0.2 - 1.0 where:
///
/// 0.2 means: 20 percent of the working area height or width.
/// 1.0 means: 100 percent of the working area height or width.
/// </summary>
/// <param name="workingAreaFactor">The given working area factor.</param>
/// <returns>The corrected given working area factor.</returns>
private static double GetCorrectedWorkingAreaFactor(double workingAreaFactor)
{
const double MIN_FACTOR = 0.2;
const double MAX_FACTOR = 1.0;
if (workingAreaFactor < MIN_FACTOR) { return MIN_FACTOR; }
if (workingAreaFactor > MAX_FACTOR) { return MAX_FACTOR; }
return workingAreaFactor;
}
/// <summary>
/// Set the dialogs start position when given.
/// Otherwise center the dialog on the current screen.
/// </summary>
/// <param name="flexibleMessageBoxForm">The FlexibleMessageBox dialog.</param>
/// <param name="owner">The owner.</param>
private static void SetDialogStartPosition(FlexibleMessageBoxForm flexibleMessageBoxForm, IWin32Window owner)
{
//If no owner given: Center on current screen
if (owner == null)
{
var screen = Screen.FromPoint(Cursor.Position);
flexibleMessageBoxForm.StartPosition = FormStartPosition.Manual;
flexibleMessageBoxForm.Left = screen.Bounds.Left + screen.Bounds.Width / 2 - flexibleMessageBoxForm.Width / 2;
flexibleMessageBoxForm.Top = screen.Bounds.Top + screen.Bounds.Height / 2 - flexibleMessageBoxForm.Height / 2;
}
}
/// <summary>
/// Calculate the dialogs start size (Try to auto-size width to show longest text row).
/// Also set the maximum dialog size.
/// </summary>
/// <param name="flexibleMessageBoxForm">The FlexibleMessageBox dialog.</param>
/// <param name="text">The text (the longest text row is used to calculate the dialog width).</param>
/// <param name="text">The caption (this can also affect the dialog width).</param>
private static void SetDialogSizes(FlexibleMessageBoxForm flexibleMessageBoxForm, string text, string caption)
{
//First set the bounds for the maximum dialog size
flexibleMessageBoxForm.MaximumSize = new Size(Convert.ToInt32(SystemInformation.WorkingArea.Width * FlexibleMessageBoxForm.GetCorrectedWorkingAreaFactor(MAX_WIDTH_FACTOR)),
Convert.ToInt32(SystemInformation.WorkingArea.Height * FlexibleMessageBoxForm.GetCorrectedWorkingAreaFactor(MAX_HEIGHT_FACTOR)));
//Get rows. Exit if there are no rows to render...
var stringRows = GetStringRows(text);
if (stringRows == null) { return; }
//Calculate whole text height
var textHeight = TextRenderer.MeasureText(text, FONT).Height;
//Calculate width for longest text line
const int SCROLLBAR_WIDTH_OFFSET = 15;
var longestTextRowWidth = stringRows.Max(textForRow => TextRenderer.MeasureText(textForRow, FONT).Width);
var captionWidth = TextRenderer.MeasureText(caption, SystemFonts.CaptionFont).Width;
var textWidth = Math.Max(longestTextRowWidth + SCROLLBAR_WIDTH_OFFSET, captionWidth);
//Calculate margins
var marginWidth = flexibleMessageBoxForm.Width - flexibleMessageBoxForm.richTextBoxMessage.Width;
var marginHeight = flexibleMessageBoxForm.Height - flexibleMessageBoxForm.richTextBoxMessage.Height;
//Set calculated dialog size (if the calculated values exceed the maximums, they were cut by windows forms automatically)
flexibleMessageBoxForm.Size = new Size(textWidth + marginWidth,
textHeight + marginHeight);
}
/// <summary>
/// Set the dialogs icon.
/// When no icon is used: Correct placement and width of rich text box.
/// </summary>
/// <param name="flexibleMessageBoxForm">The FlexibleMessageBox dialog.</param>
/// <param name="icon">The MessageBoxIcon.</param>
private static void SetDialogIcon(FlexibleMessageBoxForm flexibleMessageBoxForm, MessageBoxIcon icon)
{
if (icon != MessageBoxIcon.None)
{
StockIconId iconId = icon switch
{
MessageBoxIcon.Information => StockIconId.Info,
MessageBoxIcon.Warning => StockIconId.Warning,
MessageBoxIcon.Error => StockIconId.Error,
MessageBoxIcon.Question => StockIconId.Help
};
using Icon stockIcon = SystemIcons.GetStockIcon(iconId);
flexibleMessageBoxForm.pictureBoxForIcon.Image = stockIcon.ToBitmap();
}
else
{
//When no icon is used: Correct placement and width of rich text box.
flexibleMessageBoxForm.pictureBoxForIcon.Visible = false;
flexibleMessageBoxForm.richTextBoxMessage.Left -= flexibleMessageBoxForm.pictureBoxForIcon.Width;
flexibleMessageBoxForm.richTextBoxMessage.Width += flexibleMessageBoxForm.pictureBoxForIcon.Width;
}
}
// Added ability to have custom text on the buttons - BoltBait
private static void SetCustomDialogButtons(FlexibleMessageBoxForm flexibleMessageBoxForm, string[] buttons, MessageBoxDefaultButton defaultButton)
{
if (buttons.Length > 3)
{
throw new Exception("Maximum of 3 buttons can be defined.");
}
if (buttons.Length < 1)
{
SetDialogButtons(flexibleMessageBoxForm, MessageBoxButtons.OK, MessageBoxDefaultButton.Button1);
return;
}
switch (buttons.Length)
{
case 1: // returns 1 - Pressing ESC returns 1
flexibleMessageBoxForm.visibleButtonsCount = 1;
flexibleMessageBoxForm.button3.Visible = true;
flexibleMessageBoxForm.button3.Text = buttons[0];
flexibleMessageBoxForm.button3.DialogResult = DialogResult.OK;
flexibleMessageBoxForm.CancelButton = flexibleMessageBoxForm.button3;
break;
case 2: // returns 1 or 2 - Pressing ESC returns 2
flexibleMessageBoxForm.button3.Visible = true;
flexibleMessageBoxForm.button3.Text = buttons[1];
flexibleMessageBoxForm.button3.DialogResult = DialogResult.Cancel;
flexibleMessageBoxForm.visibleButtonsCount = 2;
flexibleMessageBoxForm.button2.Visible = true;
flexibleMessageBoxForm.button2.Text = buttons[0];
flexibleMessageBoxForm.button2.DialogResult = DialogResult.OK;
flexibleMessageBoxForm.button2.Location = new Point(
flexibleMessageBoxForm.button3.Location.X - ButtonMargin - flexibleMessageBoxForm.button2.Bounds.Width,
flexibleMessageBoxForm.button2.Location.Y);
flexibleMessageBoxForm.CancelButton = flexibleMessageBoxForm.button3;
break;
case 3: // returns 1 or 2 or 3 - Pressing ESC returns 3
flexibleMessageBoxForm.visibleButtonsCount = 3;
flexibleMessageBoxForm.button1.Visible = true;
flexibleMessageBoxForm.button1.Text = buttons[0];
flexibleMessageBoxForm.button1.DialogResult = DialogResult.OK;
flexibleMessageBoxForm.button2.Visible = true;
flexibleMessageBoxForm.button2.Text = buttons[1];
flexibleMessageBoxForm.button2.DialogResult = DialogResult.Cancel;
flexibleMessageBoxForm.button3.Visible = true;
flexibleMessageBoxForm.button3.Text = buttons[2];
flexibleMessageBoxForm.button3.DialogResult = DialogResult.Abort;
flexibleMessageBoxForm.CancelButton = flexibleMessageBoxForm.button3;
break;
}
flexibleMessageBoxForm.ControlBox = false;
//Set default button (used in FlexibleMessageBoxForm_Shown)
flexibleMessageBoxForm.defaultButton = defaultButton;
}
/// <summary>
/// Set dialog buttons visibilities and texts.
/// Also set a default button.
/// </summary>
/// <param name="flexibleMessageBoxForm">The FlexibleMessageBox dialog.</param>
/// <param name="buttons">The buttons.</param>
/// <param name="defaultButton">The default button.</param>
private static void SetDialogButtons(FlexibleMessageBoxForm flexibleMessageBoxForm, MessageBoxButtons buttons, MessageBoxDefaultButton defaultButton)
{
//Set the buttons visibilities and texts
switch (buttons)
{
case MessageBoxButtons.AbortRetryIgnore:
flexibleMessageBoxForm.visibleButtonsCount = 3;
flexibleMessageBoxForm.button1.Visible = true;
flexibleMessageBoxForm.button1.Text = flexibleMessageBoxForm.GetButtonText(ButtonID.ABORT);
flexibleMessageBoxForm.button1.DialogResult = DialogResult.Abort;
flexibleMessageBoxForm.button2.Visible = true;
flexibleMessageBoxForm.button2.Text = flexibleMessageBoxForm.GetButtonText(ButtonID.RETRY);
flexibleMessageBoxForm.button2.DialogResult = DialogResult.Retry;
flexibleMessageBoxForm.button3.Visible = true;
flexibleMessageBoxForm.button3.Text = flexibleMessageBoxForm.GetButtonText(ButtonID.IGNORE);
flexibleMessageBoxForm.button3.DialogResult = DialogResult.Ignore;
flexibleMessageBoxForm.ControlBox = false;
break;
case MessageBoxButtons.OKCancel:
flexibleMessageBoxForm.visibleButtonsCount = 2;
flexibleMessageBoxForm.button2.Visible = true;
flexibleMessageBoxForm.button2.Text = flexibleMessageBoxForm.GetButtonText(ButtonID.OK);
flexibleMessageBoxForm.button2.DialogResult = DialogResult.OK;
flexibleMessageBoxForm.button3.Visible = true;
flexibleMessageBoxForm.button3.Text = flexibleMessageBoxForm.GetButtonText(ButtonID.CANCEL);
flexibleMessageBoxForm.button3.DialogResult = DialogResult.Cancel;
flexibleMessageBoxForm.CancelButton = flexibleMessageBoxForm.button3;
break;
case MessageBoxButtons.RetryCancel:
flexibleMessageBoxForm.visibleButtonsCount = 2;
flexibleMessageBoxForm.button2.Visible = true;
flexibleMessageBoxForm.button2.Text = flexibleMessageBoxForm.GetButtonText(ButtonID.RETRY);
flexibleMessageBoxForm.button2.DialogResult = DialogResult.Retry;
flexibleMessageBoxForm.button3.Visible = true;
flexibleMessageBoxForm.button3.Text = flexibleMessageBoxForm.GetButtonText(ButtonID.CANCEL);
flexibleMessageBoxForm.button3.DialogResult = DialogResult.Cancel;
flexibleMessageBoxForm.CancelButton = flexibleMessageBoxForm.button3;
break;
case MessageBoxButtons.YesNo:
flexibleMessageBoxForm.visibleButtonsCount = 2;
flexibleMessageBoxForm.button2.Visible = true;
flexibleMessageBoxForm.button2.Text = flexibleMessageBoxForm.GetButtonText(ButtonID.YES);
flexibleMessageBoxForm.button2.DialogResult = DialogResult.Yes;
flexibleMessageBoxForm.button3.Visible = true;
flexibleMessageBoxForm.button3.Text = flexibleMessageBoxForm.GetButtonText(ButtonID.NO);
flexibleMessageBoxForm.button3.DialogResult = DialogResult.No;
flexibleMessageBoxForm.ControlBox = false;
break;
case MessageBoxButtons.YesNoCancel:
flexibleMessageBoxForm.visibleButtonsCount = 3;
flexibleMessageBoxForm.button1.Visible = true;
flexibleMessageBoxForm.button1.Text = flexibleMessageBoxForm.GetButtonText(ButtonID.YES);
flexibleMessageBoxForm.button1.DialogResult = DialogResult.Yes;
flexibleMessageBoxForm.button2.Visible = true;
flexibleMessageBoxForm.button2.Text = flexibleMessageBoxForm.GetButtonText(ButtonID.NO);
flexibleMessageBoxForm.button2.DialogResult = DialogResult.No;
flexibleMessageBoxForm.button3.Visible = true;
flexibleMessageBoxForm.button3.Text = flexibleMessageBoxForm.GetButtonText(ButtonID.CANCEL);
flexibleMessageBoxForm.button3.DialogResult = DialogResult.Cancel;
flexibleMessageBoxForm.CancelButton = flexibleMessageBoxForm.button3;
break;
case MessageBoxButtons.OK:
default:
flexibleMessageBoxForm.visibleButtonsCount = 1;
flexibleMessageBoxForm.button3.Visible = true;
flexibleMessageBoxForm.button3.Text = flexibleMessageBoxForm.GetButtonText(ButtonID.OK);
flexibleMessageBoxForm.button3.DialogResult = DialogResult.OK;
flexibleMessageBoxForm.CancelButton = flexibleMessageBoxForm.button3;
break;
}
//Set default button (used in FlexibleMessageBoxForm_Shown)
flexibleMessageBoxForm.defaultButton = defaultButton;
}
#endregion
#region Private event handlers
/// <summary>
/// Handles the Shown event of the FlexibleMessageBoxForm control.
/// </summary>
/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
protected override void OnShown(EventArgs e)
{
base.OnShown(e);
int buttonIndexToFocus = 1;
Button buttonToFocus;
//Set the default button...
switch (this.defaultButton)
{
case MessageBoxDefaultButton.Button1:
default:
buttonIndexToFocus = 1;
break;
case MessageBoxDefaultButton.Button2:
buttonIndexToFocus = 2;
break;
case MessageBoxDefaultButton.Button3:
buttonIndexToFocus = 3;
break;
}
if (buttonIndexToFocus > this.visibleButtonsCount) { buttonIndexToFocus = this.visibleButtonsCount; }
if (buttonIndexToFocus == 3)
{
buttonToFocus = this.button3;
}
else if (buttonIndexToFocus == 2)
{
buttonToFocus = this.button2;
}
else
{
buttonToFocus = this.button1;
}
buttonToFocus.Focus();
}
/// <summary>
/// Handles the LinkClicked event of the richTextBoxMessage control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.Windows.Forms.LinkClickedEventArgs"/> instance containing the event data.</param>
private void richTextBoxMessage_LinkClicked(object sender, LinkClickedEventArgs e)
{
try
{
Cursor.Current = Cursors.WaitCursor;
UIUtil.LaunchUrl(this, e.LinkText);
}
catch (Exception)
{
//Let the caller of FlexibleMessageBoxForm decide what to do with this exception...
throw;
}
finally
{
Cursor.Current = Cursors.Default;
}
}
/// <summary>
/// Handles the KeyUp event of the richTextBoxMessage control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.Windows.Forms.KeyEventArgs"/> instance containing the event data.</param>
private void FlexibleMessageBoxForm_KeyUp(object sender, KeyEventArgs e)
{
//Handle standard key strikes for clipboard copy: "Ctrl + C" and "Ctrl + Insert"
if (e.Control && (e.KeyCode == Keys.C || e.KeyCode == Keys.Insert))
{
var buttonsTextLine = (this.button1.Visible ? this.button1.Text + STANDARD_MESSAGEBOX_SEPARATOR_SPACES : string.Empty)
+ (this.button2.Visible ? this.button2.Text + STANDARD_MESSAGEBOX_SEPARATOR_SPACES : string.Empty)
+ (this.button3.Visible ? this.button3.Text + STANDARD_MESSAGEBOX_SEPARATOR_SPACES : string.Empty);
//Build same clipboard text like the standard .Net MessageBox
var textForClipboard = STANDARD_MESSAGEBOX_SEPARATOR_LINES
+ this.Text + Environment.NewLine
+ STANDARD_MESSAGEBOX_SEPARATOR_LINES
+ this.richTextBoxMessage.Text + Environment.NewLine
+ STANDARD_MESSAGEBOX_SEPARATOR_LINES
+ buttonsTextLine.Replace("&", string.Empty) + Environment.NewLine
+ STANDARD_MESSAGEBOX_SEPARATOR_LINES;
//Set text in clipboard
System.Windows.Forms.Clipboard.SetText(textForClipboard);
}
}
#endregion
#region Properties (only used for binding)
/// <summary>
/// The text that is been used for the heading.
/// </summary>
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public string CaptionText { get; set; }
/// <summary>
/// The text that is been used in the FlexibleMessageBoxForm.
/// </summary>
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public string MessageText { get; set; }
#endregion
#region Public show function
/// <summary>
/// Shows the specified message box.
/// </summary>
/// <param name="owner">The owner.</param>
/// <param name="text">The text.</param>
/// <param name="caption">The caption.</param>
/// <param name="buttons">The buttons.</param>
/// <param name="icon">The icon.</param>
/// <param name="defaultButton">The default button.</param>
/// <returns>The dialog result.</returns>
public static DialogResult Show(IWin32Window owner, string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton)
{
//Create a new instance of the FlexibleMessageBox form
var flexibleMessageBoxForm = new FlexibleMessageBoxForm();
//Bind the caption and the message text
flexibleMessageBoxForm.CaptionText = caption;
flexibleMessageBoxForm.MessageText = text;
flexibleMessageBoxForm.FlexibleMessageBoxFormBindingSource.DataSource = flexibleMessageBoxForm;
//Set the buttons visibilities and texts. Also set a default button.
SetDialogButtons(flexibleMessageBoxForm, buttons, defaultButton);
//Set the dialogs icon. When no icon is used: Correct placement and width of rich text box.
SetDialogIcon(flexibleMessageBoxForm, icon);
//Set the font for all controls
flexibleMessageBoxForm.Font = FONT;
flexibleMessageBoxForm.richTextBoxMessage.Font = FONT;
//Calculate the dialogs start size (Try to auto-size width to show longest text row). Also set the maximum dialog size.
SetDialogSizes(flexibleMessageBoxForm, text, caption);
//Set the dialogs start position when given. Otherwise center the dialog on the current screen.
SetDialogStartPosition(flexibleMessageBoxForm, owner);
//Show the dialog
return flexibleMessageBoxForm.ShowDialog(owner);
}
// Added ability to have custom button text - BoltBait
public static DialogResult Show(IWin32Window owner, string text, string caption, string[] buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton)
{
//Create a new instance of the FlexibleMessageBox form
var flexibleMessageBoxForm = new FlexibleMessageBoxForm();
//Bind the caption and the message text
flexibleMessageBoxForm.CaptionText = caption;
flexibleMessageBoxForm.MessageText = text;
flexibleMessageBoxForm.FlexibleMessageBoxFormBindingSource.DataSource = flexibleMessageBoxForm;
//Set the buttons visibilities and texts. Also set a default button.
SetCustomDialogButtons(flexibleMessageBoxForm, buttons, defaultButton);
//Set the dialogs icon. When no icon is used: Correct placement and width of rich text box.
SetDialogIcon(flexibleMessageBoxForm, icon);
//Set the font for all controls
flexibleMessageBoxForm.Font = FONT;
flexibleMessageBoxForm.richTextBoxMessage.Font = FONT;
//Calculate the dialogs start size (Try to auto-size width to show longest text row). Also set the maximum dialog size.
SetDialogSizes(flexibleMessageBoxForm, text, caption);
//Set the dialogs start position when given. Otherwise center the dialog on the current screen.
SetDialogStartPosition(flexibleMessageBoxForm, owner);
//Show the dialog
return flexibleMessageBoxForm.ShowDialog(owner);
}
#endregion
} //class FlexibleMessageBoxForm
#endregion
}
}