我写了这样的一段代码,目的在于将filelistbox1中选中的项添加到listbox1中并且不能重复,但是不知道哪里出了问题,就是filelistbox1中的第一条内容选中了也过不去的,但是从第2到后面的都对的,选中的就可以过去,选不中的就不过去,不知道哪里的问题,请高手指点一下!procedure TForm1.Button3Click(Sender: TObject);
var i,j,str:integer;
char:string;
begin
for i:=0 to filelistbox1.Items.Count-1 do
begin
char:=filelistbox1.Directory+'\'+filelistbox1.Items[i];
if filelistbox1.Selected[i]=true then
begin
for j:=0 to listbox1.Items.Count-1 do
begin
if char=listbox1.Items[j] then
str:=1;
end;
if str=0 then
listbox1.Items.Add(char);
end;
str:=0;
end;
end;
var i,j,str:integer;
char:string;
begin
for i:=0 to filelistbox1.Items.Count-1 do
begin
char:=filelistbox1.Directory+'\'+filelistbox1.Items[i];
if filelistbox1.Selected[i]=true then
begin
for j:=0 to listbox1.Items.Count-1 do
begin
if char=listbox1.Items[j] then
str:=1;
end;
if str=0 then
listbox1.Items.Add(char);
end;
str:=0;
end;
end;
自己解决了,呵呵,漏了初始化的值。应该是:
procedure TForm1.Button3Click(Sender: TObject);
var i,j,str:integer;
char:string;
begin
str:=0;
for i:=0 to filelistbox1.Items.Count-1 do
begin
if filelistbox1.Selected[i]=true then
begin
char:=filelistbox1.Directory+'\'+filelistbox1.Items[i];
for j:=0 to listbox1.Items.Count-1 do
begin
if char=listbox1.Items[j] then
str:=1;
end;
if str=0 then
listbox1.Items.Add(char);
end;
str:=0;
end;
end;
i: Integer;
char: string;
begin
for i := 0 to FileListBox1.Items.Count - 1 do
begin
char := FileListBox1.Directory + '\' + FileListBox1.Items[i];
if FileListBox1.Selected[I] and (ListBox1.Items.IndexOf(char) < 0) then
ListBox1.Items.Add(char);
end;
end;