RunTExample.cs 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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> ();
  11. Application.Top.Dispose ();
  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 TextField
  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. }