Browse Source

Merge pull request #906 from tig/pr891

Fixed version of PR 891
Charlie Kindel 4 years ago
parent
commit
a4805fd359
2 changed files with 49 additions and 5 deletions
  1. 31 5
      Terminal.Gui/Views/ListView.cs
  2. 18 0
      UICatalog/Scenarios/ListsAndCombos.cs

+ 31 - 5
Terminal.Gui/Views/ListView.cs

@@ -1,4 +1,4 @@
-//
+//
 // ListView.cs: ListView control
 //
 // Authors:
@@ -448,8 +448,20 @@ namespace Terminal.Gui {
 		/// <returns></returns>
 		public virtual bool MoveDown ()
 		{
-			if (selected + 1 < source.Count) {
+			if (source.Count == 0) {
+				// Do we set lastSelectedItem to zero here?
+				return false; //Nothing for us to move to
+			}
+			if (selected >= source.Count) {
+				// If for some reason we are currently outside of the
+				// valid values range, we should select the bottommost valid value.
+				// This can occur if the backing data source changes.
+				selected = source.Count - 1;
+				OnSelectedChanged ();
+				SetNeedsDisplay ();
+			} else if (selected + 1 < source.Count) { //can move by down by one.
 				selected++;
+
 				if (selected >= top + Frame.Height)
 					top++;
 				OnSelectedChanged ();
@@ -468,14 +480,27 @@ namespace Terminal.Gui {
 		/// <returns></returns>
 		public virtual bool MoveUp ()
 		{
-			if (selected > 0) {
+			if (source.Count == 0) {
+				// Do we set lastSelectedItem to zero here?
+				return false; //Nothing for us to move to
+			}
+			if (selected >= source.Count) {
+				// If for some reason we are currently outside of the
+				// valid values range, we should select the bottommost valid value.
+				// This can occur if the backing data source changes.
+				selected = source.Count - 1;
+				OnSelectedChanged ();
+				SetNeedsDisplay ();
+			} else if (selected > 0) {
 				selected--;
+				if (selected > Source.Count) {
+					selected = Source.Count - 1;
+				}
 				if (selected < top)
 					top = selected;
 				OnSelectedChanged ();
 				SetNeedsDisplay ();
 			}
-
 			return true;
 		}
 
@@ -535,6 +560,7 @@ namespace Terminal.Gui {
 		/// <returns></returns>
 		public virtual bool OnOpenSelectedItem ()
 		{
+			if (source.Count <= selected || selected < 0) return false;
 			var value = source.ToList () [selected];
 			OpenSelectedItem?.Invoke (new ListViewItemEventArgs (selected, value));
 
@@ -573,7 +599,7 @@ namespace Terminal.Gui {
 		}
 
 		///<inheritdoc/>
-		public override bool MouseEvent(MouseEvent me)
+		public override bool MouseEvent (MouseEvent me)
 		{
 			if (!me.Flags.HasFlag (MouseFlags.Button1Clicked) && !me.Flags.HasFlag (MouseFlags.Button1DoubleClicked) &&
 				me.Flags != MouseFlags.WheeledDown && me.Flags != MouseFlags.WheeledUp)

+ 18 - 0
UICatalog/Scenarios/ListsAndCombos.cs

@@ -56,6 +56,24 @@ namespace UICatalog.Scenarios {
 
 			comboBox.SelectedItemChanged += (ListViewItemEventArgs text) => lbComboBox.Text = items[comboBox.SelectedItem];
 			Win.Add (lbComboBox, comboBox);
+
+			var btnMoveUp = new Button ("Move _Up") {
+				X = 1,
+				Y = Pos.Bottom(lbListView),
+			};
+			btnMoveUp.Clicked += () => {
+				listview.MoveUp ();
+			};
+
+			var btnMoveDown = new Button ("Move _Down") {
+				X = Pos.Right (btnMoveUp) + 1,
+				Y = Pos.Bottom (lbListView),
+			};
+			btnMoveDown.Clicked += () => {
+				listview.MoveDown ();
+			};
+
+			Win.Add (btnMoveUp, btnMoveDown);
 		}
 	}
 }