こんばんは、つい最近Delphiをかじりだした者です。
質問なのですが、
現在作成しているフォームの中に
リストボックス2つ、テキストボックス1つがあり
これらの内容を1つのテキストファイルで書き込み/読み込みを行いたいのですが
どうしたらいいのでしょうか?
ごめんなさい、訂正します。
>1つのテキストファイルで書き込み/読み込み
テキストファイルに保存し、保存したファイルを開くと
各コンポーネントに保存時の内容がセットされる。
テキストボックスってなんだか知りませんが TMemo だとすると
TStringList を使って
ListBox1 の行数を StrToInt() テキストで
ListBox1 の Items
...
ListBox2 の行数を StrToInt() テキストで
ListBox2 の Items
...
TMemo の行数を StrToInt() テキストで
TMemo.Lines
...
とすると一つのテキストファイルで読み書き出来ると思います
> StrToInt() テキストで
間違いました。「IntToStr() テキストで」です
説明が悪くてすいません。
テキストボックス⇒TEditの事です。
例えば
[ ListBox1 ]
AAAAAAAAA
BBBBBBBBB
CCCCCCCCC
DDDDDDDDD
[ListBox2]
EEEEEEEEE
FFFFFFFFF
GGGGGGGGG
HHHHHHHHH
[TEdit.Text]
IIIIIIIIIIIII
↑のように格納されているデータをTStringListか何かでファイルに書き込んでいき、書き込みが終われば保存。
保存したファイルを読込むと、保存したデータが読込まれ、各コンポーネントに格納される・・・といった仕組みにしようと思ってるのですが・・・
テキストで扱うとなると各行の頭にインデックスを置いてあげては?
ということです。
procedure TForm1.SaveToOneText(path:string);
var
i:integer;
strList:TStringList;
begin
strList:=TStringList.Create;
for i:=0 to ListBox1.Items.Count-1 do
strList.Add('1:'+ListBox1.Items.Strings[i]);
for i:=0 to ListBox1.Items.Count-1 do
strList.Add('2:'+ListBox2.Items.Strings[i]);
strList.Add('3:'+Edit1.Text);
strList.SaveToFile(path);
strList.Free;
end;
procedure TForm1.LoadFromOneText(path:string);
var
i:integer;
strList:TStringList;
str:string;
begin
ListBox1.Clear;
ListBox2.Clear;
strList:=TStringList.Create;
strList.LoadFromFile(path);
for i:=0 to strList.Count-1 do
begin
str:= strList.Strings[i];
if str[1]='1' then
ListBox1.Items.Add(Copy(str,3,Length(str)-2))
else if str[1]='2' then
ListBox2.Items.Add(Copy(str,3,Length(str)-2))
else if str[1]='3' then
Edit1.Text:= Copy(str,3,Length(str)-2);
end;
strList.Free;
end;
なるほど・・・インデックスですか
やってみます。
容量制限ありますが、INIファイルを使ってみるとか?
こんな感じですけど
procedure TForm1.Button1Click(Sender: TObject);
var
i, num1, num2: integer;
sl: TStringList;
begin
num1 := ListBox1.Items.Count;
num2 := ListBox2.Items.Count;
sl := TStringList.Create;
try
sl.Add(IntToStr(num1));
for i := 0 to num1-1 do sl.Add(ListBox1.Items[i]);
sl.Add(IntToStr(num2));
for i := 0 to num2-1 do sl.Add(ListBox2.Items[i]);
sl.Add(Edit1.Text);
sl.SaveToFile('c:\test.txt');
finally
sl.Free;
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
var
i, num1, num2: integer;
sl: TStringList;
begin
sl := TStringList.Create;
try
sl.LoadFromFile('c:\test.txt');
num1 := StrToInt(sl[0]);
num2 := StrToInt(sl[num1+1]);
for i := 1 to num1 do ListBox1.Items.Add(sl[i]);
for i := num1+2 to num1+1+num2 do ListBox2.Items.Add(sl[i]);
Edit1.Text := sl[sl.Count-1];
finally
sl.Free;
end;
end;
書込にはTStringList、読込にはTMemIniFileを使います。
下記が例です。
implementation
uses IniFiles;
procedure TForm1.Button1Click(Sender: TObject);
var
F: TStringList;
begin
F:= TStringList.Create;
try
F.Add(Format('[%s]', [ComboBox1.Name]));
F.AddStrings(ComboBox1.Items);
F.Add(Format('[%s]', [Memo1.Name]));
F.AddStrings(Memo1.Lines);
F.SaveToFile('c:\test.txt');
finally
F.Free;
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
var
F: TMemIniFile;
begin
F:= TMemIniFile.Create('c:\test.txt');
try
F.ReadSectionValues(ComboBox1.Name, ComboBox1.Items);
F.ReadSectionValues(Memo1.Name, Memo1.Lines);
finally
F.Free;
end;
end;
レス送れて申し訳ないです。
ご回答してくださった皆様、本当にありがとうございます!
お蔭様で無事解決できました。
ツイート | ![]() |