123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275 |
- using System;
- using System.Collections.Generic;
- using Terminal.Gui;
- namespace UICatalog.Scenarios;
- [ScenarioMetadata ("MessageBoxes", "Demonstrates how to use the MessageBox class.")]
- [ScenarioCategory ("Controls")]
- [ScenarioCategory ("Dialogs")]
- public class MessageBoxes : Scenario
- {
- public override void Setup ()
- {
- var frame = new FrameView { X = Pos.Center (), Y = 1, Width = Dim.Percent (75), Title = "MessageBox Options" };
- Win.Add (frame);
- var label = new Label { X = 0, Y = 0, TextJustification = Alignment.Right, Text = "Width:" };
- frame.Add (label);
- var widthEdit = new TextField
- {
- X = Pos.Right (label) + 1,
- Y = Pos.Top (label),
- Width = 5,
- Height = 1,
- Text = "0"
- };
- frame.Add (widthEdit);
- label = new()
- {
- X = 0,
- Y = Pos.Bottom (label),
- Width = Dim.Width (label),
- Height = 1,
- TextJustification = Alignment.Right,
- Text = "Height:"
- };
- frame.Add (label);
- var heightEdit = new TextField
- {
- X = Pos.Right (label) + 1,
- Y = Pos.Top (label),
- Width = 5,
- Height = 1,
- Text = "0"
- };
- frame.Add (heightEdit);
- frame.Add (
- new Label { X = Pos.Right (widthEdit) + 2, Y = Pos.Top (widthEdit), Text = "If height & width are both 0," }
- );
- frame.Add (
- new Label
- {
- X = Pos.Right (heightEdit) + 2,
- Y = Pos.Top (heightEdit),
- Text = "the MessageBox will be sized automatically."
- }
- );
- label = new()
- {
- X = 0,
- Y = Pos.Bottom (label),
- Width = Dim.Width (label),
- Height = 1,
- TextJustification = Alignment.Right,
- Text = "Title:"
- };
- frame.Add (label);
- var titleEdit = new TextField
- {
- X = Pos.Right (label) + 1,
- Y = Pos.Top (label),
- Width = Dim.Fill (),
- Height = 1,
- Text = "Title"
- };
- frame.Add (titleEdit);
- label = new()
- {
- X = 0,
- Y = Pos.Bottom (label),
- Width = Dim.Width (label),
- Height = 1,
- TextJustification = Alignment.Right,
- Text = "Message:"
- };
- frame.Add (label);
- var messageEdit = new TextView
- {
- Text = "Message",
- X = Pos.Right (label) + 1,
- Y = Pos.Top (label),
- Width = Dim.Fill (),
- Height = 5
- };
- frame.Add (messageEdit);
- label = new()
- {
- X = 0,
- Y = Pos.Bottom (messageEdit),
- Width = Dim.Width (label),
- Height = 1,
- TextJustification = Alignment.Right,
- Text = "Num Buttons:"
- };
- frame.Add (label);
- var numButtonsEdit = new TextField
- {
- X = Pos.Right (label) + 1,
- Y = Pos.Top (label),
- Width = 5,
- Height = 1,
- Text = "3"
- };
- frame.Add (numButtonsEdit);
- label = new()
- {
- X = 0,
- Y = Pos.Bottom (label),
- Width = Dim.Width (label),
- Height = 1,
- TextJustification = Alignment.Right,
- Text = "Default Button:"
- };
- frame.Add (label);
- var defaultButtonEdit = new TextField
- {
- X = Pos.Right (label) + 1,
- Y = Pos.Top (label),
- Width = 5,
- Height = 1,
- Text = "0"
- };
- frame.Add (defaultButtonEdit);
- label = new()
- {
- X = 0,
- Y = Pos.Bottom (label),
- Width = Dim.Width (label),
- Height = 1,
- TextJustification = Alignment.Right,
- Text = "Style:"
- };
- frame.Add (label);
- var styleRadioGroup = new RadioGroup
- {
- X = Pos.Right (label) + 1, Y = Pos.Top (label), RadioLabels = new [] { "_Query", "_Error" }
- };
- frame.Add (styleRadioGroup);
- var ckbWrapMessage = new CheckBox
- {
- X = Pos.Right (label) + 1, Y = Pos.Bottom (styleRadioGroup), Text = "_Wrap Message", Checked = true
- };
- frame.Add (ckbWrapMessage);
- frame.ValidatePosDim = true;
- void Top_LayoutComplete (object sender, EventArgs args)
- {
- frame.Height =
- widthEdit.Frame.Height
- + heightEdit.Frame.Height
- + titleEdit.Frame.Height
- + messageEdit.Frame.Height
- + numButtonsEdit.Frame.Height
- + defaultButtonEdit.Frame.Height
- + styleRadioGroup.Frame.Height
- + ckbWrapMessage.Frame.Height
- + frame.GetAdornmentsThickness ().Vertical;
- Top.Loaded -= Top_LayoutComplete;
- }
- Top.LayoutComplete += Top_LayoutComplete;
- label = new()
- {
- X = Pos.Center (), Y = Pos.Bottom (frame) + 2, TextJustification = Alignment.Right, Text = "Button Pressed:"
- };
- Win.Add (label);
- var buttonPressedLabel = new Label
- {
- X = Pos.Center (),
- Y = Pos.Bottom (label) + 1,
- ColorScheme = Colors.ColorSchemes ["Error"],
- TextJustification = Alignment.Centered,
- Text = " "
- };
- //var btnText = new [] { "_Zero", "_One", "T_wo", "_Three", "_Four", "Fi_ve", "Si_x", "_Seven", "_Eight", "_Nine" };
- var showMessageBoxButton = new Button
- {
- X = Pos.Center (), Y = Pos.Bottom (frame) + 2, IsDefault = true, Text = "_Show MessageBox"
- };
- showMessageBoxButton.Accept += (s, e) =>
- {
- try
- {
- int width = int.Parse (widthEdit.Text);
- int height = int.Parse (heightEdit.Text);
- int numButtons = int.Parse (numButtonsEdit.Text);
- int defaultButton = int.Parse (defaultButtonEdit.Text);
- List<string> btns = new ();
- for (var i = 0; i < numButtons; i++)
- {
- //btns.Add(btnText[i % 10]);
- btns.Add (NumberToWords.Convert (i));
- }
- if (styleRadioGroup.SelectedItem == 0)
- {
- buttonPressedLabel.Text =
- $"{
- MessageBox.Query (
- width,
- height,
- titleEdit.Text,
- messageEdit.Text,
- defaultButton,
- (bool)ckbWrapMessage.Checked,
- btns.ToArray ()
- )
- }";
- }
- else
- {
- buttonPressedLabel.Text =
- $"{
- MessageBox.ErrorQuery (
- width,
- height,
- titleEdit.Text,
- messageEdit.Text,
- defaultButton,
- (bool)ckbWrapMessage.Checked,
- btns.ToArray ()
- )
- }";
- }
- }
- catch (FormatException)
- {
- buttonPressedLabel.Text = "Invalid Options";
- }
- };
- Win.Add (showMessageBoxButton);
- Win.Add (buttonPressedLabel);
- }
- }
|