Welcome to the Tweaking4All community forums!
When participating, please keep the Forum Rules in mind!
Topics for particular software or systems: Start your topic link with the name of the application or system.
For example “MacOS X – Your question“, or “MS Word – Your Tip or Trick“.
Please note that switching to another language when reading a post will not bring you to the same post, in Dutch, as there is no translation for that post!
[Solved] Lazarus/Delphi - TCheckListBox - Select only one
Delphi, Lazarus, Free Pascal
(@hans)
Famed Member Admin
Joined: 12 years ago
Posts: 2859
Topic starter
June 19, 2014 5:37 AM
The TCheckListBox allows the user to check one or more items in the list, but sometimes we want the user to select only one item.
The propery "MultiSelect" is misleading in this case, as it refers to selection and not checking an item.
With a simple piece of code we can accomplish the selection of only one item though.
Assign this to the "OnItemClick" event:
procedure TfmSelectFile.MyCheckListBoxItemClick(Sender: TObject; Index: integer);
var
Counter : integer;
MyCheckListBox : TCheckListBox; // you can also use the name of the actual TCheckListBox
begin
MyCheckListBox:=TCheckListBox(Sender);
for Counter:= 0 to MyCheckListBox.Items.Count-1 do
begin
if (Counter<>Index) then
MyCheckListBox.Checked[Counter]:= false;
end;
end;
This procedure simply sets all checkboxes to NOT checked, except the one that matches the Index.