2
0

Example.cs 3.6 KB

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