Example.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #nullable enable
  2. // A simple Terminal.Gui example in C# - using C# 9.0 Top-level statements
  3. // This is a simple example application. For the full range of functionality
  4. // see the UICatalog project
  5. using Terminal.Gui.App;
  6. using Terminal.Gui.Configuration;
  7. using Terminal.Gui.ViewBase;
  8. using Terminal.Gui.Views;
  9. // Example metadata
  10. [assembly: Terminal.Gui.Examples.ExampleMetadata ("Simple Example", "A basic login form demonstrating Terminal.Gui fundamentals")]
  11. [assembly: Terminal.Gui.Examples.ExampleCategory ("Getting Started")]
  12. [assembly: Terminal.Gui.Examples.ExampleDemoKeyStrokes (KeyStrokes = ["a", "d", "m", "i", "n", "Tab", "p", "a", "s", "s", "w", "o", "r", "d", "Enter", "Esc"], Order = 1)]
  13. // Override the default configuration for the application to use the Light theme
  14. ConfigurationManager.RuntimeConfig = """{ "Theme": "Light" }""";
  15. ConfigurationManager.Enable (ConfigLocations.All);
  16. IApplication app = Application.Create (example: true);
  17. app.Init ();
  18. app.Run<ExampleWindow> ();
  19. string? result = app.GetResult<string> ();
  20. // Dispose the app to clean up and enable Console.WriteLine below
  21. app.Dispose ();
  22. // To see this output on the screen it must be done after shutdown,
  23. // which restores the previous screen.
  24. Console.WriteLine ($@"Username: {result}");
  25. // Defines a top-level window with border and title
  26. public sealed class ExampleWindow : Window
  27. {
  28. public ExampleWindow ()
  29. {
  30. Title = $"Example App ({Application.QuitKey} to quit)";
  31. // Create input components and labels
  32. var usernameLabel = new Label { Text = "Username:" };
  33. var userNameText = new TextField
  34. {
  35. // Position text field adjacent to the label
  36. X = Pos.Right (usernameLabel) + 1,
  37. // Fill remaining horizontal space
  38. Width = Dim.Fill ()
  39. };
  40. var passwordLabel = new Label
  41. {
  42. Text = "Password:", X = Pos.Left (usernameLabel), Y = Pos.Bottom (usernameLabel) + 1
  43. };
  44. var passwordText = new TextField
  45. {
  46. Secret = true,
  47. // align with the text box above
  48. X = Pos.Left (userNameText),
  49. Y = Pos.Top (passwordLabel),
  50. Width = Dim.Fill ()
  51. };
  52. // Create login button
  53. var btnLogin = new Button
  54. {
  55. Text = "Login",
  56. Y = Pos.Bottom (passwordLabel) + 1,
  57. // center the login button horizontally
  58. X = Pos.Center (),
  59. IsDefault = true
  60. };
  61. // When login button is clicked display a message popup
  62. btnLogin.Accepting += (s, e) =>
  63. {
  64. if (userNameText.Text == "admin" && passwordText.Text == "password")
  65. {
  66. MessageBox.Query (App, "Logging In", "Login Successful", "Ok");
  67. Result = userNameText.Text;
  68. App?.RequestStop ();
  69. }
  70. else
  71. {
  72. MessageBox.ErrorQuery (App, "Logging In", "Incorrect username or password", "Ok");
  73. }
  74. // When Accepting is handled, set e.Handled to true to prevent further processing.
  75. e.Handled = true;
  76. };
  77. // Add the views to the Window
  78. Add (usernameLabel, userNameText, passwordLabel, passwordText, btnLogin);
  79. }
  80. }