Forum > General

checkbox number

<< < (2/3) > >>

howardpc:
If you want the freedom of rolling your own routines, you could adapt the following. Add a form OnCreate handler to a new Lazarus project and copy this:


--- Code: ---unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, Forms, Dialogs, StdCtrls;

const MaxCheckBoxes = 10;
      Margin = 10;
type

  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  private
    procedure CreateCheckboxes;
    procedure GetCheckboxID(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.lfm}

procedure TForm1.CreateCheckboxes;
var
  cb: TCheckBox;
  lastTop: integer=Margin;
  i: Integer;
begin
  for i:= 1 to MaxCheckBoxes do
  begin
    cb:=TCheckBox.Create(Self);
    cb.Left:=Margin;
    cb.Top:=lastTop;
    Inc(lastTop, cb.Height + Margin div 2);
    cb.Caption:=Format('Checkbox #%d',[i]);
    cb.Tag:=i;
    cb.OnChange:=@GetCheckboxID;
    cb.Parent:=Self;
  end;
  Form1.Height:=lastTop;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  CreateCheckboxes;
end;

procedure TForm1.GetCheckboxID(Sender: TObject);
begin
  ShowMessageFmt('You clicked checkbox #%d',[TCheckBox(Sender).Tag]);
end;

end.


--- End code ---

dogriz:
You also can use "Tag" property:

--- Code: ---CheckBox5.Tag := 5;
CheckBox6.Tag := 6;
...
--- End code ---
And when you check the CheckBoxN, just put it's tag value N into a variable (array or something). When you uncheck, remove the value N from variable.

This solution may not bee too elegant, but is simple.

BigChimp:
@dogriz: have you looked at howardpc's example ;)

dogriz:

--- Quote from: BigChimp on August 03, 2014, 10:51:34 am ---@dogriz: have you looked at howardpc's example ;)

--- End quote ---
:) Obviously not... I even made an example app...

clemmi:
Thank you both for your code,

howardpc :  sorry, but this code is a bit over my head. If you tell of a reference learning source I'll appreciate it.

cb:=TCheckBox.Create(Self);
cb.Caption:=Format('Checkbox #%d',(i));
cb.Tag:=i;
cb.OnChange:=@GetCheckboxID;
cb.Parent:=Self;
... ,[TCheckBox(Sender).Tag]);
---------------------------------------------------------------------
dogriz: your example application is probably just what I needed. I think I can adapt some of the code to my application. Thanks!

I'm not too familiar with this particular code (with sender ...),

     procedure TForm1.CheckBox1Click(Sender: TObject);
         begin
         with Sender as TCheckBox do
         CheckedCheckBoxes[Tag] := Checked;
         end;

Still, I can experiment with it and definitely use it.

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version