G
glacier_gxf
Unregistered / Unconfirmed
GUEST, unregistred user!
我是一个初学者,我想在一个按钮的Click事件中实现从一个已有了N条Item的ProIDListBox中随机抽取没有重复的M条Items放入RandomListBox中,当然0<M<N,但是老是出现重复的。用Exit跳出循环又不对,请各位大侠帮我分析分析,代码如下:
procedure TMainForm.RzRapidFireButton13Click(Sender: TObject);
Var i:integer;
Str:string;
BStr:string;
T:boolean;
begin
While RandomListBox.Items.Count<M Do
begin
if RandomListBox.Items.Count=0 then
RandomListBox.items.Add(ProIDListBox.Items.Strings[random(ProNum)]);
For i:=0 To RandomListBox.Items.Count-1 Do
begin
T:=False;
Str:=ProIDListBox.Items.Strings[random(ProNum)];
BStr:=RandomListBox.Items.Strings;
if Str=BStr then
begin
T:=True;
exit;
end;
RandomListBox.items.Add(Str);
end;
end;
end;
procedure TMainForm.RzRapidFireButton13Click(Sender: TObject);
Var i:integer;
Str:string;
BStr:string;
T:boolean;
begin
While RandomListBox.Items.Count<M Do
begin
if RandomListBox.Items.Count=0 then
RandomListBox.items.Add(ProIDListBox.Items.Strings[random(ProNum)]);
For i:=0 To RandomListBox.Items.Count-1 Do
begin
T:=False;
Str:=ProIDListBox.Items.Strings[random(ProNum)];
BStr:=RandomListBox.Items.Strings;
if Str=BStr then
begin
T:=True;
exit;
end;
RandomListBox.items.Add(Str);
end;
end;
end;