Example.cs 2.5 KB

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