ListsAndCombos.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. using System;
  2. using System.Linq;
  3. using System.IO;
  4. using System.Collections.Generic;
  5. using Terminal.Gui;
  6. using NStack;
  7. namespace UICatalog.Scenarios {
  8. [ScenarioMetadata (Name: "ListView & ComboBox", Description: "Demonstrates a ListView populating a ComboBox that acts as a filter.")]
  9. [ScenarioCategory ("Controls")]
  10. class ListsAndCombos : Scenario {
  11. public override void Setup ()
  12. {
  13. List<string> items = new List<string> ();
  14. foreach (var dir in new [] { "/etc", @"\windows\System32" }) {
  15. if (Directory.Exists (dir)) {
  16. items = Directory.GetFiles (dir)
  17. .Select (Path.GetFileName)
  18. .Where (x => char.IsLetterOrDigit (x [0]))
  19. .Distinct ()
  20. .OrderBy (x => x).ToList ();
  21. }
  22. }
  23. // ListView
  24. var lbListView = new Label ("Listview") {
  25. ColorScheme = Colors.TopLevel,
  26. X = 0,
  27. Width = 30
  28. };
  29. var listview = new ListView (items) {
  30. X = 0,
  31. Y = Pos.Bottom (lbListView) + 1,
  32. Width = 30
  33. };
  34. listview.OpenSelectedItem += (object sender, ListViewItemEventArgs e) => lbListView.Text = items [listview.SelectedItem];
  35. Win.Add (lbListView, listview);
  36. // ComboBox
  37. var lbComboBox = new Label ("ComboBox") {
  38. ColorScheme = Colors.TopLevel,
  39. X = Pos.Right (lbListView) + 1,
  40. Width = 30
  41. };
  42. var comboBox = new ComboBox (0, 0, 30, 10, items) {
  43. X = Pos.Right(listview) + 1 ,
  44. Y = Pos.Bottom (lbListView) +1,
  45. Width = 30
  46. };
  47. comboBox.Changed += (object sender, ustring text) => lbComboBox.Text = text;
  48. Win.Add (lbComboBox, comboBox);
  49. }
  50. }
  51. }