diff --git a/Example/demo.cs b/Example/demo.cs index edb9ef34b..da61d517b 100644 --- a/Example/demo.cs +++ b/Example/demo.cs @@ -449,7 +449,8 @@ static class Demo { static void ComboBoxDemo () { - List items = new List (); + //TODO: Duplicated code in ListsAndCombos.cs Consider moving to shared assembly + var items = new List (); foreach (var dir in new [] { "/etc", @$"{Environment.GetEnvironmentVariable ("SystemRoot")}\System32" }) { if (Directory.Exists (dir)) { items = Directory.GetFiles (dir).Union (Directory.GetDirectories (dir)) @@ -459,8 +460,8 @@ static class Demo { } } var list = new ComboBox () { Width = Dim.Fill(), Height = Dim.Fill() }; - list.SetSource(items.ToList()); - list.SelectedItemChanged += (object sender, ListViewItemEventArgs text) => { Application.RequestStop (); }; + list.SetSource(items); + list.OpenSelectedItem += (ListViewItemEventArgs text) => { Application.RequestStop (); }; var d = new Dialog () { Title = "Select source file", Width = Dim.Percent (50), Height = Dim.Percent (50) }; d.Add (list); diff --git a/Terminal.Gui/Views/ComboBox.cs b/Terminal.Gui/Views/ComboBox.cs index 644d3b4c1..7323fdf91 100644 --- a/Terminal.Gui/Views/ComboBox.cs +++ b/Terminal.Gui/Views/ComboBox.cs @@ -55,13 +55,14 @@ namespace Terminal.Gui { } /// - /// Changed event, raised when the selection has been confirmed. + /// This event is raised when the selected item in the has changed. /// - /// - /// Client code can hook up to this event, it is - /// raised when the selection has been confirmed. - /// - public event EventHandler SelectedItemChanged; + public Action SelectedItemChanged; + + /// + /// This event is raised when the user Double Clicks on an item or presses ENTER to open the selected item. + /// + public Action OpenSelectedItem; IList searchset; ustring text = ""; @@ -153,6 +154,12 @@ namespace Terminal.Gui { }; } + /// + /// Gets the index of the currently selected item in the + /// + /// The selected item or -1 none selected. + public int SelectedItem { private set; get; } + bool isShow = false; /// @@ -209,7 +216,19 @@ namespace Terminal.Gui { { // Note: Cannot rely on "listview.SelectedItem != lastSelectedItem" because the list is dynamic. // So we cannot optimize. Ie: Don't call if not changed - SelectedItemChanged?.Invoke (this, new ListViewItemEventArgs(listview.SelectedItem, search.Text)); + SelectedItemChanged?.Invoke (new ListViewItemEventArgs(SelectedItem, search.Text)); + + return true; + } + + /// + /// Invokes the OnOpenSelectedItem event if it is defined. + /// + /// + public virtual bool OnOpenSelectedItem () + { + var value = search.Text; + OpenSelectedItem?.Invoke (new ListViewItemEventArgs (SelectedItem, value)); return true; } @@ -301,6 +320,8 @@ namespace Terminal.Gui { this.text = search.Text = text.ToString(); search.CursorPosition = 0; search.TextChanged += Search_Changed; + SelectedItem = GetSelectedItemFromSource (this.text); + OnSelectedChanged (); } private void Selected () @@ -313,10 +334,23 @@ namespace Terminal.Gui { SetValue (searchset [listview.SelectedItem]); search.CursorPosition = search.Text.RuneCount; Search_Changed (search.Text); - OnSelectedChanged (); + OnOpenSelectedItem (); Reset (keepSearchText: true); } + private int GetSelectedItemFromSource (ustring value) + { + if (source == null) { + return -1; + } + for (int i = 0; i < source.Count; i++) { + if (source.ToList () [i].ToString () == value) { + return i; + } + } + return -1; + } + /// /// Reset to full original list /// diff --git a/UICatalog/Scenarios/ListsAndCombos.cs b/UICatalog/Scenarios/ListsAndCombos.cs index 065fbe263..7d44e8a89 100644 --- a/UICatalog/Scenarios/ListsAndCombos.cs +++ b/UICatalog/Scenarios/ListsAndCombos.cs @@ -12,7 +12,8 @@ namespace UICatalog.Scenarios { public override void Setup () { - List items = new List (); + //TODO: Duplicated code in Demo.cs Consider moving to shared assembly + var items = new List (); foreach (var dir in new [] { "/etc", @$"{Environment.GetEnvironmentVariable ("SystemRoot")}\System32" }) { if (Directory.Exists (dir)) { items = Directory.GetFiles (dir).Union(Directory.GetDirectories(dir)) @@ -35,7 +36,7 @@ namespace UICatalog.Scenarios { Height = Dim.Fill(2), Width = Dim.Percent (40) }; - listview.OpenSelectedItem += (ListViewItemEventArgs e) => lbListView.Text = items [listview.SelectedItem]; + listview.SelectedItemChanged += (ListViewItemEventArgs e) => lbListView.Text = items [listview.SelectedItem]; Win.Add (lbListView, listview); // ComboBox @@ -53,7 +54,7 @@ namespace UICatalog.Scenarios { }; comboBox.SetSource (items); - comboBox.SelectedItemChanged += (object sender, ListViewItemEventArgs text) => lbComboBox.Text = (ustring)text.Value; + comboBox.SelectedItemChanged += (ListViewItemEventArgs text) => lbComboBox.Text = items[comboBox.SelectedItem]; Win.Add (lbComboBox, comboBox); } }