Forum > General

checkbox number

<< < (3/3)

howardpc:

--- Quote from: clemmi on August 03, 2014, 07:50:40 pm ---.. this code is a bit over my head. If you tell of a reference learning source I'll appreciate it.

--- End quote ---

There are several good tutorials in the Lazarus wiki, lots of Delphi books (which you can often pick up cheaply secondhand), and few Lazarus books.
I've added some comments.


--- Code: ---procedure TForm1.CreateCheckboxes;
var
  cb: TCheckBox;  // a variable to store a reference to a checkbox instance
  lastTop: integer=Margin;  // a variable that tracks the integer Top value, initialised to Margin
  i: Integer;  // a loop counter variable
begin
  for i:= 1 to MaxCheckBoxes do  // set up a for..do loop
  begin
    cb:=TCheckBox.Create(Self); // create a checkbox owned by the form (Self), so the form will be responsible for freeing it
    cb.Left:=Margin; // set several important checkbox display properties
    cb.Top:=lastTop;
    Inc(lastTop, cb.Height + Margin div 2); // change lastTop for the next checkbox in the loop
    cb.Caption:=Format('Checkbox #%d',[i]);  // set the checkbox caption to 'Checkbox #x' where x is replaced by the value of the loop counter
    cb.Tag:=i;  // store the identifying number of the checkbox in its Tag property
    cb.OnChange:=@GetCheckboxID;  // set the checkbox's OnChange property to the address of a procedure that will notify you when the checkbox changes
    cb.Parent:=Self; // set the checkbox's Parent property to the form, so the form will display it correctly
  end;
  Form1.Height:=lastTop; // ensure the form is long enough to show all the checkboxes
end;

--- End code ---

clemmi:
Thanks, I have a lot of books on Lazarus and Delphi. But, most of them cover only standard coding and not some more tricky or more advanced ones. If you know of a more advanced, but understandable, one let me know.

I found the Forum to be the main source for any code to do what I need. But it takes severals responses to get the one that works for me. At any rate I really apreciate all that responded and they have been a great help in getting my projects done.

I hope some day to know enough to pay back helping others.
-cl

mirce.vladimirov:

--- Quote from: Blaazen on August 02, 2014, 04:58:09 pm ---The most straightforward way to work with 50 checkboxes is to use TCheckGroup or TCheckListBox.

--- End quote ---

I join to Blaazen's advice : use  TCheckGroup or TCheckListBox.  If you do then you will be able to do :

--- Code: ---if CheckGroup1.Checked[0] then....;
if CheckGroup1.Checked[1] then....; 
if CheckGroup1.Checked[2] then....; 
if CheckGroup1.Checked[3] then....; 
if CheckGroup1.Checked[4] then....; 

--- End code ---

minesadorada:
I once wrote an golf app which had sets of checkboxes, labels and other controls - probably around a hundred in all.

I found it easiest to declare Arrays of controls, and create them dynamically at startup.  Easier to code, if the interactions are similar and also easier to maintain.

Navigation

[0] Message Index

[*] Previous page

Go to full version