Example.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 = ["SetDelay:500", "a", "d", "m", "i", "n", "Tab", "p", "a", "s", "s", "w", "o", "r", "d", "Enter"], Order = 1)]
  13. [assembly: Terminal.Gui.Examples.ExampleDemoKeyStrokes (KeyStrokes = ["SetDelay:500", "Enter"], Order = 2)]
  14. [assembly: Terminal.Gui.Examples.ExampleDemoKeyStrokes (KeyStrokes = ["SetDelay:100", "Esc"], Order = 3)]
  15. // Override the default configuration for the application to use the Light theme
  16. ConfigurationManager.RuntimeConfig = """{ "Theme": "Light" }""";
  17. ConfigurationManager.Enable (ConfigLocations.All);
  18. IApplication app = Application.Create (example: true);
  19. app.Init ();
  20. app.Run<ExampleWindow> ();
  21. // Dispose the app to clean up and enable Console.WriteLine below
  22. app.Dispose ();
  23. // To see this output on the screen it must be done after shutdown,
  24. // which restores the previous screen.
  25. Console.WriteLine ($@"Username: {ExampleWindow.UserName}");
  26. // Defines a top-level window with border and title
  27. public sealed class ExampleWindow : Window
  28. {
  29. public static string UserName { get; set; }
  30. public ExampleWindow ()
  31. {
  32. Title = $"Example App ({Application.QuitKey} to quit)";
  33. // Create input components and labels
  34. var usernameLabel = new Label { Text = "Username:" };
  35. var userNameText = new TextField
  36. {
  37. // Position text field adjacent to the label
  38. X = Pos.Right (usernameLabel) + 1,
  39. // Fill remaining horizontal space
  40. Width = Dim.Fill ()
  41. };
  42. var passwordLabel = new Label
  43. {
  44. Text = "Password:", X = Pos.Left (usernameLabel), Y = Pos.Bottom (usernameLabel) + 1
  45. };
  46. var passwordText = new TextField
  47. {
  48. Secret = true,
  49. // align with the text box above
  50. X = Pos.Left (userNameText),
  51. Y = Pos.Top (passwordLabel),
  52. Width = Dim.Fill ()
  53. };
  54. // Create login button
  55. var btnLogin = new Button
  56. {
  57. Text = "Login",
  58. Y = Pos.Bottom (passwordLabel) + 1,
  59. // center the login button horizontally
  60. X = Pos.Center (),
  61. IsDefault = true
  62. };
  63. // When login button is clicked display a message popup
  64. btnLogin.Accepting += (s, e) =>
  65. {
  66. if (userNameText.Text == "admin" && passwordText.Text == "password")
  67. {
  68. MessageBox.Query (App, "Logging In", "Login Successful", "Ok");
  69. UserName = userNameText.Text;
  70. Application.RequestStop ();
  71. }
  72. else
  73. {
  74. MessageBox.ErrorQuery (App, "Logging In", "Incorrect username or password", "Ok");
  75. }
  76. // When Accepting is handled, set e.Handled to true to prevent further processing.
  77. e.Handled = true;
  78. };
  79. // Add the views to the Window
  80. Add (usernameLabel, userNameText, passwordLabel, passwordText, btnLogin);
  81. var lv = new ListView
  82. {
  83. Y = Pos.AnchorEnd (),
  84. Height = Dim.Auto (),
  85. Width = Dim.Auto ()
  86. };
  87. lv.SetSource (["One", "Two", "Three", "Four"]);
  88. Add (lv);
  89. }
  90. }