How to fit all the pieces of my TStringgrid Puzzle?
Posted on 2005-04-15
I have a
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);
f : file;
arr : array of char;
n : cardinal;
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 >
FileMode := fmOpenRead;
BlockRead(f, arr, length(arr));
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 >
for ig := 0 to 9 do
letters[ig].X := StrToIntDef(str,-1);
letters[ig].Y := StrToIntDef(str,-1);
// 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;
// When a cell of Stringgrid SG1 is clicked
procedure TForm1.SG1SelectCell(Sender: TObject; ACol, ARow: Integer;var CanSelect: Boolean);
var i: integer;
for i := 0 to 9 do
if (letters[i].X = ACol) and (letters[i].Y = ARow) then
// 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
procedure TForm1.Timer1Timer(Sender: TObject);
if (Now > ExpectedEndTime) then
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
// The following I commented out even though the DurationAsStringieThingie works perfect in conjunction with
// the ExpectedEndTime and Now values.
// mmoData.Lines.Add(DurationAsString(ExpectedEndTime - Now));
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,