Is there a bug in TListBox? lazarus / 31-LCL ---------------------------------------------------------------------- vfclists · 2010-05-03 19:10 This code is to scan the list of selected items in a List box and add them to a string list. It crashes when it gets to an unselected item. Am I doing something wrong here, or is it a known bug? Code: [Select]procedure TutilitiesInterfaceForm.GetEventsList(list: TStringList); var  i :integer; begin   for i := 0 to lbEventsToMonitorA.Items.Count - 1 do   begin     if lbEventsToMonitorA.Selected[i] = true then       list.Add(lbEventsToMonitorA.Items[i]);   end; end; ---------------------------------------------------------------------- Marc · 2010-05-03 21:56 on what line does it crash ? This code should work. ps. Code: [Select]if lbEventsToMonitorA.Selected[i] = true then is the same as Code: [Select]if lbEventsToMonitorA.Selected[i] then Selected[] already returns a boolean, no need to compare it against another boolean ---------------------------------------------------------------------- vfclists · 2010-05-03 23:45 It crashes on this line: Code: [Select]if lbEventsToMonitorA.Selected[i] = true then and the crash occurs when the item is not selected, ie always crashes on the first on selected item. Quote from: Marc on May 03, 2010, 11:56:05 pmon what line does it crash ? This code should work. ps. Code: [Select]if lbEventsToMonitorA.Selected[i] = true then is the same as Code: [Select]if lbEventsToMonitorA.Selected[i] then Selected[] already returns a boolean, no need to compare it against another boolean ---------------------------------------------------------------------- eny · 2010-05-04 04:46 Just a thought: is your listbox populated, the first time you call 'GetEventsList'? ---------------------------------------------------------------------- Marc · 2010-05-04 06:50 Quote from: eny on May 04, 2010, 06:46:43 amJust a thought: is your listbox populated, the first time you call 'GetEventsList'? if not populated, "for i := 0 to lbEventsToMonitorA.Items.Count - 1 do" wont do anything. ---------------------------------------------------------------------- Marc · 2010-05-04 06:51 Quote from: vfclists on May 04, 2010, 01:45:58 amIt crashes on this line: Code: [Select]if lbEventsToMonitorA.Selected[i] = true then and the crash occurs when the item is not selected, ie always crashes on the first on selected item. is lbEventsToMonitorA visible ? ---------------------------------------------------------------------- vfclists · 2010-05-04 07:17 It is populated at design time. As I said, it crashes when the index hits the first unselected item. Quote from: eny on May 04, 2010, 06:46:43 amJust a thought: is your listbox populated, the first time you call 'GetEventsList'? ---------------------------------------------------------------------- vfclists · 2010-05-04 07:19 Quote from: Marc on May 04, 2010, 08:51:16 amQuote from: vfclists on May 04, 2010, 01:45:58 amIt crashes on this line: Code: [Select]if lbEventsToMonitorA.Selected[i] = true then and the crash occurs when the item is not selected, ie always crashes on the first on selected item. is lbEventsToMonitorA visible ? It is visible, fully upfront. Something appears to be wrong with code generation. ---------------------------------------------------------------------- Bart · 2010-05-04 14:06 Similar code: Code: [Select]var   i: Integer; begin   for i := 0 to listbox1.Items.Count - 1 do     if listbox1.Selected[i] then       debugln('Selected[',dbgs(i),'] = True, Value = ',Listbox1.Items[i])     else       debugln('Selected[',dbgs(i),'] = False, Value = ',Listbox1.Items[i]); end; runs perfectly fine here. Q: Is list assigned (created) when you pass it to the procedure? Bart ---------------------------------------------------------------------- vfclists · 2010-05-05 05:16 I have checked and the code is correct. There were problems further down in the source code, but the debugger was executing them without moving the highlight to them. The highlight would be in one place where as the code was executing somewhere else. Is that a known feature? (Lazarus 0.9.28 Windows) ---------------------------------------------------------------------- Marc · 2010-05-05 19:46 Only if source and exe are out of sync. (iow, you edited the source without recompilation)