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
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
The 'LoadListBoxClick' method is linked to the ListBox1.OnClick event...
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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
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
I forgot to mention (just for fun) that
I have also easelly added 100.000 lines to a Listbox
without difficulties.
Regards,
Tomas Helgi
ASKER
Epsylon, I have mailed you my source..
I've examined your code but I don't see what you mean...
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_Dat e);
// 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).GameN ame,TCheat (C2).GameN ame);
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.F ont.Color;
R.SelAttributes.Style:=R.F ont.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_CH AR,ord(SB[ J]),0);
if I < length(S) then
case S[I] of
ch_Red : R.SelAttributes.Color:=clR ed;
ch_Bold : R.SelAttributes.Style:=[fs Bold];
ch_Blue : R.SelAttributes.Color:=clB lue;
ch_None : begin
R.SelAttributes.Color:=R.F ont.Color;
R.SelAttributes.Style:=R.F ont.Style;
end;
end;
inc(I);
end;
end;
procedure TCheat.SaveToStream(F : TStream);
begin
StrToStm(CodeStr(GameName) ,F);
F.Write(Version,SizeOf(Ver sion));
F.Write(IssDate,SizeOf(Iss Date));
StrToStm(CodeStr(CheatText ),F);
end;
procedure TCheat.LoadFromStream(F : TStream);
begin
GameName:=CodeStr(StrFromS tm(F));
F.Read(Version,SizeOf(Vers ion));
F.Read(IssDate,SizeOf(IssD ate));
CheatText:=CodeStr(StrFrom Stm(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(FileNam e,fmCreate );
N:=Count;
F.Write(N,SizeOf(N));
for N:=0 to Count-1 do
TCheat(Items[N]).SaveToStr eam(F);
F.Free;
end;
procedure TCheats.Load(FileName : string);
var F : TFileStream;
I,N : integer;
C : TCheat;
begin
F := TFileStream.Create(FileNam e,fmOpenRe ad);
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(S ender: 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(S ender: 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.GameN ame+' '+FormatDateTime('dd-mmm-y yyy',C.Iss Date));
end;
end;
// show text of cheat in richedit
procedure TForm1.ListBox1Click(Sende r: TObject);
var I : integer;
begin
I:=ListBox1.ItemIndex;
if (I >=0) and (I<Cheats.Count)
then SetRichText(Richedit1,TChe at(Cheats[ I]).CheatT ext);
end;
end.
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_Dat
// 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
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.F
R.SelAttributes.Style:=R.F
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_CH
if I < length(S) then
case S[I] of
ch_Red : R.SelAttributes.Color:=clR
ch_Bold : R.SelAttributes.Style:=[fs
ch_Blue : R.SelAttributes.Color:=clB
ch_None : begin
R.SelAttributes.Color:=R.F
R.SelAttributes.Style:=R.F
end;
end;
inc(I);
end;
end;
procedure TCheat.SaveToStream(F : TStream);
begin
StrToStm(CodeStr(GameName)
F.Write(Version,SizeOf(Ver
F.Write(IssDate,SizeOf(Iss
StrToStm(CodeStr(CheatText
end;
procedure TCheat.LoadFromStream(F : TStream);
begin
GameName:=CodeStr(StrFromS
F.Read(Version,SizeOf(Vers
F.Read(IssDate,SizeOf(IssD
CheatText:=CodeStr(StrFrom
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(FileNam
N:=Count;
F.Write(N,SizeOf(N));
for N:=0 to Count-1 do
TCheat(Items[N]).SaveToStr
F.Free;
end;
procedure TCheats.Load(FileName : string);
var F : TFileStream;
I,N : integer;
C : TCheat;
begin
F := TFileStream.Create(FileNam
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:
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(S
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(S
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.GameN
end;
end;
// show text of cheat in richedit
procedure TForm1.ListBox1Click(Sende
var I : integer;
begin
I:=ListBox1.ItemIndex;
if (I >=0) and (I<Cheats.Count)
then SetRichText(Richedit1,TChe
end;
end.
Sorry, I don't have that much time. Use a database like Access for instance...
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
I have posted the example to you.
Regards,
Tomas Helgi
ASKER
I don't really understand how it works at this point.
ASKER
I'll go check my mail and get back to you. Thank's.
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
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
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:
begin
ini.Free;
end;
// Saves the listbox items to the inifile
procedure TForm1.SaveListBoxClick(Se
begin
ini.WriteString('listbox',
end;
// Saves the current memo and associates it with the selected item in the inifile
procedure TForm1.SaveMemoClick(Sende
begin
if ListBox1.ItemIndex >= 0 then
ini.WriteString('memos', ListBox1.Items[ListBox1.It
end;
// Loads the memo associated with the selected listbox item
procedure TForm1.LoadListBoxClick(Se
begin
if ListBox1.ItemIndex >= 0 then
Memo1.Lines.CommaText := ini.ReadString('memos', ListBox1.Items[ListBox1.It
end;
end.
Use this as 'demo.ini' to start with:
[listbox]
items=aaa,bbbb,ccc,dddd,ee
[memos]
bbbb=sdfsdfsdf
aaa=Line1,Line2
ccc=dfgdfg,dfgdfg
dddd=gfdgdfg
eee=fdg
ffffff=sdfsdf