MessageBoxes.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using Terminal.Gui;
  5. namespace UICatalog {
  6. [ScenarioMetadata (Name: "MessageBoxes", Description: "Demonstrates how to use MessageBoxes")]
  7. [ScenarioCategory ("Controls")]
  8. [ScenarioCategory ("Dialogs")]
  9. class MessageBoxes : Scenario {
  10. public override void Setup ()
  11. {
  12. var frame = new FrameView ("MessageBox Options") {
  13. X = Pos.Center(),
  14. Y = 1,
  15. Width = Dim.Percent(75),
  16. Height = 10
  17. };
  18. Win.Add (frame);
  19. var label = new Label ("Width:") {
  20. X = 0,
  21. Y = 0,
  22. Width = 15,
  23. Height = 1,
  24. TextAlignment = Terminal.Gui.TextAlignment.Right,
  25. };
  26. frame.Add (label);
  27. var widthEdit = new TextField ("50") {
  28. X = Pos.Right (label) + 1,
  29. Y = Pos.Top (label),
  30. Width = 5,
  31. Height = 1
  32. };
  33. frame.Add (widthEdit);
  34. label = new Label ("Height:") {
  35. X = 0,
  36. Y = Pos.Bottom (label),
  37. Width = Dim.Width(label),
  38. Height = 1,
  39. TextAlignment = Terminal.Gui.TextAlignment.Right,
  40. };
  41. frame.Add (label);
  42. var heightEdit = new TextField ("6") {
  43. X = Pos.Right (label) + 1,
  44. Y = Pos.Top (label),
  45. Width = 5,
  46. Height = 1
  47. };
  48. frame.Add (heightEdit);
  49. label = new Label ("Title:") {
  50. X = 0,
  51. Y = Pos.Bottom (label),
  52. Width = Dim.Width (label),
  53. Height = 1,
  54. TextAlignment = Terminal.Gui.TextAlignment.Right,
  55. };
  56. frame.Add (label);
  57. var titleEdit = new TextField ("Title") {
  58. X = Pos.Right (label) + 1,
  59. Y = Pos.Top (label),
  60. Width = Dim.Fill(),
  61. Height = 1
  62. };
  63. frame.Add (titleEdit);
  64. label = new Label ("Message:") {
  65. X = 0,
  66. Y = Pos.Bottom (label),
  67. Width = Dim.Width (label),
  68. Height = 1,
  69. TextAlignment = Terminal.Gui.TextAlignment.Right,
  70. };
  71. frame.Add (label);
  72. var messageEdit = new TextField ("Message") {
  73. X = Pos.Right (label) + 1,
  74. Y = Pos.Top (label),
  75. Width = Dim.Fill (),
  76. Height = 1
  77. };
  78. frame.Add (messageEdit);
  79. label = new Label ("Num Buttons:") {
  80. X = 0,
  81. Y = Pos.Bottom (label),
  82. Width = Dim.Width (label),
  83. Height = 1,
  84. TextAlignment = Terminal.Gui.TextAlignment.Right,
  85. };
  86. frame.Add (label);
  87. var numButtonsEdit = new TextField ("3") {
  88. X = Pos.Right (label) + 1,
  89. Y = Pos.Top (label),
  90. Width = 5,
  91. Height = 1
  92. };
  93. frame.Add (numButtonsEdit);
  94. label = new Label ("Style:") {
  95. X = 0,
  96. Y = Pos.Bottom (label),
  97. Width = Dim.Width (label),
  98. Height = 1,
  99. TextAlignment = Terminal.Gui.TextAlignment.Right,
  100. };
  101. frame.Add (label);
  102. var styleRadioGroup = new RadioGroup (new [] { "_Query", "_Error" } ) {
  103. X = Pos.Right (label) + 1,
  104. Y = Pos.Top (label),
  105. };
  106. frame.Add (styleRadioGroup);
  107. frame.Height = Dim.Height (widthEdit) + Dim.Height (heightEdit) + Dim.Height (titleEdit) + Dim.Height (messageEdit)
  108. + Dim.Height(numButtonsEdit) + Dim.Height (styleRadioGroup) + 2;
  109. label = new Label ("Button Pressed:") {
  110. X = Pos.Center (),
  111. Y = Pos.Bottom (frame) + 2,
  112. Height = 1,
  113. TextAlignment = Terminal.Gui.TextAlignment.Right,
  114. };
  115. Win.Add (label);
  116. var buttonPressedLabel = new Label ("") {
  117. X = Pos.Center (),
  118. Y = Pos.Bottom (frame) + 4,
  119. Width = 25,
  120. Height = 1,
  121. ColorScheme = Colors.Error,
  122. };
  123. var showMessageBoxButton = new Button ("Show MessageBox") {
  124. X = Pos.Center(),
  125. Y = Pos.Bottom (frame) + 2 ,
  126. IsDefault = true,
  127. Clicked = () => {
  128. try {
  129. int width = int.Parse (widthEdit.Text.ToString ());
  130. int height = int.Parse (heightEdit.Text.ToString ());
  131. int numButtons = int.Parse (numButtonsEdit.Text.ToString ());
  132. var btns = new List<string> ();
  133. for (int i = 0; i < numButtons; i++) {
  134. btns.Add($"Btn {i}");
  135. }
  136. if (styleRadioGroup.Selected == 0) {
  137. buttonPressedLabel.Text = $"{MessageBox.Query (width, height, titleEdit.Text.ToString (), messageEdit.Text.ToString (), btns.ToArray ())}";
  138. } else {
  139. buttonPressedLabel.Text = $"{MessageBox.ErrorQuery (width, height, titleEdit.Text.ToString (), messageEdit.Text.ToString (), btns.ToArray ())}";
  140. }
  141. } catch {
  142. buttonPressedLabel.Text = "Invalid Options";
  143. }
  144. },
  145. };
  146. Win.Add (showMessageBoxButton);
  147. Win.Add (buttonPressedLabel);
  148. }
  149. }
  150. }