Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何避免TStringList.SaveToFile覆盖我以前的更改?_File_Delphi_Frame_Firemonkey_Tabcontrol - Fatal编程技术网

如何避免TStringList.SaveToFile覆盖我以前的更改?

如何避免TStringList.SaveToFile覆盖我以前的更改?,file,delphi,frame,firemonkey,tabcontrol,File,Delphi,Frame,Firemonkey,Tabcontrol,我正在尝试将我的TStringList写入一个TXT文件(mydbtxt.TXT) 我有两个源文件,MainForm和Frame。在我的MainForm中,有一个TabControl和一个按钮,我可以在其中动态添加更多选项卡。添加选项卡时,框架将成为新选项卡的父项 当我添加一个新选项卡并向其中填充一些数据时,我的第二个选项卡将始终覆盖我的第一个选项卡。请帮忙,最近的记录一直在覆盖我以前的记录 请参考我的图片: 以下是我创建框架的方式,以及如何将框架添加到TabControl中: procedu

我正在尝试将我的
TStringList
写入一个TXT文件(
mydbtxt.TXT

我有两个源文件,MainForm和Frame。在我的MainForm中,有一个TabControl和一个按钮,我可以在其中动态添加更多选项卡。添加选项卡时,框架将成为新选项卡的父项

当我添加一个新选项卡并向其中填充一些数据时,我的第二个选项卡将始终覆盖我的第一个选项卡。请帮忙,最近的记录一直在覆盖我以前的记录

请参考我的图片:

以下是我创建框架的方式,以及如何将框架添加到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;

每次调用
AddNewTab()
时,都会重新分配一个
profileFrame
变量。在
bSaveClick()
中,您在循环中使用相同的变量,因此添加到
TStringList
的每一行当然都将具有相同的数据。因为您没有访问以前创建的单个
TProfileFrame
对象。您只访问您创建的最后一个
TProfileFrame

由于每个
TProfileFrame
对象都是
TTabItem
的子对象,因此可以尝试以下操作:

程序TForm1.bsavick(发送方:TObject);
变量
strList:TStringList;
i:整数;
TabItem:TTabItem;
profileFrame:TProfileFrame;
开始
strList:=TStringList.Create;
尝试
对于i:=0到TabControl1.TabCount-1 do
开始
TabItem:=TabControl1.Tabs[i];
profileFrame:=TabItem.Children[0]作为TProfileFrame;
strList.Add('PlanName,SavingAccount,InitialCapital,toppup,Every,remention,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.Memention_comboBox.Selected.Text+','
+profileFrame.cashOutBy_comboBox.Selected.Text+','
+profileFrame.percentage_edit.Text+','
+profileFrame.initialCapital\u edit.Text);
结束;
strList.SaveToFile(TPath.Combine(TPath.GetDocumentsPath,'mydbtxt.txt');
最后
strList.Free;
结束;
结束;
程序TForm1.ADDNETAB;
变量
TTabItem:TTabItem;
profileFrame:TProfileFrame;
开始
profileFrame:=TProfileFrame.Create(Self);
//TabItem
TabItem:=TabControl1.Add();
inc(制表符名称和计数);
tabItem.Text:=tab\u name\u Count.ToString;
//
profileFrame.Parent:=tabItem;
结束;

也就是说,没有必要为添加到
TStringList
的每条记录复制列名。这只会污染文本文件并使其更难读取。请尝试以下方法:

程序TForm1.bsavick(发送方:TObject);
变量
strList:TStringList;
i:整数;
TabItem:TTabItem;
profileFrame:TProfileFrame;
开始
strList:=TStringList.Create;
尝试
strList.Add('PlanName,SavingAccount,InitialCapital,toppup,Every,remention,CashOutBy,Percentage,TotalInitialCapital');
对于i:=0到TabControl1.TabCount-1 do
开始
TabItem:=TabControl1.Tabs[i];
profileFrame:=TabItem.Children[0]作为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.Memention_comboBox.Selected.Text+','
+profileFrame.cashOutBy_comboBox.Selected.Text+','
+profileFrame.percentage_edit.Text+','
+profileFrame.initialCapital\u edit.Text);
结束;
strList.SaveToFile(TPath.Combine(TPath.GetDocumentsPath,'mydbtxt.txt');
最后
strList.Free;
结束;
结束;
然后,文件将更像这样:


问题是您的
strList.Add
行不引用
i
。您反复添加同一行。
profileFrame
不会每次引用不同的帧。它总是一样的。它不知道您的循环或
i
。您需要做的是引用
i
迭代
i
中的第个帧。您没有使用对象列表吗?然后使用
FMyFrames[i]。计划标题。文本
等等。(此外,您忘了告诉我们在您的情况下
profileFrame
是什么。它是一个全局变量吗?它定义在哪里?它分配到哪里?但它与解决方案无关。)此外,您可以删除设置分隔符和引号字符的行,因为您根本不使用
TStringList
的name-value功能。即使使用了,您也应该在循环之外(之前)设置这些行.您好,先生,我已经在我的问题中添加了一些内容。您可以指导我吗?但是我们一直在谈论的
TObjectList
在哪里?您需要在
TForm1
类声明的
private
部分添加一个
FFrames:TObjectList
,在
FormCreate
FFrames:=TObjectList.Create(False)
say),在
FormDestroy
FFrames.free
)中释放它,并在
AddNewTab
FFrames.add(profileFrame)中将每个新帧添加到其中
。您的
profileFrame
变量将始终引用上次添加的帧,因为您每次都会覆盖此变量!但现在您可以引用
FFrame[0]、FFrame[1]、…
来访问每个单独的帧。
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;