Example.cs 4.2 KB

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