Program.cs 2.8 KB

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