保存Combobox项目3,4 delphi

时间:2012-10-29 21:05:43

标签: delphi combobox

默认情况下,我有一个带3项的Combobox,我想保存这样的项目:

Item1 //don't save
Item2 //Don't save
Items3 //save
//save all futur items added

为什么我的代码不起作用?

if Combobox1.ItemIndex > 2 then // i used 2 for test and it's no work
   Combobox1.Items.SaveToFile('util.conf');
end;

如果我删除如果Combobox1.ItemIndex> 2然后保存所有项目......

如何解决这个问题?

1 个答案:

答案 0 :(得分:5)

将项目复制到临时列表,然后删除要从该临时列表中删除的项目。然后保存它。例如,此代码将从列表中删除前两个元素。

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;

我不确定我是否完全理解需要删除列表中的哪些元素。无论如何,复制到不同列表并保存它的基本思想几乎可以肯定是您所需要的。您一定能够找出需要删除的元素。