diff --git a/components/fpspreadsheet/source/common/fpspreadsheet.pas b/components/fpspreadsheet/source/common/fpspreadsheet.pas index 9d8fa1b2d..9e9625a4c 100644 --- a/components/fpspreadsheet/source/common/fpspreadsheet.pas +++ b/components/fpspreadsheet/source/common/fpspreadsheet.pas @@ -688,7 +688,7 @@ type {@@ Event procedure containing a specific worksheet } TsWorksheetEvent = procedure (Sender: TObject; ASheet: TsWorksheet) of object; - {@@ Event procedure called when a worksheet is removed } + {@@ Event procedure called when a worksheet is removed. ASheetIndex = -1 --> all sheets } TsRemoveWorksheetEvent = procedure (Sender: TObject; ASheetIndex: Integer) of object; @@ -8051,7 +8051,9 @@ end; -------------------------------------------------------------------------------} destructor TsWorkbook.Destroy; begin + DisableNotifications; RemoveAllWorksheets; + EnableNotifications; FWorksheets.Free; FCellFormatList.Free; @@ -8791,7 +8793,11 @@ end; -------------------------------------------------------------------------------} procedure TsWorkbook.RemoveAllWorksheets; begin + FActiveWorksheet := nil; FWorksheets.ForEachCall(RemoveWorksheetsCallback, nil); + FWorksheets.Clear; + if (FLockCount = 0) and Assigned(FOnRemoveWorksheet) then + FOnRemoveWorksheet(self, -1); end; {@@ ---------------------------------------------------------------------------- diff --git a/components/fpspreadsheet/source/visual/fpspreadsheetctrls.pas b/components/fpspreadsheet/source/visual/fpspreadsheetctrls.pas index 2cc5f6a2c..bb176aa40 100644 --- a/components/fpspreadsheet/source/visual/fpspreadsheetctrls.pas +++ b/components/fpspreadsheet/source/visual/fpspreadsheetctrls.pas @@ -1610,7 +1610,11 @@ begin else sheet := Workbook.GetWorksheetbyIndex(ASheetIndex); end else - sheet := FWorksheet; + if ASheetIndex > -1 then + sheet := FWorksheet + else + sheet := nil; // all sheets were removed + // FWorksheet := sheet; // is needed by listeners! NotifyListeners([lniWorksheetRemove]); SelectWorksheet(sheet); @@ -1882,6 +1886,9 @@ end; procedure TsCellEdit.DoEnter; begin + if Worksheet = nil then + exit; + if not CanEditCell(Worksheet.ActiveCellRow, Worksheet.ActiveCellCol) then begin MessageDlg('This cell is protected from editing. Unlock worksheet protection '+ diff --git a/components/fpspreadsheet/source/visual/fpspreadsheetgrid.pas b/components/fpspreadsheet/source/visual/fpspreadsheetgrid.pas index 25382a778..a78e11755 100644 --- a/components/fpspreadsheet/source/visual/fpspreadsheetgrid.pas +++ b/components/fpspreadsheet/source/visual/fpspreadsheetgrid.pas @@ -2242,6 +2242,8 @@ var TL: TPoint; begin inherited; + if Worksheet = nil then + exit; FTopLeft := CalcTopLeft(false); @@ -4351,6 +4353,9 @@ var clipArea: TRect; begin + if Worksheet = nil then + exit; + sr := GetWorksheetRow(ARow); scLastused := Worksheet.GetLastColIndex; gc := AFirstCol; @@ -4966,6 +4971,9 @@ var cell: PCell; r, c: Cardinal; begin + if Worksheet = nil then + exit; + if FAllowDragAndDrop and (not Assigned(DragManager) or not DragManager.IsDragging) and (ssLeft in Shift) and @@ -5016,6 +5024,9 @@ procedure TsCustomWorksheetGrid.MouseMove(Shift: TShiftState; X, Y: Integer); var prevMouseCell: TPoint; begin + if Worksheet = nil then + exit; + prevMouseCell := GCache.MouseCell; inherited; @@ -5264,7 +5275,7 @@ var cp: TsCellprotections; begin Result := inherited; - if Result and Worksheet.IsProtected then + if Result and Assigned(Worksheet) and Worksheet.IsProtected then begin cell := Worksheet.FindCell(GetWorksheetRow(ARow), GetWorksheetCol(ACol)); cp := Worksheet.ReadCellProtection(cell);