Example.cs 2.6 KB

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