Example.cs 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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.ViewBase;
  7. using Terminal.Gui.Views;
  8. // Override the default configuration for the application to use the Amber Phosphor theme
  9. ConfigurationManager.RuntimeConfig = """{ "Theme": "Amber Phosphor" }""";
  10. ConfigurationManager.Enable (ConfigLocations.All);
  11. IApplication app = Application.Create ();
  12. app.Run<ExampleWindow> ();
  13. // Dispose the app to clean up and enable Console.WriteLine below
  14. app.Dispose ();
  15. // To see this output on the screen it must be done after shutdown,
  16. // which restores the previous screen.
  17. Console.WriteLine ($@"Username: {ExampleWindow.UserName}");
  18. // Defines a top-level window with border and title
  19. public sealed class ExampleWindow : Window
  20. {
  21. public static string UserName { get; set; }
  22. public ExampleWindow ()
  23. {
  24. Title = $"Example App ({Application.QuitKey} to quit)";
  25. // Create input components and labels
  26. var usernameLabel = new Label { Text = "Username:" };
  27. var userNameText = new TextField
  28. {
  29. // Position text field adjacent to the label
  30. X = Pos.Right (usernameLabel) + 1,
  31. // Fill remaining horizontal space
  32. Width = Dim.Fill ()
  33. };
  34. var passwordLabel = new Label
  35. {
  36. Text = "Password:", X = Pos.Left (usernameLabel), Y = Pos.Bottom (usernameLabel) + 1
  37. };
  38. var passwordText = new TextField
  39. {
  40. Secret = true,
  41. // align with the text box above
  42. X = Pos.Left (userNameText),
  43. Y = Pos.Top (passwordLabel),
  44. Width = Dim.Fill ()
  45. };
  46. // Create login button
  47. var btnLogin = new Button
  48. {
  49. Text = "Login",
  50. Y = Pos.Bottom (passwordLabel) + 1,
  51. // center the login button horizontally
  52. X = Pos.Center (),
  53. IsDefault = true
  54. };
  55. // When login button is clicked display a message popup
  56. btnLogin.Accepting += (s, e) =>
  57. {
  58. if (userNameText.Text == "admin" && passwordText.Text == "password")
  59. {
  60. MessageBox.Query (App, "Logging In", "Login Successful", "Ok");
  61. UserName = userNameText.Text;
  62. Application.RequestStop ();
  63. }
  64. else
  65. {
  66. MessageBox.ErrorQuery (App, "Logging In", "Incorrect username or password", "Ok");
  67. }
  68. // When Accepting is handled, set e.Handled to true to prevent further processing.
  69. e.Handled = true;
  70. };
  71. // Add the views to the Window
  72. Add (usernameLabel, userNameText, passwordLabel, passwordText, btnLogin);
  73. }
  74. }