[英]How to avoid TStringList.SaveToFile overwriting my previous changes?
I'm trying to write my TStringList
into a TXT file ( mydbtxt.txt
).我正在尝试将我的
TStringList
写入 TXT 文件( mydbtxt.txt
)。
I have two source files, the MainForm and a Frame.我有两个源文件,MainForm 和 Frame。 In my MainForm, there is a TabControl and a Button, where I can dynamically add more tabs.
在我的 MainForm 中,有一个 TabControl 和一个 Button,我可以在其中动态添加更多选项卡。 When tabs are added, the Frame will be the
Parent
of the new tabs.添加选项卡后,框架将成为新选项卡的
Parent
级。
When I add a new tab and fill up some data into it, my second tab will always overwrite my first tab.当我添加一个新选项卡并在其中填写一些数据时,我的第二个选项卡将始终覆盖我的第一个选项卡。 Please help, the latest keeps on overwriting my previous record.
请帮忙,最新的继续覆盖我以前的记录。
Please refer to my image:请参考我的图片:
procedure TForm1.bSaveClick(Sender: TObject);
var
strList: TStringList;
i:integer;
begin
strList := TStringList.Create;
try
for i := 0 to TabControl1.TabCount - 1 do
begin
strList.Delimiter := ' ';
strList.QuoteChar := ',';
strList.Add('PlanName,SavingAccount,InitialCapital,TopUp,Every,RemindOn,CashOutBy,Percentage,TotalInitialCapital' +#13#10
+ profileFrame.plan_title.Text +','
+ profileFrame.savingAccount_edit.Text +','
+ profileFrame.initialCapital_edit.Text +','
+ profileFrame.topUp_edit.Text +','
+ profileFrame.every_comboBox.Selected.Text +','
+ profileFrame.remindOn_comboBox.Selected.Text +','
+ profileFrame.cashOutBy_comboBox.Selected.Text +','
+ profileFrame.percentage_edit.Text +','
+ profileFrame.initialCapital_edit.Text);
end;
strList.SaveToFile(TPath.GetDocumentsPath + TPath.PathSeparator + 'mydbtxt.txt');
finally
strList.Free;
end;
end;
This is how I create my Frame, and how I add my Frame into the TabControl:这就是我创建 Frame 以及将 Frame 添加到 TabControl 中的方式:
procedure TForm1.AddNewTab;
begin
profileFrame := TProfileFrame.Create(Self);
//TabItem
TabItem := TabControl1.Add();
inc(tab_name_Count);
tabItem.Text := tab_name_Count.ToString;
//
profileFrame.Parent := tabItem;
end;
You have a single profileFrame
variable that you are re-assigning each time AddNewTab()
is called.每次调用
AddNewTab()
时,您都会重新分配一个profileFrame
变量。 In bSaveClick()
, you are using that same variable in a loop, so of course every line added to the TStringList
is going to have the same data.在
bSaveClick()
中,您在循环中使用相同的变量,因此当然添加到TStringList
的每一行都将具有相同的数据。 Because you are not accessing the individual TProfileFrame
objects that you previously created.因为您没有访问您之前创建的单个
TProfileFrame
对象。 You are accessing only the last TProfileFrame
that you created.您仅访问您创建的最后一个
TProfileFrame
。
Since each TProfileFrame
object is a child of a TTabItem
, you can try this instead:由于每个
TProfileFrame
object 都是TTabItem
的子项,您可以试试这个:
procedure TForm1.bSaveClick(Sender: TObject);
var
strList: TStringList;
i:integer;
TabItem: TTabItem;
profileFrame: TProfileFrame;
begin
strList := TStringList.Create;
try
for i := 0 to TabControl1.TabCount - 1 do
begin
TabItem := TabControl1.Tabs[i];
profileFrame := TabItem.Children[0] as TProfileFrame;
strList.Add('PlanName,SavingAccount,InitialCapital,TopUp,Every,RemindOn,CashOutBy,Percentage,TotalInitialCapital');
strList.Add(profileFrame.plan_title.Text +','
+ profileFrame.savingAccount_edit.Text +','
+ profileFrame.initialCapital_edit.Text +','
+ profileFrame.topUp_edit.Text +','
+ profileFrame.every_comboBox.Selected.Text +','
+ profileFrame.remindOn_comboBox.Selected.Text +','
+ profileFrame.cashOutBy_comboBox.Selected.Text +','
+ profileFrame.percentage_edit.Text +','
+ profileFrame.initialCapital_edit.Text);
end;
strList.SaveToFile(TPath.Combine(TPath.GetDocumentsPath, 'mydbtxt.txt');
finally
strList.Free;
end;
end;
procedure TForm1.AddNewTab;
var
TTabItem: TTabItem;
profileFrame: TProfileFrame;
begin
profileFrame := TProfileFrame.Create(Self);
//TabItem
TabItem := TabControl1.Add();
inc(tab_name_Count);
tabItem.Text := tab_name_Count.ToString;
//
profileFrame.Parent := tabItem;
end;
That being said, there is no need to duplicate the column names for each record that is added to the TStringList
.话虽如此,没有必要为添加到
TStringList
的每条记录复制列名。 That will just pollute the text file and make it harder to read.这只会污染文本文件并使其更难阅读。 Try this instead:
试试这个:
procedure TForm1.bSaveClick(Sender: TObject);
var
strList: TStringList;
i:integer;
TabItem: TTabItem;
profileFrame: TProfileFrame;
begin
strList := TStringList.Create;
try
strList.Add('PlanName,SavingAccount,InitialCapital,TopUp,Every,RemindOn,CashOutBy,Percentage,TotalInitialCapital');
for i := 0 to TabControl1.TabCount - 1 do
begin
TabItem := TabControl1.Tabs[i];
profileFrame := TabItem.Children[0] as TProfileFrame;
strList.Add(profileFrame.plan_title.Text +','
+ profileFrame.savingAccount_edit.Text +','
+ profileFrame.initialCapital_edit.Text +','
+ profileFrame.topUp_edit.Text +','
+ profileFrame.every_comboBox.Selected.Text +','
+ profileFrame.remindOn_comboBox.Selected.Text +','
+ profileFrame.cashOutBy_comboBox.Selected.Text +','
+ profileFrame.percentage_edit.Text +','
+ profileFrame.initialCapital_edit.Text);
end;
strList.SaveToFile(TPath.Combine(TPath.GetDocumentsPath, 'mydbtxt.txt');
finally
strList.Free;
end;
end;
Then the file will look more like this:然后文件看起来更像这样:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.