MessageBoxes.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. using NStack;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. using Terminal.Gui;
  6. namespace UICatalog.Scenarios {
  7. [ScenarioMetadata (Name: "MessageBoxes", Description: "Demonstrates how to use the MessageBox class.")]
  8. [ScenarioCategory ("Controls")]
  9. [ScenarioCategory ("Dialogs")]
  10. public class MessageBoxes : Scenario {
  11. public override void Setup ()
  12. {
  13. var frame = new FrameView ("MessageBox Options") {
  14. X = Pos.Center (),
  15. Y = 1,
  16. Width = Dim.Percent (75),
  17. Height = 12
  18. };
  19. Win.Add (frame);
  20. var label = new Label ("Width:") {
  21. X = 0,
  22. Y = 0,
  23. Width = 15,
  24. Height = 1,
  25. TextAlignment = Terminal.Gui.TextAlignment.Right,
  26. };
  27. frame.Add (label);
  28. var widthEdit = new TextField ("0") {
  29. X = Pos.Right (label) + 1,
  30. Y = Pos.Top (label),
  31. Width = 5,
  32. Height = 1
  33. };
  34. frame.Add (widthEdit);
  35. label = new Label ("Height:") {
  36. X = 0,
  37. Y = Pos.Bottom (label),
  38. Width = Dim.Width (label),
  39. Height = 1,
  40. TextAlignment = Terminal.Gui.TextAlignment.Right,
  41. };
  42. frame.Add (label);
  43. var heightEdit = new TextField ("0") {
  44. X = Pos.Right (label) + 1,
  45. Y = Pos.Top (label),
  46. Width = 5,
  47. Height = 1
  48. };
  49. frame.Add (heightEdit);
  50. frame.Add (new Label ("If height & width are both 0,") {
  51. X = Pos.Right (widthEdit) + 2,
  52. Y = Pos.Top (widthEdit),
  53. });
  54. frame.Add (new Label ("the MessageBox will be sized automatically.") {
  55. X = Pos.Right (heightEdit) + 2,
  56. Y = Pos.Top (heightEdit),
  57. });
  58. label = new Label ("Title:") {
  59. X = 0,
  60. Y = Pos.Bottom (label),
  61. Width = Dim.Width (label),
  62. Height = 1,
  63. TextAlignment = Terminal.Gui.TextAlignment.Right,
  64. };
  65. frame.Add (label);
  66. var titleEdit = new TextField ("Title") {
  67. X = Pos.Right (label) + 1,
  68. Y = Pos.Top (label),
  69. Width = Dim.Fill (),
  70. Height = 1
  71. };
  72. frame.Add (titleEdit);
  73. label = new Label ("Message:") {
  74. X = 0,
  75. Y = Pos.Bottom (label),
  76. Width = Dim.Width (label),
  77. Height = 1,
  78. TextAlignment = Terminal.Gui.TextAlignment.Right,
  79. };
  80. frame.Add (label);
  81. var messageEdit = new TextView () {
  82. Text = "Message",
  83. X = Pos.Right (label) + 1,
  84. Y = Pos.Top (label),
  85. Width = Dim.Fill (),
  86. Height = 5,
  87. };
  88. frame.Add (messageEdit);
  89. label = new Label ("Num Buttons:") {
  90. X = 0,
  91. Y = Pos.Bottom (messageEdit),
  92. Width = Dim.Width (label),
  93. Height = 1,
  94. TextAlignment = Terminal.Gui.TextAlignment.Right,
  95. };
  96. frame.Add (label);
  97. var numButtonsEdit = new TextField ("3") {
  98. X = Pos.Right (label) + 1,
  99. Y = Pos.Top (label),
  100. Width = 5,
  101. Height = 1
  102. };
  103. frame.Add (numButtonsEdit);
  104. label = new Label ("Default Button:") {
  105. X = 0,
  106. Y = Pos.Bottom (label),
  107. Width = Dim.Width (label),
  108. Height = 1,
  109. TextAlignment = Terminal.Gui.TextAlignment.Right,
  110. };
  111. frame.Add (label);
  112. var defaultButtonEdit = new TextField ("0") {
  113. X = Pos.Right (label) + 1,
  114. Y = Pos.Top (label),
  115. Width = 5,
  116. Height = 1
  117. };
  118. frame.Add (defaultButtonEdit);
  119. label = new Label ("Style:") {
  120. X = 0,
  121. Y = Pos.Bottom (label),
  122. Width = Dim.Width (label),
  123. Height = 1,
  124. TextAlignment = Terminal.Gui.TextAlignment.Right,
  125. };
  126. frame.Add (label);
  127. var styleRadioGroup = new RadioGroup (new ustring [] { "_Query", "_Error" }) {
  128. X = Pos.Right (label) + 1,
  129. Y = Pos.Top (label),
  130. };
  131. frame.Add (styleRadioGroup);
  132. var ckbWrapMessage = new CheckBox ("Wrap Message", true) {
  133. X = Pos.Right (label) + 1,
  134. Y = Pos.Top (label) + 3
  135. };
  136. frame.Add (ckbWrapMessage);
  137. frame.ForceValidatePosDim = true;
  138. void Top_Loaded (object sender, EventArgs args)
  139. {
  140. frame.Height =
  141. widthEdit.Frame.Height +
  142. heightEdit.Frame.Height +
  143. titleEdit.Frame.Height +
  144. messageEdit.Frame.Height +
  145. numButtonsEdit.Frame.Height +
  146. defaultButtonEdit.Frame.Height +
  147. styleRadioGroup.Frame.Height +
  148. 2 +
  149. ckbWrapMessage.Frame.Height;
  150. Application.Top.Loaded -= Top_Loaded;
  151. }
  152. //Application.Top.Loaded += Top_Loaded;
  153. label = new Label ("Button Pressed:") {
  154. X = Pos.Center (),
  155. Y = Pos.Bottom (frame) + 4,
  156. Height = 1,
  157. TextAlignment = Terminal.Gui.TextAlignment.Right,
  158. };
  159. Win.Add (label);
  160. var buttonPressedLabel = new Label (" ") {
  161. X = Pos.Center (),
  162. Y = Pos.Bottom (frame) + 5,
  163. Width = 25,
  164. Height = 1,
  165. ColorScheme = Colors.Error,
  166. TextAlignment = Terminal.Gui.TextAlignment.Centered
  167. };
  168. //var btnText = new [] { "_Zero", "_One", "T_wo", "_Three", "_Four", "Fi_ve", "Si_x", "_Seven", "_Eight", "_Nine" };
  169. var showMessageBoxButton = new Button ("Show MessageBox") {
  170. X = Pos.Center (),
  171. Y = Pos.Bottom (frame) + 2,
  172. IsDefault = true,
  173. };
  174. showMessageBoxButton.Clicked += (s,e) => {
  175. try {
  176. int width = int.Parse (widthEdit.Text.ToString ());
  177. int height = int.Parse (heightEdit.Text.ToString ());
  178. int numButtons = int.Parse (numButtonsEdit.Text.ToString ());
  179. int defaultButton = int.Parse (defaultButtonEdit.Text.ToString ());
  180. var btns = new List<ustring> ();
  181. for (int i = 0; i < numButtons; i++) {
  182. //btns.Add(btnText[i % 10]);
  183. btns.Add (NumberToWords.Convert (i));
  184. }
  185. if (styleRadioGroup.SelectedItem == 0) {
  186. buttonPressedLabel.Text = $"{MessageBox.Query (width, height, titleEdit.Text.ToString (), messageEdit.Text.ToString (), defaultButton, (bool)ckbWrapMessage.Checked, btns.ToArray ())}";
  187. } else {
  188. buttonPressedLabel.Text = $"{MessageBox.ErrorQuery (width, height, titleEdit.Text.ToString (), messageEdit.Text.ToString (), defaultButton, (bool)ckbWrapMessage.Checked, btns.ToArray ())}";
  189. }
  190. } catch (FormatException) {
  191. buttonPressedLabel.Text = "Invalid Options";
  192. }
  193. };
  194. Win.Add (showMessageBoxButton);
  195. Win.Add (buttonPressedLabel);
  196. }
  197. }
  198. }