Recent

Author Topic: checkbox number  (Read 7457 times)

clemmi

  • Jr. Member
  • **
  • Posts: 54
checkbox number
« on: August 02, 2014, 03:34:58 pm »
I have about 50 checkBoxes. How can I get the number of the checkbox checked? During runtime of course.
For instance, if CheckBox5 in checked.
Can I get a variable to store the number 5 when the checkbox is checked, so I can use it in the program?
I don't need to know which one is checked, I need the number in a variable when the checkbox is checked.
Thanks!
-cl
 
« Last Edit: August 02, 2014, 04:42:43 pm by clemmi »

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: checkbox number
« Reply #1 on: August 02, 2014, 03:43:43 pm »
So have you searched the wiki and forum for those keywords?

Using a search engine to search for getcontrolindex and Lazarus leads e.g. here
http://lazarus-ccr.sourceforge.net/docs/lcl/controls/twincontrol.getcontrolindex.html

Otherwise you could search for "enumerating controls" or "iterating through controls" or something similar...

edit... and you find posts like
http://forum.lazarus.freepascal.org/index.php/topic,20236.msg116561.html#msg116561
which may be helpful...
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

clemmi

  • Jr. Member
  • **
  • Posts: 54
Re: checkbox number
« Reply #2 on: August 02, 2014, 04:40:50 pm »
thanks for the answer but it didn't help.
I removed those trials words since it may not be what I need.
-cl

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: checkbox number
« Reply #3 on: August 02, 2014, 04:49:39 pm »
thanks for the answer but it didn't help.
I removed those trials words since it may not be what I need.
Sorry, no idea what "removing those trials words" means. Did you see the link to the forum post I posted? It iterates through some controls:
Code: [Select]
var i: Integer;
begin
  for i:=0 to TabSheet1.ControlCount-1 do
    if TabSheet1.Controls[i] is TEdit then ... ;
... well, you're probably not looking at a tabsheet but a form and you're looking for a radio button not a Tedit, so try something like
Code: [Select]
var i: Integer;
begin
  for i:=0 to Self.ControlCount-1 do
    if Self.Controls[i] is TRadiobutton then ...
... something with Self.Controls[i].ControlName or something or perhaps .Name... this is air code...
Note: I made this code up. Don't know if using Self for the form reference is what should be done here.
No idea if TRadioButton if the correct name, but Lazarus provides autocomplete so you should be able to figure out what to use...
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

Blaazen

  • Hero Member
  • *****
  • Posts: 2782
  • POKE 54296,15
    • Eye-Candy Controls
Re: checkbox number
« Reply #4 on: August 02, 2014, 04:58:09 pm »
The most straightforward way to work with 50 checkboxes is to use TCheckGroup or TCheckListBox.
Lazarus 2.1.0 r59757M FPC 3.3.1 r40507 x86_64-linux-qt Chakra, Qt 4.8.7/5.11.2, Plasma 5.14.2
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

howardpc

  • Hero Member
  • *****
  • Posts: 3176
Re: checkbox number
« Reply #5 on: August 02, 2014, 05:24:16 pm »
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: [Select]
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.


dogriz

  • Full Member
  • ***
  • Posts: 105
    • Tech blog - Delphi, Lazarus, Firebird, Windows, Linux, Android...
Re: checkbox number
« Reply #6 on: August 03, 2014, 10:50:21 am »
You also can use "Tag" property:
Code: [Select]
CheckBox5.Tag := 5;
CheckBox6.Tag := 6;
...
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.
FPC 3.0.4
Lazarus 2.0.2
Debian x86_64, arm

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: checkbox number
« Reply #7 on: August 03, 2014, 10:51:34 am »
@dogriz: have you looked at howardpc's example ;)
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

dogriz

  • Full Member
  • ***
  • Posts: 105
    • Tech blog - Delphi, Lazarus, Firebird, Windows, Linux, Android...
Re: checkbox number
« Reply #8 on: August 03, 2014, 11:08:38 am »
@dogriz: have you looked at howardpc's example ;)
:) Obviously not... I even made an example app...
FPC 3.0.4
Lazarus 2.0.2
Debian x86_64, arm

clemmi

  • Jr. Member
  • **
  • Posts: 54
Re: checkbox number
« Reply #9 on: August 03, 2014, 07:50:40 pm »
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.


howardpc

  • Hero Member
  • *****
  • Posts: 3176
Re: checkbox number
« Reply #10 on: August 03, 2014, 10:41:17 pm »
.. this code is a bit over my head. If you tell of a reference learning source I'll appreciate it.

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: [Select]
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;

clemmi

  • Jr. Member
  • **
  • Posts: 54
Re: checkbox number
« Reply #11 on: August 07, 2014, 09:56:06 pm »
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

  • Full Member
  • ***
  • Posts: 220
Re: checkbox number
« Reply #12 on: August 08, 2014, 12:14:16 am »
The most straightforward way to work with 50 checkboxes is to use TCheckGroup or TCheckListBox.

I join to Blaazen's advice : use  TCheckGroup or TCheckListBox.  If you do then you will be able to do :
Code: [Select]
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....; 

minesadorada

  • Hero Member
  • *****
  • Posts: 588
  • Retired
Re: checkbox number
« Reply #13 on: August 08, 2014, 01:21:59 am »
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.
GPL Apps: Health MonitorRetro Ski Run
OnlinePackageManager Components: LazAutoUpdate, LongTimer, PoweredBy, ScrollText, PlaySound, CryptINI