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