Example.cs 2.5 KB

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