请帮我看看下面的十多行的代码,很简单的问题。(20分)

  • 主题发起人 主题发起人 kent100
  • 开始时间 开始时间
K

kent100

Unregistered / Unconfirmed
GUEST, unregistred user!
procedure TForm1.N3Click(Sender: TObject);
var
i:Integer;
s:String;
begin
s:='';
//for i := ListBox1.Items.Count - 1 downto 0 do
for i := 0 to ListBox1.Items.Count - 1 do
begin
if ListBox1.Selected then
begin
s:=s+ListBox1.Items;
listbox1.Items.Delete(i);
begin
with table1 do
if table1.Locate('jbdw',s,[]) then
begin
delete;
end;
break;
end;
end
else
showmessage('请选择左边列表中的一项');
break;
end;
end;

以上代码的意思是说当Listbox中没有一项选中时就出现提示
否则,删除选中的项。
但是我选中了Listbox中的一项但是还是提示我没选中,这是为何,程序老是执行到else后去,
而且我的if...else放在for循环里不是一个好办法,所以请大家帮我改改代码,谢谢!
 
用 ListBox.ItemIndex 得到当前选中行的序号
 
ili
你是否都没看代码呀,
我明明有写呀,if ListBox1.Selected then。。。。
 
所以你改成:
if ListBox1.ItemIndex <> -1 then
……
不行么?说不定就可以了?!而且你何必要用循环来找当前选中的项呢?况且,你的循环也是错的,
当第一行没有选中就跳为esle去了~~
 
如果没有响应你看看ListBox1是否是你想实际操作的对象.
也许你想要ListBox2响应,却写了1的代码.我试过你写的代码是正确的.
procedure TForm1.N3Click(Sender: TObject);
var
i: Integer;
begin
with ListBox1 do
if ItemIndex <> -1 then
if table1.Locate('jbdw', Items[ItemIndex], []) then
begin
table1.Delete;
Items.Delete(ItemIndex);
end;
ShowMessage('请选择左边列表中的一项');
end;

发出贴子后我发现有点问题又改了一下.可能和你的Email不一样了.
 
var
i: Integer;
s: string;
selected: Boolean;
begin
s := '';
selected := False;
for i := 0 to ListBox1.Items.Count - 1 do
begin
if ListBox1.Selected then
begin
selected := True;
s := s + ListBox1.Items;
listbox1.Items.Delete(i);
with table1 do
begin
if table1.Locate('jbdw', s, []) then delete;
end;
Exit;
end;
end;

if not (selected) then
begin
showmessage('请选择左边列表中的一项');
exit;
end;
 
你上面的代码跟本就没有完成循环。
在第一个没有选中的情况下就跳到了“请选择左边列表中的一项”并退出了。
 
多人接受答案了。
 
后退
顶部