I have written a program to search for errors (according to a pattern) in the text. The error search is done programmatically from the text in SynEdit with the subsequent display of errors in StringGrid. How to make a synchronous change in the corresponding line of SynEdit when changing the text in StringGrid?
Open the file in SynEdit.
Create a temporary file from the text in SynEdit.
Load the temporary file (FileName.tmp) into TStringlist.
Searching for errors in the text from TStringlist.
Filling StringGrid.
procedure Tform1.SearchDetailErrors(Sender: TObject); var strlError: TStringlist; i: integer; str: string; begin if not DirectoryExists('Duplicates & Errors') then frmMain.CreateDirDuplicates(Sender); if frmMain.SynEdit.Lines.Count >= 1 then frmMain.SynEdit.Lines.SaveToFile('Duplicates & Errors\All Erros ' + ExtractFileName(frmMain.OpenTxtFileDlg.FileName) + '.tmp'); strlError := TStringList.Create; strlError.LoadFromFile('Duplicates & Errors\All Erros ' + ExtractFileName(frmMain.OpenTxtFileDlg.FileName) + '.tmp'); for i := 0 to strlError.Count - 1 do begin strlError.Strings[i] := Trim(strlError.Strings[i]); if CountPos('<', strlError.Strings[i]) <> 1 then begin str := IntToStr(i + 1) + ' | < | ' + strlError.Strings[i]; //str := IntToStr(i + 1) + ' < ' + strlError.Strings[i]; StrLDupl.AddStrings(str); str := ''; CPos := 0; end; if CountPos('>', strlError.Strings[i]) <> 1 then begin str := IntToStr(i + 1) + ' | > | ' + strlError.Strings[i]; StrLDupl.AddStrings(str); str := ''; CPos := 0; end; ... if Odd(CountPos('!', strlError.Strings[i])) then begin str := IntToStr(i + 1) + ' | ! | ' + strlError.Strings[i]; //str := IntToStr(i + 1) + ' " ' + strlError.Strings[i]; StrLDupl.AddStrings(str); str := ''; CPos := 0; end; ... StringGridOptions(Sender); end; procedure Tform1.StringGridOptions(Sender: TObject); begin with SGDetailErrors do ... begin Cells[0, 0] := 'Line № '; Cells[1, 0] := 'Error '; Cells[2, 0] := 'String'; end; FillStringGridResultDetailErrors(Sender); end; procedure Tform1.FillStringGridResultDetailErrors(Sender: TObject); var strlError: TStringList; i: Integer; s: String; begin strlError := TStringList.Create; try strlError.LoadFromFile('Duplicates & Errors\All Errors ' + ExtractFileName(frmMain.OpenTxtFileDlg.FileName) + '.txt'); strlError.QuoteChar := #0; for i := 0 to strlError.Count - 1 do begin with SGDetailErrors do begin Rows[i + 1].Delimiter := '|'; strlError.Strings[i] := StringReplace(strlError.Strings[i], ' ', 'tmp', [rfReplaceAll]); Rows[i + 1].DelimitedText := strlError.Strings[i]; Cells[0, i] := StringReplace(Cells[0, i], 'tmp', '', [rfReplaceAll]); Cells[1, i] := StringReplace(Cells[1, i], 'tmp', '', [rfReplaceAll]); Cells[2, i] := StringReplace(Cells[2, i], 'tmp', ' ', [rfReplaceAll]); end; end; except raise Exception.Create('Error processing file.'); end; strlError.Free; end;
