Example.cs 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // This is a simple example application. For the full range of functionality
  2. // see the UICatalog project
  3. // A simple Terminal.Gui example in C# - using C# 9.0 Top-level statements
  4. using System;
  5. using Terminal.Gui;
  6. ConfigurationManager.Memory = """
  7. {
  8. "Application.QuitKey" : "Ctrl+Q"
  9. }
  10. """;
  11. Application.Run<ExampleWindow> ().Dispose ();
  12. // Before the application exits, reset Terminal.Gui for clean shutdown
  13. Application.Shutdown ();
  14. // To see this output on the screen it must be done after shutdown,
  15. // which restores the previous screen.
  16. Console.WriteLine ($@"Username: {ExampleWindow.UserName}");
  17. // Defines a top-level window with border and title
  18. public class ExampleWindow : Window
  19. {
  20. public static string UserName;
  21. public ExampleWindow ()
  22. {
  23. Title = $"Example App ({Application.QuitKey} to quit)";
  24. // Create input components and labels
  25. var usernameLabel = new Label { Text = "Username:" };
  26. var userNameText = new TextField
  27. {
  28. // Position text field adjacent to the label
  29. X = Pos.Right (usernameLabel) + 1,
  30. // Fill remaining horizontal space
  31. Width = Dim.Fill ()
  32. };
  33. var passwordLabel = new Label
  34. {
  35. Text = "Password:", X = Pos.Left (usernameLabel), Y = Pos.Bottom (usernameLabel) + 1
  36. };
  37. var passwordText = new TextField
  38. {
  39. Secret = true,
  40. // align with the text box above
  41. X = Pos.Left (userNameText),
  42. Y = Pos.Top (passwordLabel),
  43. Width = Dim.Fill ()
  44. };
  45. // Create login button
  46. var btnLogin = new Button
  47. {
  48. Text = "Login",
  49. Y = Pos.Bottom (passwordLabel) + 1,
  50. // center the login button horizontally
  51. X = Pos.Center (),
  52. IsDefault = true
  53. };
  54. // When login button is clicked display a message popup
  55. btnLogin.Accepting += (s, e) =>
  56. {
  57. if (userNameText.Text == "admin" && passwordText.Text == "password")
  58. {
  59. MessageBox.Query ("Logging In", "Login Successful", "Ok");
  60. UserName = userNameText.Text;
  61. Application.RequestStop ();
  62. }
  63. else
  64. {
  65. MessageBox.ErrorQuery ("Logging In", "Incorrect username or password", "Ok");
  66. }
  67. };
  68. // Add the views to the Window
  69. Add (usernameLabel, userNameText, passwordLabel, passwordText, btnLogin);
  70. }
  71. }