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