Mirror

How to determine which combinations of rows and columns have been checked off in an array of TCheckBoxes (Views: 101)


Problem/Question/Abstract:

I have a form with checkboxes in 8 rows and 7 columns. I want to be able to determine which combinations of rows and columns have been checked off. Is the best way to do this using a 2-D array? How would I declare this array for the checkboxes?

Answer:

FArray: array[0..7, 0..6] of TCheckBox;

You also you have to create Checkboxes at runtime, like:

FArray[i, j] := TCheckBox.Create(Self);

I would suggest to use the Tag property instead. Assign the same OnClick event handler to all checkboxes and code the Tag for each checkbox. Say 23 (second column, third row), you know like matrix indices in math:

....OnClick(Sender: TObject);
var
  ATag: Integer;
begin
  if (Sender is TComponent) then
  begin
    ATag := TComponent(Sender).Tag;
    ShowMessage(Format('Column %d, Row %d', [ATag div 10, ATag mod 10]));
  end;
end;

<< Back to main page