RunTExample.cs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. using Terminal.Gui;
  2. namespace UICatalog.Scenarios {
  3. [ScenarioMetadata (Name: "Run<T> Example", Description: "Illustrates using Application.Run<T> to run a custom class")]
  4. [ScenarioCategory ("Top Level Windows")]
  5. public class RunTExample : Scenario {
  6. public override void Setup ()
  7. {
  8. // No need to call Init if Application.Run<T> is used
  9. }
  10. public override void Run ()
  11. {
  12. Application.Run<ExampleWindow> ();
  13. }
  14. public class ExampleWindow : Window {
  15. public 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 () {
  21. Text = "Username:"
  22. };
  23. usernameText = new TextField ("") {
  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. Text = "Password:",
  31. X = Pos.Left (usernameLabel),
  32. Y = Pos.Bottom (usernameLabel) + 1
  33. };
  34. var passwordText = new TextField ("") {
  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. 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.Clicked += (s,e) => {
  51. if (usernameText.Text == "admin" && passwordText.Text == "password") {
  52. MessageBox.Query ("Login Successful", $"Username: {usernameText.Text}", "Ok");
  53. Application.RequestStop ();
  54. } else {
  55. MessageBox.ErrorQuery ("Error Logging In", "Incorrect username or password (hint: admin/password)", "Ok");
  56. }
  57. };
  58. // Add the views to the Window
  59. Add (usernameLabel, usernameText, passwordLabel, passwordText, btnLogin);
  60. }
  61. }
  62. }
  63. }