Example.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // A simple Terminal.Gui example in C# - using C# 9.0 Top-level statements
  2. // This is the same code found in the Termiminal Gui README.md file.
  3. using Terminal.Gui;
  4. using NStack;
  5. Application.Init ();
  6. // Creates the top-level window to show
  7. var win = new Window ("Example App") {
  8. X = 0,
  9. Y = 1, // Leave one row for the toplevel menu
  10. // By using Dim.Fill(), this Window will automatically resize without manual intervention
  11. Width = Dim.Fill (),
  12. Height = Dim.Fill ()
  13. };
  14. Application.Top.Add (win);
  15. // Creates a menubar, the item "New" has a help menu.
  16. var menu = new MenuBar (new MenuBarItem [] {
  17. new MenuBarItem ("_File", new MenuItem [] {
  18. new MenuItem ("_New", "Creates a new file", null),
  19. new MenuItem ("_Close", "",null),
  20. new MenuItem ("_Quit", "", () => { if (Quit ()) Application.Top.Running = false; })
  21. }),
  22. new MenuBarItem ("_Edit", new MenuItem [] {
  23. new MenuItem ("_Copy", "", null),
  24. new MenuItem ("C_ut", "", null),
  25. new MenuItem ("_Paste", "", null)
  26. })
  27. });
  28. Application.Top.Add (menu);
  29. static bool Quit ()
  30. {
  31. var n = MessageBox.Query (50, 7, "Quit Example", "Are you sure you want to quit this example?", "Yes", "No");
  32. return n == 0;
  33. }
  34. var login = new Label ("Login: ") { X = 3, Y = 2 };
  35. var password = new Label ("Password: ") {
  36. X = Pos.Left (login),
  37. Y = Pos.Top (login) + 1
  38. };
  39. var loginText = new TextField ("") {
  40. X = Pos.Right (password),
  41. Y = Pos.Top (login),
  42. Width = 40
  43. };
  44. var passText = new TextField ("") {
  45. Secret = true,
  46. X = Pos.Left (loginText),
  47. Y = Pos.Top (password),
  48. Width = Dim.Width (loginText)
  49. };
  50. // Add the views to the main window,
  51. win.Add (
  52. // Using Computed Layout:
  53. login, password, loginText, passText,
  54. // Using Absolute Layout:
  55. new CheckBox (3, 6, "Remember me"),
  56. new RadioGroup (3, 8, new ustring [] { "_Personal", "_Company" }, 0),
  57. new Button (3, 14, "Ok"),
  58. new Button (10, 14, "Cancel"),
  59. new Label (3, 18, "Press F9 or ESC plus 9 to activate the menubar")
  60. );
  61. // Run blocks until the user quits the application
  62. Application.Run ();
  63. // Always bracket Application.Init with .Shutdown.
  64. Application.Shutdown ();