Link to home
Start Free TrialLog in
Avatar of TCSCode
TCSCode

asked on

Read/Save contents of 2 Listboxs, 2 Memos to one file.

Hi,

How can i get my program to to save all the settings in one file? but i have 2 sections i want to reload when i click on the title of a listbox.

For example:

Listbox1 line 1 has : "Hello World"
Memo1 has           : "Hello to the world"          

Listbox2 Line 2 has : "Yard Sale"
Memo1 has           : "Yard sale friday at 8:00am"

So if i have "Hello World" selected in the listbox1 I want Memo1 to show "Hello to the world" without quotes of course or if i have "Yard Sale" selected I would like it to show "Yard Sale friday at 8:00am"

When i close or open my program, I want it to save/load it to one file or load it from that file to do the above!

When I load my program I would like to click on "Hello World" and "Yard Sale" and have memo1 display the text that was saved on exit.

I will give more points if needed thank's



 

Avatar of Epsylon
Epsylon

This is getting more and more complicated, so I you want to store more data than what you have now, consider using a database.

For now try this, using an inifile:


unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, IniFiles;

type
  TForm1 = class(TForm)
    SaveMemo: TButton;
    SaveListBox: TButton;
    ListBox1: TListBox;
    Memo1: TMemo;
    procedure FormCreate(Sender: TObject);
    procedure SaveListBoxClick(Sender: TObject);
    procedure SaveMemoClick(Sender: TObject);
    procedure LoadListBoxClick(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    { Private declarations }
    ini: TIniFile;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

const INIFILE = '.\demo.ini';

// Loads the listbox items from the inifile
procedure TForm1.FormCreate(Sender: TObject);
begin
  ini := TIniFile.Create(INIFILE);
  ListBox1.Items.CommaText := ini.ReadString('listbox', 'items', '');
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  ini.Free;
end;

// Saves the listbox items to the inifile
procedure TForm1.SaveListBoxClick(Sender: TObject);
begin
  ini.WriteString('listbox', 'items', ListBox1.Items.CommaText);
end;

// Saves the current memo and associates it with the selected item in the inifile
procedure TForm1.SaveMemoClick(Sender: TObject);
begin
  if ListBox1.ItemIndex >= 0 then
    ini.WriteString('memos', ListBox1.Items[ListBox1.ItemIndex], Memo1.Lines.CommaText);
end;

// Loads the memo associated with the selected listbox item
procedure TForm1.LoadListBoxClick(Sender: TObject);
begin
  if ListBox1.ItemIndex >= 0 then
    Memo1.Lines.CommaText := ini.ReadString('memos', ListBox1.Items[ListBox1.ItemIndex], '');
end;

end.



Use this as 'demo.ini' to start with:

[listbox]
items=aaa,bbbb,ccc,dddd,eee,ffffff
[memos]
bbbb=sdfsdfsdf
aaa=Line1,Line2
ccc=dfgdfg,dfgdfg
dddd=gfdgdfg
eee=fdg
ffffff=sdfsdf
The 'LoadListBoxClick' method is linked to the ListBox1.OnClick event...
ASKER CERTIFIED SOLUTION
Avatar of Tomas Helgi Johannsson
Tomas Helgi Johannsson
Flag of Iceland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of TCSCode

ASKER

Epsylon, Is it possible to have more then 10 Lines in a memo. Would you like me to send you a copy of my project so you can see what I mean?
Yes, it is possible.

epsylon3@hotmail.com
Hi!
Yes, you can have more than 10 lines in a TMemo.
I don't remember the exactly size limit but I have
easelly added 2-6 A4 pages to a TMemo.

Regards,
Tomas Helgi
Hi!
I forgot to mention (just for fun) that
I have also easelly added 100.000 lines to a Listbox
without difficulties.

Regards,
Tomas Helgi
Avatar of TCSCode

ASKER

Epsylon, I have mailed you my source..
I've examined your code but I don't see what you mean...
Avatar of TCSCode

ASKER

Epsylon, here is code that i got from a question on Experts Exchange, This is how i want to save the contents of a listbox and a memo to only one file. Something like a database.

unit Unit1;

interface

uses
 Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
 Buttons, StdCtrls, ComCtrls;

type
 // object to store chat data in memory
 TCheat = class(TObject)
   GameName  : string;
   Version   : integer;
   IssDate   : TDateTime;
   CheatText : string;

   procedure SaveToStream(F : TStream);
   procedure LoadFromStream(F : TStream);
 end;

 TSortKeys = (sk_Name,sk_Version,sk_Date);

 // list of cheats in memory
 TCheats = class(TList)
   procedure SortBy(SortKey : TSortKeys);
   procedure Save(FileName : string);
   procedure Load(FileName : string);
 end;


 TForm1 = class(TForm)
   SpeedButton1: TSpeedButton;
   SpeedButton2: TSpeedButton;
   ListBox1: TListBox;
   RichEdit1: TRichEdit;
   procedure FormCreate(Sender: TObject);
   procedure FormDestroy(Sender: TObject);
   procedure SpeedButton1Click(Sender: TObject);
   procedure SpeedButton2Click(Sender: TObject);
   procedure ListBox1Click(Sender: TObject);
 private
 public
   Cheats : TCheats;
 end;

var
 Form1: TForm1;

implementation

{$R *.DFM}

const CheatFile = '.\CHEATS.DAT';
     // formatting characters
     ch_Red    = #1;
     ch_Bold   = #2;
     ch_Blue   = #3;
     ch_None   = #4;

procedure StrToStm(const S : string; aSTM : TStream);
var I : integer;
begin
 I:=length(S);
 aSTM.Write(I,4);
 aSTM.Write(S[1],I);
end;

function StrFromStm(aSTM : TStream) : string;
var I : integer;
begin
 aSTM.Read(I,4);
 SetLength(result,I);
 aSTM.Read(result[1],I);
end;

function  CodeStr(S : string) : string;
var I : integer;
begin
 for I:=1 to length(S) do S[I]:=chr(ord(S[I]) xor 5);
 result:=S;
end;

function CompCheatByName(C1,C2 : pointer) : integer;
begin
 result:=CompareText(TCheat(C1).GameName,TCheat(C2).GameName);
end;

function CompCheatByVersion(C1,C2 : pointer) : integer;
begin
 if TCheat(C1).Version > TCheat(C2).Version then result:=1 else
 if TCheat(C1).Version < TCheat(C2).Version then result:=-1 else
   result:=0;
end;

function CompCheatByDate(C1,C2 : pointer) : integer;
begin
 if TCheat(C1).IssDate > TCheat(C2).IssDate then result:=1 else
 if TCheat(C1).IssDate < TCheat(C2).IssDate then result:=-1 else
   result:=0;
end;

procedure SetRichText(R : TRichEdit; S : string);
var I,J  : integer;
   SB   : string;
begin
  R.Lines.Clear;
  I := 1;
  R.SelAttributes.Color:=R.Font.Color;
  R.SelAttributes.Style:=R.Font.Style;
  while I < length(S) do
  begin
    SB:='';
    while S[I] > ch_none do
    begin
      SB:=SB+S[I];
      inc(I);
    end;
    for J:=1 to length(SB) do SendMessage(R.Handle,WM_CHAR,ord(SB[J]),0);

    if I < length(S) then
    case S[I] of
    ch_Red  : R.SelAttributes.Color:=clRed;
    ch_Bold : R.SelAttributes.Style:=[fsBold];
    ch_Blue : R.SelAttributes.Color:=clBlue;
    ch_None : begin
               R.SelAttributes.Color:=R.Font.Color;
               R.SelAttributes.Style:=R.Font.Style;
              end;
    end;
    inc(I);
  end;
end;

procedure TCheat.SaveToStream(F : TStream);
begin
 StrToStm(CodeStr(GameName),F);
 F.Write(Version,SizeOf(Version));
 F.Write(IssDate,SizeOf(IssDate));
 StrToStm(CodeStr(CheatText),F);
end;

procedure TCheat.LoadFromStream(F : TStream);
begin
 GameName:=CodeStr(StrFromStm(F));
 F.Read(Version,SizeOf(Version));
 F.Read(IssDate,SizeOf(IssDate));
 CheatText:=CodeStr(StrFromStm(F));
end;


procedure TCheats.SortBy(SortKey : TSortKeys);
begin
  case SortKey of
  sk_Name    : Sort(@CompCheatByName);
  sk_Date    : Sort(@CompCheatByDate);
  sk_Version : Sort(@CompCheatByVersion);
  end;
end;

procedure TCheats.Save(FileName : string);
var F : TFileStream;
   N : integer;
begin
 F := TFileStream.Create(FileName,fmCreate);
 N:=Count;
 F.Write(N,SizeOf(N));
 for N:=0 to Count-1 do
 TCheat(Items[N]).SaveToStream(F);
 F.Free;
end;

procedure TCheats.Load(FileName : string);
var F   : TFileStream;
   I,N : integer;
   C   : TCheat;
begin
 F := TFileStream.Create(FileName,fmOpenRead);
 N:=Count;
 F.Read(N,SizeOf(N));
 for I:=0 to N-1 do
 begin
   C := TCheat.Create;
   C.LoadFromStream(F);
   Add(C);
 end;
 F.Free;
end;


procedure TForm1.FormCreate(Sender: TObject);
begin
  Cheats := TCheats.Create;
end;

procedure TForm1.FormDestroy(Sender: TObject);
var I : integer;
begin
  for I:=0 to Cheats.Count-1 do
  TCheat(Cheats[I]).Free; // don't sure D3 has TObjectList
  Cheats.Free;
end;

// create random cheats file
procedure TForm1.SpeedButton1Click(Sender: TObject);
var C : TCheat;
   I : integer;
begin
 for I:=1 to 100 do
 begin
   C:=TCheat.Create;
   C.GameName  := 'Game Name'+IntToStr(I);
   C.IssDate   := Now-365+random(365);
   C.Version   := Random(10);
   C.CheatText := 'Cheat for GameName'+IntToStr(I)+^M^J+
                  ch_Red+'first'+ch_none+'  line *************'^M+
                  ch_Bold+'second line *************'^M+
                  'last'+ch_Blue+' blue line'+ch_none+' ***************'^M+
                  '---';
   Cheats.Add(C);
 end;
 Cheats.SortBy(sk_Name);
 Cheats.Save(CheatFile);
end;

// load created!!! before file
procedure TForm1.SpeedButton2Click(Sender: TObject);
var C : TCheat;
   I : integer;
begin
 Cheats.Load(CheatFile);
 Cheats.SortBy(sk_Date);
 for I:=0 to Cheats.Count-1 do
 begin
   C:=Cheats[I];
   ListBox1.Items.Add(C.GameName+'  '+FormatDateTime('dd-mmm-yyyy',C.IssDate));
 end;
end;

// show text of cheat in richedit
procedure TForm1.ListBox1Click(Sender: TObject);
var I : integer;
begin
  I:=ListBox1.ItemIndex;
  if (I >=0) and (I<Cheats.Count)
  then SetRichText(Richedit1,TCheat(Cheats[I]).CheatText);
end;

end.
Sorry, I don't have that much time. Use a database like Access for instance...
Avatar of TCSCode

ASKER

Hi TomasHelgi, Can you email me a working demo of the code you have displayed to hyperon@eudoramail.com thank's
Hi!

I have posted the example to you.

Regards,
Tomas Helgi
Avatar of TCSCode

ASKER

I don't really understand how it works at this point.
Avatar of TCSCode

ASKER

I'll go check my mail and get back to you. Thank's.
Avatar of TCSCode

ASKER

Great thank's... Is there any place or books i could get that could teach me delphi programming, that is better then delphi help files and the books that came with delphi?
Hi!

I recommend the Delphi 5 Developer's Guide (if you have D5)
,Mastering Delphi 5 or Delphi Developer's Handbook.
Then if you are considering C/S development then the Client/Server Developer's Guide is a good book (I have a older versions of C/S Developer's Guide with Delphi3)
Then there is a lot of books for Delphi programming that focuses on COM,XML,OpenGL etc.
Check out www.amazon.com, type in Delphi in the Search field and you have a list of books that you could take with you to the nearest bookstore or order it online.

Regards,
Tomas Helgi