procedure TForm1.Button1Click(Sender: TObject);
var
i:integer;
begin
for i:=0 to 10 do
begin
ComboBox1.Items.Add(inttostr(i));
end;
end;procedure TForm1.Button2Click(Sender: TObject);var
i:integer;
begin
for i:=0 to 10 do
begin
ComboBox1.Items.Delete(i);
end;
end;button1给combobox1添加了10个item(0,1,2,3,4,5,6,7,8,9,10)button2意思是清空这十个项目,可结果是只清掉了itemindex是单数的项目
还保留着(1,3,5,7,9)我用xp和win2k都这样,Delphi7,不想用clear命令!
var
i:integer;
begin
for i:=0 to 10 do
begin
ComboBox1.Items.Add(inttostr(i));
end;
end;procedure TForm1.Button2Click(Sender: TObject);var
i:integer;
begin
for i:=0 to 10 do
begin
ComboBox1.Items.Delete(i);
end;
end;button1给combobox1添加了10个item(0,1,2,3,4,5,6,7,8,9,10)button2意思是清空这十个项目,可结果是只清掉了itemindex是单数的项目
还保留着(1,3,5,7,9)我用xp和win2k都这样,Delphi7,不想用clear命令!
for i:=ComboBox1.itmes.count-1 to 0 do
begin
ComboBox1.Items.Delete(i);
end;
用downto
for i:=ComboBox1.itmes.count-1 downto 0 do
begin
ComboBox1.Items.Delete(i);
end;
for i:=ComboBox1.itmes.count-1 downto 0 do
ComboBox1.Items.Delete(i);用下面这个是不行的
for i:=0 to 10 do
ComboBox1.Items.Delete(i);
你可以添加几条语句来调试,找出原因。
因为你删除一个以后,下面的都会往上面移动的。
for i:=0 to 10 do
begin
ShowMessage('c['+intToStr(i)+']='+ComboBox1.Items.String[i]);
ComboBox1.Items.Delete(i);
end;
你可以看到ComboBox1.Items.String[1]已经不等于删除以前的ComboBox1.Items.String[1]了,其实是删除以前的ComboBox1.Items.String[2]
-------------------
0 a
1 b
2 c
3 d
4 e
当i=0时 ( ComboBox1.Items.Delete(i);) 删除了a以后,变成了
index text
-------------------
0 b
1 c
2 d
3 e
i=i+1自增了以后,i=1 ,这时就删除了c 留下b没有动,依次增加后出现了部分未删除的情况如果要删除的话,最好从下往上删。如若要清空,就用ComboBox1.clear吧