RunTExample.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. using Terminal.Gui;
  2. namespace UICatalog.Scenarios;
  3. [ScenarioMetadata ("Run<T> Example", "Illustrates using Application.Run<T> to run a custom class")]
  4. [ScenarioCategory ("Top Level Windows")]
  5. public class RunTExample : Scenario
  6. {
  7. public override void Run () { Application.Run<ExampleWindow> (); }
  8. public override void Setup ()
  9. {
  10. // No need to call Init if Application.Run<T> is used
  11. }
  12. public class ExampleWindow : Window
  13. {
  14. private readonly TextField _usernameText;
  15. public ExampleWindow ()
  16. {
  17. Title = $"Example App ({Application.QuitKey} to quit)";
  18. // Create input components and labels
  19. var usernameLabel = new Label { Text = "Username:" };
  20. _usernameText = new TextField
  21. {
  22. // Position text field adjacent to the label
  23. X = Pos.Right (usernameLabel) + 1,
  24. // Fill remaining horizontal space
  25. Width = Dim.Fill ()
  26. };
  27. var passwordLabel = new Label
  28. {
  29. Text = "Password:", X = Pos.Left (usernameLabel), Y = Pos.Bottom (usernameLabel) + 1
  30. };
  31. var passwordText = new TextField
  32. {
  33. Secret = true,
  34. // align with the text box above
  35. X = Pos.Left (_usernameText),
  36. Y = Pos.Top (passwordLabel),
  37. Width = Dim.Fill ()
  38. };
  39. // Create login button
  40. var btnLogin = new Button
  41. {
  42. Text = "Login",
  43. Y = Pos.Bottom (passwordLabel) + 1,
  44. // center the login button horizontally
  45. X = Pos.Center (),
  46. IsDefault = true
  47. };
  48. // When login button is clicked display a message popup
  49. btnLogin.Accept += (s, e) =>
  50. {
  51. if (_usernameText.Text == "admin" && passwordText.Text == "password")
  52. {
  53. MessageBox.Query ("Login Successful", $"Username: {_usernameText.Text}", "Ok");
  54. Application.RequestStop ();
  55. }
  56. else
  57. {
  58. MessageBox.ErrorQuery (
  59. "Error Logging In",
  60. "Incorrect username or password (hint: admin/password)",
  61. "Ok"
  62. );
  63. }
  64. };
  65. // Add the views to the Window
  66. Add (usernameLabel, _usernameText, passwordLabel, passwordText, btnLogin);
  67. }
  68. }
  69. }