[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 216
  • Last Modified:

How to fit all the pieces of my TStringgrid Puzzle?

Hi Folks!

I have a
TStringgrid (SG1),
2 FileListbox (LBWZ and LBC), 1 holds the txt files the other holds the ini files my Stringgrid uses
a Combobox (CBWord),  to display the item to be searched
a Timer (Timer1),
a TMemo (mmoData) to log all
  and some terrific pieces of code coming from here. Now I have some trouble visualizing all that happens due to all that occurs in response to the events. I have been walking through my code over and over again but I still find it all quite complicated to understand.
Nevertheless I almost got it working but I would like to change that almost into completely and therefore I've come to visit EE once again.


So actually I'm asking you to check this code and point out where I go wrong assuming the following is my goal:
This is what I want my app to do:

After a button click the TStringgrid should load the contents of the first file of the FileListBox to fill its cells and it should load the first file of another FileListBox which provides the coordinates of the cell it should monitor. These two files are loaded as a pair and I gave them the same name but changed the extension. The .txt file is filled with 400 characters and the .ini file is filled with 20 integers from top to bottom being the x coordinate then y coordinate and so on.
 This works, after I posted my questions here I got several brilliant replies and I managed to glue all together.
After the contents is loaded into the TSTringgrid, I want my timer to be enabled with an interval of 2 minutes. If one of the predefined cells (all coordinates provided by the second file I mentioned) is not clicked, the timer should instruct my app to load the second file of the FileListBox and its corresponding .ini file from the second FileListBox. IF again no cell is clicked within 2 minutes, the timer should instruct my app to load the third item and so on until it reaches the 6th file cause that means end of play.
If the user clicks one predefined cell within 2 minutes, it should halt the timer, record the time it took and then instruct my app to load the next file of the FIleListBox. Now for each of the 6 files, I have some word I want displayed right above the Tstringgrid, that would be the word the user has to search.
Now I want my app to log which file it loaded, at what time it was loaded and whether the user found the word within the 2 minutes.

Here's 'where I am' currently >


// Load .txt file and corresponding .ini file from filelistboxes into SG1 >

procedure TForm1.LoadMatrix(Sender: TObject);
var
  f : file;
  arr : array of char;
  n : cardinal;
  ig: integer;
  fCells: TextFile;
  str: string;
begin
SG1.Enabled := True;

 // This is my attempt to force the app to load in a specific order, I also named the files from 01 to 06 >

AssignFile(f, LBWZ.items.Strings[lbwz.itemindex]);
FileMode := fmOpenRead;
reset(f, 1);
SetLength(arr, filesize(f));
BlockRead(f, arr[0], length(arr));
CloseFile(f);
for n := 0 to high(arr) do SG1.Cells[n mod 20, n div 20] := arr[n];

// Here's where I feed it the corresponding .ini file according to the first FileListBox index > this should ensure the right .ini file
// was loaded with the right .txt file since they should act like a pair >

AssignFile(fCells,LBC.Items.Strings[lbwz.itemindex]);
  Reset(fCells);
  for ig := 0 to 9 do
    begin
    readln(fCells,str);
    letters[ig].X := StrToIntDef(str,-1);
    readln(fCells,str);
    letters[ig].Y := StrToIntDef(str,-1);
    end;
  CloseFile(fCells);

// Here's where I want it to log which file it loaded at what time

  mmoData.Lines.Add(LBWZ.Items.Strings[LBWZ.ItemIndex]+ ' om: '+TimeToStr(Now)+' gestart');

 // This was my first attempt to instruct it to load the next item from the FileListBox, I commented it out of my code and then
// moved it downwards as you may see but that was a shot in the dark as well    
 
       //LBWZ.ItemIndex := LBWZ.ItemIndex + 1;

// Following was my attempt to make it skip the first two items of the FileListBox > The timer should only be enabled starting
// the third item from the FileListBox

       if LBWZ.ItemIndex > 1 then
      Timer1.Enabled := True;

// This is where I moved the code I mentioned I had commented out

        lbwz.ItemIndex := lbwz.itemindex + 1;

// This is my attempt to have it change the word displayed above the STringgrid > I used a combobox with predefined items
// and my goal was to set the index to the index of the FileListBoxes so that when item 3 of FileListBox is loaded, item 3 of the
// Combobox was displayed simultaneously, this seems to work currently

        CBWORD.ItemIndex := CBWORD.ItemIndex + 1;

end;

// When a cell of Stringgrid SG1 is clicked

procedure TForm1.SG1SelectCell(Sender: TObject; ACol, ARow: Integer;var CanSelect: Boolean);
  var i: integer;
begin
  for i := 0 to 9 do
    if (letters[i].X = ACol) and (letters[i].Y = ARow) then
    begin

// My attempt to ring a bell or something, since the user found one of the predefined cells and clicked
// it > all of this should be logged and then the next item from the FileListBox should be loaded

    SG1.Enabled := False;

// The timer should be disabled I reckon when the user clicked one of the predefined cells.

     Timer1.Enabled := False;
        EndTime := Now;

// Log all to the memo > this results in duplicate entries but so far I have been unable to amputate a part of it without
// scr*wing up most of the logging that is done

      mmoData.Lines.Add(LBWZ.Items.Strings[LBWZ.ItemIndex]+ ' om: '+TimeToStr(Now)+' gevonden'+' na: ' +DurationAsString(EndTime - StartTime));

// Make all stop when it reaches the 6th item of the FileListBox, until then LoadMatrix

        if lbwz.ItemIndex < 5 then  
            LoadMatrix(Sender);
     
end;
end;

//OnTimer event

procedure TForm1.Timer1Timer(Sender: TObject);
begin
 if (Now > ExpectedEndTime) then
        begin
            Timer1.Enabled := False;

// Every time this occurs means the user did not find the word

            mmoData.Lines.Add('Woord niet geraden!');
            if lbwz.ItemIndex < 4 then
            LoadMatrix(Sender);

// The following I commented out even though the DurationAsStringieThingie works perfect in conjunction with
// the ExpectedEndTime and Now values.

       // end
       // else
         // mmoData.Lines.Add(DurationAsString(ExpectedEndTime - Now));
end;
//

IF its necessary to post the entire unit, please let me know and I will. I could also upload the sourcecode if necessary.
If you need more info, please let me know > although my commenting is more likely to make a bigger mess than anything else, it is also my attempt to understand what the H*ll is happening out here in my code.
Feel free to correct me when and whereever I am wrong. It would be great when you would help me understand why something doesn't work the way I expected it to, but mind me, this will require some patience coming from you. Those who already helped me here on EE stimulated me to keep on asking and asking so that's what I'm doing currently. I'm already proud of what I accomplished and the person for whom I created this app expects it this week to start testing, so I won't give up now cause I'm close, very close to accomplishing my goal and helping her out :)

Using the LoadMatrix procedure, the OnTimer event and the StringgridSelectCell procedure, should be sufficient to get this thing going right?


Regards and respect,

Peter

0
PeterdeB
Asked:
PeterdeB
  • 4
1 Solution
 
PeterdeBAuthor Commented:
Hi Folks!

After some Delphi-ing I came up with this code to force my app to load files one after another:

//
// Load Matrix into SG1
procedure TForm1.LoadMatrix(Sender: TObject);
var
  f : file;
  arr : array of char;
  n : cardinal;
  ig: integer;
  fCells: TextFile;
  str: string;
begin
i := 1 + i;
str:= 'c:\woordzoeker\wz\'+inttostr(i)+'.txt';
SG1.Enabled := True;
AssignFile(f, str);
FileMode := fmOpenRead;
reset(f, 1);
SetLength(arr, filesize(f));
BlockRead(f, arr[0], length(arr));
CloseFile(f);
for n := 0 to high(arr) do SG1.Cells[n mod 20, n div 20] := arr[n];
AssignFile(fCells,str);
  Reset(fCells);
  for ig := 0 to 9 do
    begin
    readln(fCells,str);
    letters[ig].X := StrToIntDef(str,-1);
    readln(fCells,str);
    letters[ig].Y := StrToIntDef(str,-1);
    end;
  CloseFile(fCells);
        StartTime := Now; // set the start time
        ExpectedEndTime := Now + COUNTDOWN_TIME;
end;
//


This now ensures that it loads the first file first then the second then the third and so on :)

Regards,

Peter
0
 
PeterdeBAuthor Commented:
Oooeps in my enthousiasm I overlooked something > the second file loaded should be an .ini file instead of .txt :)

Peter
0
 
PeterdeBAuthor Commented:
I'm close to solving this myself. I already got the select-file-thingie working and the timer-thingie as well. I will post my code when done.

Regards,

Peter :)
0
 
PeterdeBAuthor Commented:
This is what I made of it, might not be brilliant but alas it works :) I'll ask for a refund and close this question.

// Load Matrix into SG1
procedure TForm1.LoadMatrix(Sender: TObject);
var
  f : file;
  arr : array of char;
  n : cardinal;
  ig,ind: integer;
  fCells: TextFile;
  str: string;
begin
ind := wz;
case ind of
0: str:= 'c:\woordzoeker\wz\01.txt';
1: str:= 'c:\woordzoeker\wz\02.txt';
2: str:= 'c:\woordzoeker\wz\03.txt';
3: str:= 'c:\woordzoeker\wz\04.txt';
4: str:= 'c:\woordzoeker\wz\05.txt';
5: str:= 'c:\woordzoeker\wz\06.txt';
6: str:= 'c:\woordzoeker\wz\07.txt';
7: str:= 'c:\woordzoeker\wz\08.txt';
else
Timer1.Enabled := False;
Exit;
end;
SGW1.Enabled := True;
LBLSEARCHWORD.Caption := CBWZT.Items.Strings[wz];
AssignFile(f, str);
FileMode := fmOpenRead;
reset(f, 1);
SetLength(arr, filesize(f));
BlockRead(f, arr[0], length(arr));
CloseFile(f);
for n := 0 to high(arr) do SGWZT.Cells[n mod 20, n div 20] := arr[n];
str:= ChangeFileExt(str, '.ini');
AssignFile(fCells,str);
  Reset(fCells);
  for ig := 0 to 19 do
    begin
    readln(fCells,str);
    letters[ig].X := StrToIntDef(str,-1);
    readln(fCells,str);
    letters[ig].Y := StrToIntDef(str,-1);
    end;
  CloseFile(fCells);
        StartTime := Now; // set the start time
        ExpectedEndTime := Now + COUNTDOWN_TIME;  // set the stop time > 2 minutes
      //  ShowMessage(IntTostr(wz));
        end;
0
 
LunchyCommented:
Closed, 250 points refunded.
Lunchy
Friendly Neighbourhood Community Support Admin
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now