Program.cs 3.4 KB

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