How to Load Richedit Line by Line From File

Asked

Viewed 233 times

0

I need to load a Richedit from a file. txt reading line by line.

I’m using a code, but Richedit doesn’t carry accented characters (Examples: ç, à, á, ã, é, ê, í, ô, õ, ó, ù...)

The code is as follows::

procedure TFrmMain.AbrirTxtNovo;
var
   MyOpenDialog : TOpenDialog;
   MyTextFile : TextFile;
   Linha : String;
begin
MyOpenDialog := TOpenDialog.Create(self);
MyOpenDialog.Filter := 'Arquivos Texto|*.txt';
MyOpenDialog.InitialDir := 'C:\';
MyOpenDialog.Title := ' Selecione um arquivo para leitura ';

If (MyOpenDialog.Execute) And (Length (MyOpenDialog.FileName) > 0) then
   begin
   AssignFile (MyTextFile, MyOpenDialog.FileName);
   Reset (MyTextFile);
   RichEdit.ScrollBars := ssBoth; // Permito exibição das Scroolbars
   RichEdit.Lines.Clear; // Limpo o RichEdit
   RichEdit.Font.Name := 'Arial'; // Ajusto a fonte para Arial
   RichEdit.WordWrap := False; // Evito quebra na linha

   while not (Eof (MyTextFile)) do
      begin
      ReadLn (MyTextFile, Linha);
      RichEdit.Lines.Add (LimpaString (Linha));
      end;

   CloseFile (MyTextFile);
   end
else
   Application.MessageBox(PChar ('Erro ao tentar ler o arquivo!' +
   MyOpenDialog.FileName), 'Erro na abertura do arquivo', mb_Ok);

end;

The function Cleanstring

function LimpaString (strIN: String): String;
var
   Counter : Integer;
   strOUT : String;
begin
   for Counter := 1 to Length (strIN) do
   if (Ord (strIN[Counter]) >= 032) and (Ord (strIN[Counter]) <= 125) then
       strOUT := strOUT + strIN[Counter];

Result := strOUT;
end;

How to solve this problem?

  • but if you do Richedit.Lines.Add('aéó'); it works, right? And the file has tags the richtext or it’s just plain text?

  • Good morning #Lucasmb.. all right?? : ) Dude I’m using Delphi 7.. I took your code and put it here and it worked fine.. I created a text file with the following words..: Mace bad action there Ló Tú and were correctly imported by your code. The only thing I could not replicate was the Cleanstring FUNCTION. So I ran your code without it. If the code is only this one.. Then who knows the problem is not the .TXT.file. Make it available that I care about here.. the Cleanstring function also.. Anything is just talk. :)

  • Hello @Ricardom.Souza I had forgotten to mention the Function Limpastring (which has now been quoted in the question) and it is really this Function that is "cleaning" the txt accents. I removed it and it worked. Thank you very much.

  • Hello @Tiagorodrigues I’m using Richedit.Lines.Add('...') and it’s working fine. Thanks.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.