Example.cs 1.9 KB

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