public static bool FOLVSaveState(FastObjectListView FOLV) { bool ret = false; try { FOLVObjectListViewState olvState = new FOLVObjectListViewState(); olvState.VersionNumber = 1; olvState.NumberOfColumns = FOLV.AllColumns.Count; // FOLV.AllColumns.Count olvState.CurrentView = FOLV.View; // If we have a sort column, it is possible that it is not currently being shown, in which // case, it's Index will be -1. So we calculate its index directly. Technically, the sort // column does not even have to a member of AllColumns, in which case IndexOf will return -1, // which is works fine since we have no way of restoring such a column anyway. if (FOLV.PrimarySortColumn != null) { olvState.SortColumn = FOLV.AllColumns.IndexOf(FOLV.PrimarySortColumn);// FOLV.AllColumns.IndexOf(FOLV.PrimarySortColumn) } olvState.LastSortOrder = FOLV.PrimarySortOrder; olvState.IsShowingGroups = FOLV.ShowGroups; // If FOLV.Columns.Count > 0 AndAlso FOLV.Columns(0).LastDisplayIndex = -1 Then 'FOLV.AllColumns(0).LastDisplayIndex = -1 // 'FOLV.RememberDisplayIndicies() // End If if (FOLV.Columns.Count == 0) { Log("Error: No columns to save?"); return(false); } foreach (OLVColumn column in FOLV.AllColumns) // FOLV.AllColumns { olvState.ColumnIsVisible.Add(column.IsVisible); olvState.ColumnDisplayIndicies.Add(column.LastDisplayIndex); olvState.ColumnWidths.Add(column.Width); } // Now that we have stored our state, save to json string Filename = Path.Combine(Path.GetDirectoryName(AppSettings.Settings.SettingsFileName), $"ListSaveState-{FOLV.Name}.JSON"); string json = Global.WriteToJsonFile <FOLVObjectListViewState>(Filename, olvState); if (json != null && !string.IsNullOrEmpty(json)) { ret = true; } } catch (Exception ex) { Log("Error: " + ex.Msg()); } return(ret); }
public static bool FOLVRestoreState(FastObjectListView FOLV) { bool Ret = false; try { string Filename = Path.Combine(Path.GetDirectoryName(AppSettings.Settings.SettingsFileName), $"ListSaveState-{FOLV.Name}.JSON"); if (!File.Exists(Filename)) { return(false); } FOLVObjectListViewState olvState = Global.ReadFromJsonFile <FOLVObjectListViewState>(Filename); // The number of columns has changed. We have no way to match old // columns to the new ones, so we just give up. if (olvState == null || olvState.NumberOfColumns != FOLV.AllColumns.Count) { Log("Debug: olvState Is null OrElse olvState.NumberOfColumns <> FOLV.AllColumns.Count."); return(false); } if (olvState.SortColumn == -1) { FOLV.PrimarySortColumn = null; FOLV.PrimarySortOrder = SortOrder.None; } else { FOLV.PrimarySortColumn = FOLV.AllColumns[olvState.SortColumn]; // FOLV.AllColumns(olvState.SortColumn) FOLV.PrimarySortOrder = olvState.LastSortOrder; } for (int i = 0; i <= olvState.NumberOfColumns - 1; i++) { OLVColumn column = FOLV.AllColumns[i]; // FOLV.AllColumns(i) column.Width = System.Convert.ToInt32(olvState.ColumnWidths[i]); column.IsVisible = System.Convert.ToBoolean(olvState.ColumnIsVisible[i]); column.LastDisplayIndex = System.Convert.ToInt32(olvState.ColumnDisplayIndicies[i]); } // ReSharper disable RedundantCheckBeforeAssignment if (olvState.IsShowingGroups != FOLV.ShowGroups) { // ReSharper restore RedundantCheckBeforeAssignment FOLV.ShowGroups = olvState.IsShowingGroups; } if (FOLV.View == olvState.CurrentView) { FOLV.RebuildColumns(); } else { FOLV.View = olvState.CurrentView; } //tell other functions not to resize columns unless forced FOLV.Tag = "resizedcols"; Ret = true; } catch (Exception ex) { Log("Error: " + ex.Msg()); } return(Ret); }