/// <summary> /// Change the size of the virtual list so that it matches its data source /// </summary> public virtual void UpdateVirtualListSize() { if (VirtualListDataSource != null) { SetVirtualListSize(VirtualListDataSource.GetObjectCount()); } }
/// <summary> /// Handle the CacheVirtualItems event /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected virtual void HandleCacheVirtualItems(object sender, CacheVirtualItemsEventArgs e) { if (VirtualListDataSource != null) { VirtualListDataSource.PrepareCache(e.StartIndex, e.EndIndex); } }
/// <summary> /// Remove all of the given objects from the control /// </summary> /// <param name="modelObjects">Collection of objects to be removed</param> /// <remarks> /// <para>Nulls and model objects that are not in the ListView are silently ignored.</para> /// <para>Due to problems in the underlying ListView, if you remove all the objects from /// the control using this method and the list scroll vertically when you do so, /// then when you subsequenially add more objects to the control, /// the vertical scroll bar will become confused and the control will draw one or more /// blank lines at the top of the list. </para> /// </remarks> public override void RemoveObjects(ICollection <object> modelObjects) { if (VirtualListDataSource == null) { return; } // Give the world a chance to cancel or change the removed objects ItemsRemovingEventArgs args = new ItemsRemovingEventArgs(modelObjects); OnItemsRemoving(args); if (args.Canceled) { return; } try { BeginUpdate(); VirtualListDataSource.RemoveObjects(args.ObjectsToRemove); BuildList(); } finally { EndUpdate(); } }
/// <summary> /// Set the collection of objects that this control will show. /// </summary> /// <param name="collection"></param> /// <param name="preserveState">Should the state of the list be preserved as far as is possible.</param> public override void SetObjects(IEnumerable <object> collection, bool preserveState) { if (InvokeRequired) { Invoke((MethodInvoker) delegate { SetObjects(collection, preserveState); }); return; } if (VirtualListDataSource == null) { return; } // Give the world a chance to cancel or change the assigned collection ItemsChangingEventArgs args = new ItemsChangingEventArgs(null, collection); OnItemsChanging(args); if (args.Canceled) { return; } BeginUpdate(); try { VirtualListDataSource.SetObjects(args.NewObjects); BuildList(); } finally { EndUpdate(); } }
/// <summary> /// Select the row that is displaying the given model object. All other rows are deselected. /// </summary> /// <param name="modelObject">Model object to select</param> /// <param name="setFocus">Should the object be focused as well?</param> public override void SelectObject(object modelObject, bool setFocus) { // Without a data source, we can't do this. if (VirtualListDataSource == null) { return; } // Check that the object is in the list (plus not all data sources can locate objects) int index = VirtualListDataSource.GetObjectIndex(modelObject); if (index < 0 || index >= VirtualListSize) { return; } // If the given model is already selected, don't do anything else (prevents an flicker) if (SelectedIndices.Count == 1 && SelectedIndices[0] == index) { return; } // Finally, select the row SelectedIndices.Clear(); SelectedIndices.Add(index); if (setFocus && SelectedItem != null) { SelectedItem.Focused = true; } }
/// <summary> /// Update the rows that are showing the given objects /// </summary> /// <remarks>This method does not resort the items.</remarks> public override void RefreshObjects(IList <object> modelObjects) { if (InvokeRequired) { Invoke((MethodInvoker) delegate { RefreshObjects(modelObjects); }); return; } // Without a data source, we can't do this. if (VirtualListDataSource == null) { return; } try { BeginUpdate(); ClearCachedInfo(); foreach (object modelObject in modelObjects) { int index = VirtualListDataSource.GetObjectIndex(modelObject); if (index >= 0) { VirtualListDataSource.UpdateObject(index, modelObject); RedrawItems(index, index, true); } } } finally { EndUpdate(); } }
/// <summary> /// Inserts the given collection of model objects to this control at hte given location /// </summary> /// <param name="modelObjects">A collection of model objects</param> /// <remarks> /// <para>The added objects will appear in their correct sort position, if sorting /// is active. Otherwise, they will appear at the given position of the list.</para> /// <para>No check is performed to see if any of the objects are already in the ListView.</para> /// <para>Null objects are silently ignored.</para> /// </remarks> public override void InsertObjects(int index, ICollection <object> modelObjects) { if (VirtualListDataSource == null) { return; } // Give the world a chance to cancel or change the added objects ItemsAddingEventArgs args = new ItemsAddingEventArgs(index, modelObjects); OnItemsAdding(args); if (args.Canceled) { return; } try { BeginUpdate(); VirtualListDataSource.InsertObjects(index, args.ObjectsToAdd); BuildList(); } finally { EndUpdate(); } }
/// <summary> /// Find the given model object within the listview and return its index /// </summary> /// <param name="modelObject">The model object to be found</param> /// <returns>The index of the object. -1 means the object was not present</returns> public override int IndexOf(object modelObject) { if (VirtualListDataSource == null || modelObject == null) { return(-1); } return(VirtualListDataSource.GetObjectIndex(modelObject)); }
/// <summary> /// Return the OLVListItem that displays the given model object /// </summary> /// <param name="modelObject">The modelObject whose item is to be found</param> /// <returns>The OLVListItem that displays the model, or null</returns> /// <remarks>This method has O(n) performance.</remarks> public override OLVListItem ModelToItem(object modelObject) { if (VirtualListDataSource == null || modelObject == null) { return(null); } int index = VirtualListDataSource.GetObjectIndex(modelObject); return(index >= 0 ? GetItem(index) : null); }
/// <summary> /// Return the model object at the given index /// </summary> /// <param name="index">Index of the model object to be returned</param> /// <returns>A model object</returns> public override object GetModelObject(int index) { if (VirtualListDataSource != null && index >= 0 && index < GetItemCount()) { return(VirtualListDataSource.GetNthObject(index)); } else { return(null); } }
/// <summary> /// Handle the SearchForVirtualList event, which is called when the user types into a virtual list /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected virtual void HandleSearchForVirtualItem(object sender, SearchForVirtualItemEventArgs e) { // The event has e.IsPrefixSearch, but as far as I can tell, this is always false (maybe that's different under Vista) // So we ignore IsPrefixSearch and IsTextSearch and always to a case insensitve prefix match. // We can't do anything if we don't have a data source if (VirtualListDataSource == null) { return; } // Where should we start searching? If the last row is focused, the SearchForVirtualItemEvent starts searching // from the next row, which is actually an invalidate index -- so we make sure we never go past the last object. int start = Math.Min(e.StartIndex, VirtualListDataSource.GetObjectCount() - 1); // Give the world a chance to fiddle with or completely avoid the searching process BeforeSearchingEventArgs args = new BeforeSearchingEventArgs(e.Text, start); OnBeforeSearching(args); if (args.Canceled) { return; } // Do the search int i = FindMatchingRow(args.StringToFind, args.StartSearchFrom, e.Direction); // Tell the world that a search has occurred AfterSearchingEventArgs args2 = new AfterSearchingEventArgs(args.StringToFind, i); OnAfterSearching(args2); // If we found a match, tell the event if (i != -1) { e.Index = i; } }
/// <summary> /// Select the rows that is displaying any of the given model object. All other rows are deselected. /// </summary> /// <param name="modelObjects">A collection of model objects</param> /// <remarks>This method has O(n) performance where n is the number of model objects passed. /// Do not use this to select all the rows in the list -- use SelectAll() for that.</remarks> public override void SelectObjects(IList <object> modelObjects) { // Without a data source, we can't do this. if (VirtualListDataSource == null) { return; } SelectedIndices.Clear(); if (modelObjects == null) { return; } foreach (object modelObject in modelObjects) { int index = VirtualListDataSource.GetObjectIndex(modelObject); if (index >= 0 && index < VirtualListSize) { SelectedIndices.Add(index); } } }
/// <summary> /// Find the first row in the given range of rows that prefix matches the string value of the given column. /// </summary> /// <param name="text"></param> /// <param name="first"></param> /// <param name="last"></param> /// <param name="column"></param> /// <returns>The index of the matched row, or -1</returns> protected override int FindMatchInRange(string text, int first, int last, OLVColumn column) { return(VirtualListDataSource.SearchText(text, first, last, column)); }