Page 1 of 1
Forum

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!



Share:
Notifications
Clear all

[Solved] Lazarus/Delphi - TCheckListBox - Select only one

1 Posts
1 Users
0 Reactions
4,226 Views
 Hans
(@hans)
Famed Member Admin
Joined: 12 years ago
Posts: 2859
Topic starter  

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.


   
ReplyQuote
Share: