Sfoglia il codice sorgente

ListView: Check if pressing enter, selected item is actually in data list

Returns false if selected index is not in range of the list (stale value or empty list)
Mgamerz 4 anni fa
parent
commit
9c26ef22f9
1 ha cambiato i file con 15 aggiunte e 14 eliminazioni
  1. 15 14
      Terminal.Gui/Views/ListView.cs

+ 15 - 14
Terminal.Gui/Views/ListView.cs

@@ -450,17 +450,18 @@ namespace Terminal.Gui {
 		{
 			if (source.Count == 0){
 				// Do we set lastSelectedItem to zero here?
-				return false; //Nothing for us to move to
-			}
+                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;
+                selected = source.Count - 1;
 				OnSelectedChanged ();
 				SetNeedsDisplay ();
-			} else if (selected + 1 < source.Count) { //can move by down by one.
+            } else if (selected + 1 < source.Count) { //can move by down by one.
 				selected++;
+
 				if (selected >= top + Frame.Height)
 					top++;
 				OnSelectedChanged ();
@@ -481,26 +482,25 @@ namespace Terminal.Gui {
 		{
 			if (source.Count == 0){
 				// Do we set lastSelectedItem to zero here?
-				return false; //Nothing for us to move to
-			}
+                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;
+                selected = source.Count - 1;
 				OnSelectedChanged ();
 				SetNeedsDisplay ();
-			} else if (selected > 0) {
-				selected--; 
+            } else if (selected > 0) {
+				selected--;
 				if (selected > Source.Count) {
-					selected = Source.Count - 1;
-				}
-				if (selected < top) 
+                    selected = Source.Count - 1;
+                }
+				if (selected < top)
 					top = selected;
 				OnSelectedChanged ();
 				SetNeedsDisplay ();
 			}
-
 			return true;
 		}
 
@@ -560,7 +560,8 @@ namespace Terminal.Gui {
 		/// <returns></returns>
 		public virtual bool OnOpenSelectedItem ()
 		{
-			var value = source.ToList () [selected];
+			if (source.Count <= selected ||selected < 0) return false;
+            var value = source.ToList () [selected];
 			OpenSelectedItem?.Invoke (new ListViewItemEventArgs (selected, value));
 
 			return true;