我有一个Combobox默认3项,我想保存这样的项目:
Item1 //don't save
Item2 //Don't save
Items3 //save
//save all futur items added
Run Code Online (Sandbox Code Playgroud)
为什么我的代码不起作用?
if Combobox1.ItemIndex > 2 then // i used 2 for test and it's no work
Combobox1.Items.SaveToFile('util.conf');
end;
Run Code Online (Sandbox Code Playgroud)
如果我删除如果Combobox1.ItemIndex> 2然后保存所有项目...
如何解决这个问题呢?
将项目复制到临时列表,然后删除要从该临时列表中删除的任何一个.然后保存它.例如,此代码将从列表中删除前两个元素.
TempList := TStringList.Create;
try
TempList.Assign(ComboBox1.Items);
if TempList.Count>0 then
TempList.Delete(0);
if TempList.Count>0 then
TempList.Delete(0);
TempList.SaveToFile('util.conf');
finally
TempList.Free;
end;
Run Code Online (Sandbox Code Playgroud)
我不确定我是否完全理解需要删除列表中的哪些元素.无论如何,复制到不同列表并保存它的基本思想几乎可以肯定是你需要的.您一定能够找出需要删除的元素.