[Solved] Loading a text file into a TMemo comonent. lazarus / 13-General ---------------------------------------------------------------------- JFistere · 2016-05-09 08:07 I want to load a text file into Memo1. The method I am using comes from a Wikipedia tutorial (bad idea?): ----------------------------- Save and load You can quite easily save and load the contents of a memo by using the methods SaveToFile and LoadFromFile of the class TStrings. The following example shows you how: Create a new application with a TMemo Memo1 and three TButton Button1, Button2 and Button3.ents and other options  Notify me of replies. Lock this topic. Return to this topic. Don't use smileys. Attach: Choose File (Clear Attachment) (more attachments) Allowed file types: bmp, doc, gif, jpg, mpg, pdf, png, txt, zip, rar, tar, gz, tgz, 7z, patch, diff, ico Restrictions: 4 per post, maximum total size 250KB, maximum individual size 250KB shortcuts: hit alt+s to submit/post or alt+p to preview Post Additionally put a TSaveDialog and a TOpenDialog from the component palette Dialogs on the form. Change Caption of Button1 to "Fill memo". In the event handler OnClick of Button1 fill the memo with any text, as in the example Insert lines directly. Change Caption of Button2 to "Save memo". Change Caption of Button3 to "Load memo". now modify the event handler OnClick of the buttons: procedure TForm1.Button2Click(Sender: TObject); begin   if SaveDialog1.Execute then     Memo1.Lines.SaveToFile(SaveDialog1.FileName); end; procedure TForm1.Button3Click(Sender: TObject); begin   if OpenDialog1.Execute then     Memo1.Lines.LoadFromFile(OpenDialog1.FileName); end; -------------------------------- I adapted it to load the file immediately when Memo1's Form is created. (There is no OnCreate event for TMemo.) Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm3.FormCreate(Sender: TObject);var  DatFil: AnsiString; begin  DatFil := 'PHONBOOK.TXT';  if OpenDialog2.Execute then    Memo1.Lines.LoadFromFile(OpenDialog2.DatFil);end;   The compiler halts with the cursor just before the DatFil on the last line with the message "Error identifier idents no member 'DatFil'". What is it telling me? (I tried to upload source files but it would not accept the file type. In the future what should the type for source code?) ---------------------------------------------------------------------- howardpc · 2016-05-09 08:23 If you know in advance which file you want loaded you can use its name directly (with a path if not in the current directory): Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.FormCreate(Sender: TObject);const  DatFileName = 'PHONBOOK.TXT';begin  Memo1.Lines.LoadFromFile(DatFileName);end; If you use a TOpenDialog, you have to assign the name you want to the dialog's Filename property. In this case, of course, the user could opt to open a different file. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm3.FormCreate(Sender: TObject);const  DatFileName = 'PHONBOOK.TXT';begin  OpenDialog2.Filename:=DatFileName;  if OpenDialog2.Execute then    Memo1.Lines.LoadFromFile(OpenDialog2.Filename);end; ---------------------------------------------------------------------- JFistere · 2016-05-10 00:00 Thanks! Your solution works perfectly. I will be using both methods, a constant filename and a variable filename, although I will not need the dialog box for the variable filename. I am still puzzled over what files to upload if I want to attach source code. Can you clarify that? ---------------------------------------------------------------------- howardpc · 2016-05-10 08:28 The source text files only. Usually this is a .lpi, a .lpr and .pas (or .pp) files for your unit sources. For a GUI project there will be one or more .lfm files for your form(s). i. e. no binary files (.exe, .ppu, .a, etc). The IDE provides a convenient menu that does all this for you (you can just accept the defaults, or perhaps choose a different location to save the published project): Project->Publish Project... Compress the resulting folder, say as a .zip, and it will be small enough to send to the forum. Anyone interested will be able to regenerate your project as an executable from the uncompressed sources you send.