Recent

Author Topic: Save texts into one txt file  (Read 1439 times)

Przemyslav

  • New Member
  • *
  • Posts: 39
Save texts into one txt file
« on: August 21, 2018, 12:40:53 pm »
Hello everybody :) I have problem with controls. I have form (see attachment) i and I would like to generate report file (txt) when I close form. This file should contain what options I had choose in ComboBoxes, what I wrote in Edit and Memo components and which CheckBox I selected. Can you help me with this problem? :)

P.S. Sorry for my English :)

ASerge

  • Hero Member
  • *****
  • Posts: 1671
Re: Save texts into one txt file
« Reply #1 on: August 21, 2018, 01:01:49 pm »
Hello everybody :) I have problem with controls. I have form (see attachment) i and I would like to generate report file (txt) when I close form. This file should contain what options I had choose in ComboBoxes, what I wrote in Edit and Memo components and which CheckBox I selected. Can you help me with this problem? :)
What problems? In opening, writing, and saving a file? Or in retrieving the properties of controls?

Przemyslav

  • New Member
  • *
  • Posts: 39
Re: Save texts into one txt file
« Reply #2 on: August 21, 2018, 01:03:29 pm »
I have problem with saving all this controls (what contains) in one file. My txt should be for example:

Putin 1 ----> from ComboBox1 i Edit1
Obama 2 ----> from ComboBox2 i Edit2

Blebleble... ----> from Memo1

Abcdefgh.... ----> from Memo2

Select:
CheckBox3 ----> state from CheckBoxes
« Last Edit: August 21, 2018, 01:06:44 pm by Przemyslav »

eric

  • Sr. Member
  • ****
  • Posts: 268
Re: Save texts into one txt file
« Reply #3 on: August 21, 2018, 01:14:16 pm »
You could use TIniFile, which makes it easy to save and retrieve that sort of information.

Handoko

  • Hero Member
  • *****
  • Posts: 3773
  • My goal: build my own game engine using Lazarus
Re: Save texts into one txt file
« Reply #4 on: August 21, 2018, 01:31:58 pm »
@Przemyslav

As @eric said, it will be easier to use TIniFile:
https://www.freepascal.org/docs-html/fcl/inifiles/tinifile.html

But here I provide you how to use TStringList to save the controls' results to a text file.

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
  9.   LCLType;
  10.  
  11. type
  12.  
  13.   { TForm1 }
  14.  
  15.   TForm1 = class(TForm)
  16.     CheckBox1: TCheckBox;
  17.     CheckBox2: TCheckBox;
  18.     CheckBox3: TCheckBox;
  19.     CheckBox4: TCheckBox;
  20.     ComboBox1: TComboBox;
  21.     ComboBox2: TComboBox;
  22.     Edit1: TEdit;
  23.     Edit2: TEdit;
  24.     Memo1: TMemo;
  25.     Memo2: TMemo;
  26.     procedure FormCloseQuery(Sender: TObject; var CanClose: boolean);
  27.     procedure FormCreate(Sender: TObject);
  28.   private
  29.  
  30.   public
  31.  
  32.   end;
  33.  
  34. var
  35.   Form1: TForm1;
  36.  
  37. implementation
  38.  
  39. {$R *.lfm}
  40.  
  41. { TForm1 }
  42.  
  43. procedure TForm1.FormCreate(Sender: TObject);
  44. begin
  45.   ComboBox1.Items.Add('Orange');
  46.   ComboBox1.Items.Add('Banana');
  47.   ComboBox1.Items.Add('Grape');
  48.   ComboBox2.Items.Add('Coffee');
  49.   ComboBox2.Items.Add('Tea');
  50.   ComboBox2.Items.Add('Juice');
  51. end;
  52.  
  53. procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: boolean);
  54. const
  55.   OutputFileName = 'report.txt';
  56. var
  57.   OutputFile: TStringList;
  58. begin
  59.  
  60.   if (Application.MessageBox('Save report to ' + OutputFileName + '?',
  61.     'Save Report', MB_YESNO) <> IDYES) then
  62.     Exit;
  63.  
  64.   OutputFile := TStringList.Create;
  65.  
  66.   // Save ComboBox data
  67.   OutputFile.Add('// ComboBox1');
  68.   OutputFile.Add(ComboBox1.Text);
  69.   OutputFile.Add('');
  70.   OutputFile.Add('// ComboBox');
  71.   OutputFile.Add(ComboBox2.Text);
  72.   OutputFile.Add('');
  73.  
  74.   // Save Edit data
  75.   OutputFile.Add('//Edit1');
  76.   OutputFile.Add(Edit1.Text);
  77.   OutputFile.Add('');
  78.   OutputFile.Add('//Edit2');
  79.   OutputFile.Add(Edit2.Text);
  80.   OutputFile.Add('');
  81.  
  82.   // Save Memo data
  83.   OutputFile.Add('// ---- start of Memo1 ----');
  84.   OutputFile.AddStrings(Memo1.Lines);
  85.   OutputFile.Add('// --- end of Memo1 ---');
  86.   OutputFile.Add('');
  87.   OutputFile.Add('// --- start of Memo2 ---');
  88.   OutputFile.AddStrings(Memo2.Lines);
  89.   OutputFile.Add('// --- end of Memo2 ---');
  90.   OutputFile.Add('');
  91.  
  92.   // Save CheckBox data
  93.   OutputFile.Add('Checkbox1 = ' + CheckBox1.Checked.ToString);
  94.   OutputFile.Add('Checkbox2 = ' + CheckBox2.Checked.ToString);
  95.   OutputFile.Add('Checkbox3 = ' + CheckBox3.Checked.ToString);
  96.   OutputFile.Add('Checkbox4 = ' + CheckBox4.Checked.ToString);
  97.  
  98.   OutputFile.SaveToFile(OutputFileName);
  99.  
  100. end;
  101.  
  102. end.

You can download the test.zip for testing.

Przemyslav

  • New Member
  • *
  • Posts: 39
Re: Save texts into one txt file
« Reply #5 on: August 21, 2018, 01:33:46 pm »
Thank you very much !!!! You are genious :) I must learn this.

 

TinyPortal © 2005-2018