Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to handle hundreds of checkboxes 1

Status
Not open for further replies.

mschlawin

MIS
Aug 11, 1998
5
US
Hello,
I'm using Delphi 7. I have a form with over one hundred checkboxes layed out on a diagram of seats in an auditorium. A check means the seat is taken, unchecked means it is open. The checkbox components are named by the seat name, A1, A2...A34,B1,B2,...B36,C1...etc so form1.a1.checked:=true means seat A1 is taken.

I need to read all of the checkbox components and tell if they are checked and I need to modify a component's "checked" property. How do I do this without writing a line of code for every component? (I'll have 900 seats total!) I do have an array with every seat name in it.

My ideas: (that I cannot get to work!)

1. Put all of the checkbox components into an array so I can loop through the array and search or modify the "checked" property.
2. Refer to the component by a variable name. Since I have all seat names in an array, can I somehow refer to

form.A2.checked (but replace the A2 with a variable with a value of 'A2'?)

3. Use pointers. I found the componentcount and componentsindex and components[x].name properties. I can loop from 0 to componentcount and display every component name (seat name) but I cannot figure out how to change the "checked" property of say form1.components[3].componentname

I'm fairly certain this can be done, I just need a little help!

Thanks in advance.

Matt
 
i did a similar thing but with buttons.

var SeatArray [1..900] of TCheckbox;

in form create assign the boxes to the array;

SeatArray [1]:=A1;
SeatArray [2]:=A2;
ect...
of do a form.findcomponent loop

now you can address the Checkbox in loops ect.

for i:=1 to 20 do SeatArray.checked:=false;
for i:=21 to 40 do SeatArray.checked:=false;

you get the point.

in your case an array for each row may be better

SeatingRowAarray [1..34] of TCheckbox;
SeatingRowBarray [1..36] of TCheckbox;
SeatingRowCarray [1..38] of TCheckbox;

thats my opinion, someone else may have a better option.

Aaron
 
Aaron,
Thanks for you input. I've been trying for the last couple of days what you suggested , but I'm having problems.

First I declared types like this:
type SeatType=record
Name:string[5];
chkbox:TCheckBox;
end;

BalconySeatType=array[1..TotalBalconySeats] of SeatType;

and a var like this:
BalconySeat:BalconySeatType;

I load balconyseat.Name (like AA1,AA2,etc) from a text file.

Then I use the following code to set each checkbox equal:
for x:=1 to Totalbalconyseats do
balconyseat[x].chkbox:=balcony.FindComponent(balconyseat[x].Name) as Tcheckbox;

This compiles and runs, but whenever I try to access the data at all, like:
if (balconyseat[x].chkbox.Checked=true)
the compiler immediately locks tight trying to execute that line. I cannot stop the program from running and I need to actually do a "end task" on Delphi itself.

So I thought the problem might be in the assignment and type cast. I manually added 10 seats like this:
balconyseat[1].chkbox:=balcony.AA1;
balconyseat[2].chkbox:=balcony.AA2;
balconyseat[3].chkbox:=balcony.AA3;...

These compile, but it does the same thing. As soon as I try any operation on balconsyseat[x].chkbox the entire program and compiler freeze hard.

Does anything here look funky to you?

Thanks again!

Matt
 
You should not compare a Checked property to True, it is either True or False, so a plain "IF blabla.Checked THEN" would suffice.

HTH
TonHu
 
Thanks for the help.

That was actually the way I first had it. When it kept crashing, I added the = true. Then I added the parenthesis. All of them give the same results.

Matt
 
tested it and this works:
new form with 2 checkboxes and 2 buttons
click button1 to load array
click button2 to flip checkbox2

Code:
unit Unit2;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, IdzlibEx;

type
  TForm2 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    CheckBox1: TCheckBox;
    CheckBox2: TCheckBox;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

type  SeatType=record
      Name:string[5];
      chkbox:TCheckBox;
      end;

BalconySeatType=array[1..2] of SeatType;

var
  Form2: TForm2;
  BalconySeat:BalconySeatType;

implementation

{$R *.dfm}

procedure TForm2.Button1Click(Sender: TObject);
begin
 BalconySeat[1].chkbox:=Checkbox1;
 BalconySeat[2].chkbox:=Checkbox2;
end;

procedure TForm2.Button2Click(Sender: TObject);
begin
 BalconySeat[2].chkbox.Checked:=not BalconySeat[2].chkbox.Checked;
end;
end.

I think the problem lies elsewhere. maybe you have names that don't exist.

check your object values:

Code:
if not assigned(balconyseat[x].chkbox) then
 showmessage('you got a problem here!');

Greetings,
Daddy


-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
Daddy,

You are a genius! I double checked the .csv file with all 902 seat names and locations and I found a one character typo. (Seat A17 should have been A117 !!) I corrected it and everything is working perfectly.

I compiled your code and it is doing just what I want.

Thanks tons!

Matt
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top