mirror of
https://github.com/gui-cs/Terminal.Gui.git
synced 2025-12-26 15:57:56 +01:00
ComboBox. Adhere to ListView interface, added:-
SelectedItem (read only) from @BDisp OpenSelectedItem
This commit is contained in:
@@ -449,7 +449,8 @@ static class Demo {
|
||||
|
||||
static void ComboBoxDemo ()
|
||||
{
|
||||
List<ustring> items = new List<ustring> ();
|
||||
//TODO: Duplicated code in ListsAndCombos.cs Consider moving to shared assembly
|
||||
var items = new List<ustring> ();
|
||||
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);
|
||||
|
||||
@@ -55,13 +55,14 @@ namespace Terminal.Gui {
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Changed event, raised when the selection has been confirmed.
|
||||
/// This event is raised when the selected item in the <see cref="ComboBox"/> has changed.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Client code can hook up to this event, it is
|
||||
/// raised when the selection has been confirmed.
|
||||
/// </remarks>
|
||||
public event EventHandler<ListViewItemEventArgs> SelectedItemChanged;
|
||||
public Action<ListViewItemEventArgs> SelectedItemChanged;
|
||||
|
||||
/// <summary>
|
||||
/// This event is raised when the user Double Clicks on an item or presses ENTER to open the selected item.
|
||||
/// </summary>
|
||||
public Action<ListViewItemEventArgs> OpenSelectedItem;
|
||||
|
||||
IList searchset;
|
||||
ustring text = "";
|
||||
@@ -153,6 +154,12 @@ namespace Terminal.Gui {
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the index of the currently selected item in the <see cref="Source"/>
|
||||
/// </summary>
|
||||
/// <value>The selected item or -1 none selected.</value>
|
||||
public int SelectedItem { private set; get; }
|
||||
|
||||
bool isShow = false;
|
||||
|
||||
///<inheritdoc/>
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Invokes the OnOpenSelectedItem event if it is defined.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reset to full original list
|
||||
/// </summary>
|
||||
|
||||
@@ -12,7 +12,8 @@ namespace UICatalog.Scenarios {
|
||||
|
||||
public override void Setup ()
|
||||
{
|
||||
List<ustring> items = new List<ustring> ();
|
||||
//TODO: Duplicated code in Demo.cs Consider moving to shared assembly
|
||||
var items = new List<ustring> ();
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user