MessageBoxes.cs 9.5 KB

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