Program.cs 2.5 KB

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