Example.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 Terminal.Gui;
  5. Application.Run<ExampleWindow> ();
  6. // Before the application exits, reset Terminal.Gui for clean shutdown
  7. Application.Shutdown ();
  8. System.Console.WriteLine ($@"Username: {ExampleWindow.Username}");
  9. // Defines a top-level window with border and title
  10. public class ExampleWindow : Window {
  11. public static string Username { get; internal set; }
  12. public TextField usernameText;
  13. public ExampleWindow ()
  14. {
  15. Title = "Example App (Ctrl+Q to quit)";
  16. // Create input components and labels
  17. var usernameLabel = new Label () {
  18. Text = "Username:"
  19. };
  20. usernameText = new TextField ("") {
  21. // Position text field adjacent to the label
  22. X = Pos.Right (usernameLabel) + 1,
  23. // Fill remaining horizontal space
  24. Width = Dim.Fill (),
  25. };
  26. var passwordLabel = new Label () {
  27. Text = "Password:",
  28. X = Pos.Left (usernameLabel),
  29. Y = Pos.Bottom (usernameLabel) + 1
  30. };
  31. var passwordText = new TextField ("") {
  32. Secret = true,
  33. // align with the text box above
  34. X = Pos.Left (usernameText),
  35. Y = Pos.Top (passwordLabel),
  36. Width = Dim.Fill (),
  37. };
  38. // Create login button
  39. var btnLogin = new Button () {
  40. Text = "Login",
  41. Y = Pos.Bottom (passwordLabel) + 1,
  42. // center the login button horizontally
  43. X = Pos.Center (),
  44. IsDefault = true,
  45. };
  46. // When login button is clicked display a message popup
  47. btnLogin.Clicked += () => {
  48. if (usernameText.Text == "admin" && passwordText.Text == "password") {
  49. MessageBox.Query ("Logging In", "Login Successful", "Ok");
  50. Username = usernameText.Text.ToString ();
  51. Application.RequestStop ();
  52. } else {
  53. MessageBox.ErrorQuery ("Logging In", "Incorrect username or password", "Ok");
  54. }
  55. };
  56. // Add the views to the Window
  57. Add (usernameLabel, usernameText, passwordLabel, passwordText, btnLogin);
  58. }
  59. }