Im Ereignis OnMeasureItem kann in einer ListBox nach dem
Hinzufügen eines neuen Items (invl. Objekt, z.B. einer Bitmap)
nicht auf das Objekt zugegriffen werden.
Mir hilfreich war
Peter Below (TeamB) sowie
dieser Code:
Zitat:
"Yes, this [Objekt nicht verfügbar nach Add] is an old problem caused by the fact that the AddObject method internally has to send two messages to the Windows listbox control. The first creates the item and sets it text, the second stores the object into the item. Unfortunately it is the first message (LB_ADDSTRING or LB_INSERTSTRING) that causes the WM_MEASUREITEM message to be end back, so the object has not been stored yet when the OnMeasureItem event fires. The best way to fix this is to simply ignore he OnMeasureItem event. Instead you measure the item immediately after the AddObject call and send a LB_SETITEMHEIGHT message to the listbox to tell it the height.
Peter Below (TeamB)
Delphi-Quellcode:
// Aufruf z.B. nach aListbox.Add
// => ListBoxRefreshItem(aListBox, aListBox.Items.Count-1);
// oder aListBox.Items.Move(oldPos, newPos);
// => ListBoxRefreshItem(aListBox, newPos);
procedure TXYZ.ListBoxRefreshItem(Control: TWinControl; index:
integer);
var
lb: TListBox;
H: integer;
begin
lb := TListBox(Control);
if lb.Style = lbStandard then exit;
ListBoxMeasureItem(lb, index, H); // Siehe unten
lb.Perform (LB_SETITEMHEIGHT, index, MAKELPARAM(H, 0));
lb.refresh;
end;
Delphi-Quellcode:
procedure THeavyLiftEditDlg.ListBoxMeasureItem(Control: TWinControl; Index:
Integer; var Height: Integer);
var
lb: TListbox;
Bitmap: TBitmap;
begin
if Index = -1 then exit;
lb := TListBox(Control);
Bitmap:= TBitmap( lb.Items.Objects[Index] )
// Measure hight according to available subitem entries.
with lb.Canvas do
begin
// Measure height sample
Font.Style := [fsBold];
Height := TextHeight('Wg') + 2 ;
Height := Bitmap.Height;
end;
end;