Tchecklistbox question lazarus / 31-LCL ---------------------------------------------------------------------- rwebb616 · 2021-05-19 02:18 Is there a way to differenciate between the user clicking the checkbox vs just selecting something in the list?  I am using a checklist box right now and I put a on-click procedure to make the selections easier so if you click any item it will reverse the checked state of the item.  The problem is if I click on the checkbox itself it will for example turn on and then back off or off and then back on depending on it's state because on itemclick is running as is on click. Any way to tell if it's an item click vs selection? Rich ---------------------------------------------------------------------- Handoko · 2021-05-19 02:39 Quote from: rwebb616 on May 19, 2021, 04:18:57 amI put a on-click procedure to make the selections easier so if you click any item it will reverse the checked state of the item. You don't need to write code for the OnClick event to reverse the state. TCheckListBox will do it automatically for you. Or maybe I didn't fully understand your explanation. Can you explain more or perhaps provide a simplified demo that can show us the issue? ---------------------------------------------------------------------- rwebb616 · 2021-05-19 02:47 I'm sorry ... I wasn't clear.. I need to check these relatively fast and having to have the mouse directly on the checkbox was problematic so I had coded the on-click event to check or uncheck the box so all I had to do was click the item rather than being right on the checkbox.. but then I had the unintended behavior that when I DO click the checkbox it reverses whatever state so if I click to check it, it checks with the item selection event and then the checkbox unchecks because the checkbox was clicked.  I just got it sorted though .. Also I forgot to mention that I can only select up to 6 items.  I did this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TfMainForm.clbCategoriesClick(Sender: TObject);var  index:integer;  i:integer;  checked:integer;begin  checked:=0;  index := clbcategories.itemindex;  clbCategories.Checked[index]:=not clbCategories.Checked[index];  for i:=0 to clbcategories.Count -1 do begin    if clbCategories.Checked[i] then inc(checked);  end;  if checked > options.maxcategories then begin  MessageDlg('Error','You can only select up to ' + options.maxcategories.tostring + ' categories!',mtError,[mbOK],'');  if clbCategories.checked[Index] then    clbCategories.Checked[Index]:=not clbCategories.checked[Index];  end;end;   and I assigned this to two events - onclick and onclickcheck.  Because it runs the same code the checkbox reverses state I guess 3 times.  Once for the actual check being clicked, then once for each event. Ultimately it ends up in the state I want it to be in so now I can click either the checkbox or the item and it will limit me to 6 categories. Rich ---------------------------------------------------------------------- Handoko · 2021-05-19 03:05 I tested your code on both Windows and GTK2, I saw the problem. That issue does not happen on Linux GTK2. Give me some time, I am working on it. ---------------------------------------------------------------------- rwebb616 · 2021-05-19 03:10 Quote from: Handoko on May 19, 2021, 05:05:15 amI tested your code on both Windows and GTK2, I saw the problem. That issue does not happen on Linux GTK2. Give me some time, I am working on it. The code I posted is actually working for what I'm doing - or do you mean the original problem I posted about? Rich ---------------------------------------------------------------------- Handoko · 2021-05-19 03:21 The code you posted has problem on Win7 but it worked perfectly on Ubuntu Mate GTK2. Here is my workaround, which quickly tested on both Linux and Windows: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit Unit1; {$mode objfpc}{$H+} interface uses  SysUtils, Forms, Dialogs, CheckLst; type   { TForm1 }   TForm1 = class(TForm)    CheckListBox1: TCheckListBox;    procedure CheckListBox1Click(Sender: TObject);    procedure CheckListBox1ClickCheck(Sender: TObject);  end; var  Form1: TForm1; implementation {$R *.lfm} { TForm1 } procedure TForm1.CheckListBox1Click(Sender: TObject);const  MaxCategories = 3;var  Index:   Integer;  i:       Integer;  Checked: integer;begin  Index   := CheckListBox1.ItemIndex;  Checked := 0;  CheckListBox1.Checked[Index] := not CheckListBox1.Checked[Index];  for i := 0 to CheckListBox1.Count-1 do    if CheckListBox1.Checked[i] then Inc(Checked);  if Checked > MaxCategories then begin    MessageDlg('Error', 'You can only select up to ' + MaxCategories.ToString +      ' categories!', mtError, [mbOK], '');  if CheckListBox1.Checked[Index] then      CheckListBox1.Checked[Index] := not CheckListBox1.Checked[Index];  end;end; procedure TForm1.CheckListBox1ClickCheck(Sender: TObject);var  Index: Integer;begin{$IfDef Linux}  Exit;{$EndIf}  Index := CheckListBox1.ItemIndex;  CheckListBox1.Checked[Index] := not CheckListBox1.Checked[Index];end; end.