/// <summary> /// Create slices appropriate for current root object and layout, reusing any existing slices, /// and clearing out any that remain unused. If it is for a different object, reuse is more limited. /// </summary> private void CreateSlices(bool differentObject) { var watch = new Stopwatch(); watch.Start(); bool wasVisible = this.Visible; var previousSlices = new ObjSeqHashMap(); int oldSliceCount = Slices.Count; ConstructingSlices = true; try { // Bizarrely, calling Hide has been known to cause OnEnter to be called in a slice; we need to suppress this, // hence guarding it by setting ConstructingSlices. Hide(); if (m_currentSlice != null) m_currentSlice.SetCurrentState(false); // needs to know no longer current, may want to save something. m_currentSlice = null; if (differentObject) m_currentSliceNew = null; //if (differentObject) // Slices.Clear(); var dummySlices = new List<Slice>(Slices.Count); foreach (Slice slice in Slices) { slice.Visible = false; if (slice.Key != null) // dummy slices may not have keys and shouldn't be reused. previousSlices.Add(slice.Key, slice); else dummySlices.Add(slice); } bool gonnerHasToolTip = false; // Does any goner have one? // Get rid of the dummies we aren't going to remove. foreach (Slice slice in dummySlices) { gonnerHasToolTip |= slice.ToolTip != null; RemoveSlice(slice); } previousSlices.ClearUnwantedPart(differentObject); CreateSlicesFor(m_root, null, m_rootLayoutName, m_layoutChoiceField, 0, 0, new ArrayList(20), previousSlices, null); // Clear out any slices NOT reused. RemoveSlice both // removes them from the DataTree's controls collection and disposes them. foreach (Slice gonner in previousSlices.Values) { gonnerHasToolTip |= gonner.ToolTip != null; RemoveSlice(gonner); } if (gonnerHasToolTip) { // Since the dumb MS ToolTip class can't just remove one, // we have to remove them all and re-add the remaining ones // in order to have it really turn loose of the SliceTreeNode. m_tooltip.RemoveAll(); foreach (Slice keeper in Slices) SetToolTip(keeper); } ResetTabIndices(0); } finally { ConstructingSlices = false; } if (wasVisible) Show(); watch.Stop(); // Uncomment this to investigate slice performance or issues with dissappearing slices //Debug.WriteLine("CreateSlices took " + watch.ElapsedMilliseconds + " ms. Originally had " + oldSliceCount + " controls; now " + Slices.Count); //previousSlices.Report(); }