Example.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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 Terminal.Gui;
  5. // Initialize the console
  6. Application.Init();
  7. // Creates the top-level window with border and title
  8. var win = new Window("Example App (Ctrl+Q to quit)");
  9. // Create input components and labels
  10. var usernameLabel = new Label("Username:");
  11. var usernameText = new TextField("")
  12. {
  13. // Position text field adjacent to label
  14. X = Pos.Right(usernameLabel) + 1,
  15. // Fill remaining horizontal space with a margin of 1
  16. Width = Dim.Fill(1),
  17. };
  18. var passwordLabel = new Label(0,2,"Password:");
  19. var passwordText = new TextField("")
  20. {
  21. Secret = true,
  22. // align with the text box above
  23. X = Pos.Left(usernameText),
  24. Y = 2,
  25. Width = Dim.Fill(1),
  26. };
  27. // Create login button
  28. var btnLogin = new Button("Login")
  29. {
  30. Y = 4,
  31. // center the login button horizontally
  32. X = Pos.Center(),
  33. IsDefault = true,
  34. };
  35. // When login button is clicked display a message popup
  36. btnLogin.Clicked += () => MessageBox.Query("Logging In", "Login Successful", "Ok");
  37. // Add all the views to the window
  38. win.Add(
  39. usernameLabel, usernameText, passwordLabel, passwordText,btnLogin
  40. );
  41. // Show the application
  42. Application.Run(win);
  43. // After the application exits, release and reset console for clean shutdown
  44. Application.Shutdown();