MessageBoxes.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. using NStack;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. using Terminal.Gui;
  6. namespace UICatalog {
  7. [ScenarioMetadata (Name: "MessageBoxes", Description: "Demonstrates how to use MessageBoxes")]
  8. [ScenarioCategory ("Controls")]
  9. [ScenarioCategory ("Dialogs")]
  10. 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 = 10
  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. ColorScheme = Colors.Dialog,
  88. };
  89. frame.Add (messageEdit);
  90. label = new Label ("Num Buttons:") {
  91. X = 0,
  92. Y = Pos.Bottom (messageEdit),
  93. Width = Dim.Width (label),
  94. Height = 1,
  95. TextAlignment = Terminal.Gui.TextAlignment.Right,
  96. };
  97. frame.Add (label);
  98. var numButtonsEdit = new TextField ("3") {
  99. X = Pos.Right (label) + 1,
  100. Y = Pos.Top (label),
  101. Width = 5,
  102. Height = 1
  103. };
  104. frame.Add (numButtonsEdit);
  105. label = new Label ("Style:") {
  106. X = 0,
  107. Y = Pos.Bottom (label),
  108. Width = Dim.Width (label),
  109. Height = 1,
  110. TextAlignment = Terminal.Gui.TextAlignment.Right,
  111. };
  112. frame.Add (label);
  113. var styleRadioGroup = new RadioGroup (new ustring [] { "_Query", "_Error" } ) {
  114. X = Pos.Right (label) + 1,
  115. Y = Pos.Top (label),
  116. };
  117. frame.Add (styleRadioGroup);
  118. void Top_Loaded ()
  119. {
  120. frame.Height = Dim.Height (widthEdit) + Dim.Height (heightEdit) + Dim.Height (titleEdit) + Dim.Height (messageEdit)
  121. + Dim.Height (numButtonsEdit) + Dim.Height (styleRadioGroup) + 2;
  122. Top.Loaded -= Top_Loaded;
  123. }
  124. Top.Loaded += Top_Loaded;
  125. label = new Label ("Button Pressed:") {
  126. X = Pos.Center (),
  127. Y = Pos.Bottom (frame) + 4,
  128. Height = 1,
  129. TextAlignment = Terminal.Gui.TextAlignment.Right,
  130. };
  131. Win.Add (label);
  132. var buttonPressedLabel = new Label (" ") {
  133. X = Pos.Center (),
  134. Y = Pos.Bottom (frame) + 5,
  135. Width = 25,
  136. Height = 1,
  137. ColorScheme = Colors.Error,
  138. };
  139. //var btnText = new [] { "_Zero", "_One", "T_wo", "_Three", "_Four", "Fi_ve", "Si_x", "_Seven", "_Eight", "_Nine" };
  140. var showMessageBoxButton = new Button ("Show MessageBox") {
  141. X = Pos.Center(),
  142. Y = Pos.Bottom (frame) + 2,
  143. IsDefault = true,
  144. };
  145. showMessageBoxButton.Clicked += () => {
  146. try {
  147. int width = int.Parse (widthEdit.Text.ToString ());
  148. int height = int.Parse (heightEdit.Text.ToString ());
  149. int numButtons = int.Parse (numButtonsEdit.Text.ToString ());
  150. var btns = new List<ustring> ();
  151. for (int i = 0; i < numButtons; i++) {
  152. //btns.Add(btnText[i % 10]);
  153. btns.Add (NumberToWords.Convert (i));
  154. }
  155. if (styleRadioGroup.SelectedItem == 0) {
  156. buttonPressedLabel.Text = $"{MessageBox.Query (width, height, titleEdit.Text.ToString (), messageEdit.Text.ToString (), btns.ToArray ())}";
  157. } else {
  158. buttonPressedLabel.Text = $"{MessageBox.ErrorQuery (width, height, titleEdit.Text.ToString (), messageEdit.Text.ToString (), btns.ToArray ())}";
  159. }
  160. } catch (FormatException) {
  161. buttonPressedLabel.Text = "Invalid Options";
  162. }
  163. };
  164. Win.Add (showMessageBoxButton);
  165. Win.Add (buttonPressedLabel);
  166. }
  167. }
  168. }