Fixing errors with TCheckListBox in C++Builder requires addressing distinct behaviors related to indexing, event tracking, and memory management that differ from standard C++ containers. Because TCheckListBox is a VCL component originating from Delphi, it uses 0-based indexing for arrays but relies on underlying Delphi logic that can trip up C++ developers.
The most common TCheckListBox errors, their underlying causes, and direct methods to fix them are detailed below. 1. Access Violation (Out of Bounds) During Loops
The Error: An Access Violation or Argument out of range runtime crash happens when iterating through items.
The Cause: Using standard C++ style loops like <= Items->Count. Since the container uses 0-based indexing, the final valid item is Items->Count - 1.
The Fix: Ensure loop conditions strictly use the less-than operator (<).
// CORRECT: Loop stops before matching the Count value for (int i = 0; i < CheckListBox1->Items->Count; i++) { if (CheckListBox1->Checked[i]) { // Process checked item safely } } Use code with caution. 2. State Mismatch Using OnClick Instead of OnClickCheck
The Error: Code meant to handle checked items fails to fire, or reads an outdated check state when a user clicks the square box.
The Cause: Hooking logic into the standard OnClick event. OnClick triggers when the text row is selected, but it often misses state changes caused by direct checkbox clicks or double-clicks.
The Fix: Use the TCheckListBox::OnClickCheck event handler. This event is built explicitly to track alterations to the checkbox state. 3. Missing Item Text via Checked Index
The Error: Compilation fails with an error stating Checked does not contain string data.
The Cause: Confusing the boolean Checked property array with the string Items array. Checked[i] only returns true or false.
The Fix: Cross-reference the index with the Items property to extract text.
if (CheckListBox1->Checked[i]) { UnicodeString itemText = CheckListBox1->Items->Strings[i]; } Use code with caution. 4. Index Drift with Sorted = true
The Error: Checking an item updates or reads the wrong item’s state after a new string is added programmatically.
The Cause: When TCheckListBox::Sorted is set to true, adding a new string causes the VCL to automatically re-order the entire list alphabetically. Hardcoded indices point to completely different text strings after sorting.
The Fix: Capture the new index instantly upon adding an item rather than relying on a static index tracker.
// Add returns the finalized index after alphabetical sorting takes place int actualIndex = CheckListBox1->Items->Add(“New Alphabetical Item”); CheckListBox1->Checked[actualIndex] = true; Use code with caution. 5. Memory Leaks with AddObject
The Error: The application consumes extra RAM over time, or throws an access violation on shutdown.
The Cause: Attaching custom C++ objects to items using Items->AddObject(). Clearing the TCheckListBox or deleting an item removes the visual row but leaves the custom C++ object allocated in heap memory.
The Fix: Explicitly loop through the items to delete pointer data before clearing the box.
for (int i = 0; i < CheckListBox1->Items->Count; i++) { TMyCustomClassobj = static_cast Use code with caution. 6. Empty Selection Crashes via ItemIndex Experts Exchange TCheckListBox in Borland C++ Builder – Experts Exchange
Leave a Reply