我已经使用递归和条件调用达成了解决方案。我用这种结构创建了一条记录:
TGridKey = record
ID : Integer;
Index : Integer;
Files : TStringList;
end;
TTrialSet = record
theGrid : array of TGridKey;
end;
在 TForm 类中使用此名称。
TfRandStimuliSet = class (TForm)
//...
lst1: TListBox;
dlgOpenPic: TOpenPictureDialog;
private
FTrialSet: TTrialSet;
procedure TfRandStimuliSet.SubSetsMountEngine;
//...
end;
设置网格数组的长度:
NumComp := 3;
SetLength(FTrialSet.theGrid, NumComp);
for I := Low(FTrialSet.theGrid) to High(FTrialSet.theGrid) do
begin
FTrialSet.theGrid[I].ID := I;
FTrialSet.theGrid[I].Index:= -1;
FTrialSet.theGrid[I].Files := TStringList.Create;
end;
在每个“I”网格中放置一些字符串:
if dlgOpenPic.Execute then
begin
if dlgOpenPic.Files.Count > 0 then
for K := 0 to (dlgOpenPic.Files.Count - 1) do
begin
FTrialSet.theGrid[I].Files.Add(dlgOpenPic.Files.Strings[K]);
end;
dlgOpenPic.Files.Clear;
end;
然后程序:
procedure TfRandStimuliSet.SubSetsMountEngine;
var ID: integer; s1 : string;
procedure AddStmFromGrid(Grid, Stm : Integer);
begin
s1 := s1 + ExtractFileName(FTrialSet.theGrid[Grid].Files.Strings[Stm]) + ',';
end;
procedure AddTrialFromIndex; //each trial is the current index's
var I: Integer;
begin
for I:= Low(FTrialSet.theGrid) to High(FTrialSet.theGrid) do
AddStmFromGrid(I,FTrialSet.theGrid[I].Index);
lst1.Items.Add(s1);
s1:= '';
end;
procedure IndexReset(aGrid : Integer);
var i : Integer;
begin
for I := aGrid to (High(FTrialSet.theGrid)) do
FTrialSet.theGrid[I].Index := 0
end;
procedure IndexInc(aGrid : Integer);
begin
AddTrialFromIndex; //Save
Inc(FTrialSet.theGrid[aGrid].Index);
end;
function MoveGrid(var ID:integer): Boolean; //begin from right most, the highest grid
var IDMaxIndex, IDCurIndex, LowID, HighID: Integer;
begin
Result := True;
LowID := Low(FTrialSet.theGrid);
HighID := High(FTrialSet.theGrid);
//Application.ProcessMessages;
if (ID < LowID) then
begin
//ShowMessage('False');
AddTrialFromIndex;
Result := False;
end
else
begin
IDMaxIndex:= FTrialSet.theGrid[ID].Files.Count -1;
IDCurIndex := FTrialSet.theGrid[ID].Index;
if IDCurIndex = IDMaxIndex then
begin
ID := ID - 1;
Result:= MoveGrid(ID);//moveleft
Exit;
end;
if (ID < HighID)
and (IDCurIndex < IDMaxIndex) then
begin
IndexInc(ID); //increment/move donw
IndexReset(ID + 1); //reset everything on the right
MoveGrid(HighID); //move to the most right/next down
Exit;
end;
if (ID = (HighID))
and (IDCurIndex < IDMaxIndex) then
begin
IndexInc(ID); //increment/move down
MoveGrid(ID) //next increment/move down
end;
end;
end;
begin
ID := High(FTrialSet.theGrid);
IndexReset(Low(FTrialSet.theGrid)); //0's for everyone
MoveGrid(ID); //begin from the most right
end;