为什么会不正确?(10分)

  • 主题发起人 主题发起人 Hexi
  • 开始时间 开始时间
H

Hexi

Unregistered / Unconfirmed
GUEST, unregistred user!
我在Delphi5中运行下面的程序,明明ListBox2.Item.Count>0,
却运行到for i:=0 to ListBox2.Items.Count-1do
ListBox2.Selected:=True;
时所ListBox越界out of bound(0);
这是不是TListBox的一个bug?
type
TForm3 = class(TForm)
SpeedButton1: TSpeedButton;//将ListBox1中的选项剪切到ListBox2
SpeedButton2: TSpeedButton;//将剪切所有1->2
SpeedButton3: TSpeedButton;//将ListBox2中的选项剪切到ListBox1
SpeedButton4: TSpeedButton;//将剪切所有2->1
ListBox1: TListBox;
ListBox2: TListBox;
procedure ListBox1Click(Sender: TObject);
procedure SpeedButton1Click(Sender: TObject);
procedure SpeedButton3Click(Sender: TObject);
procedure SpeedButton2Click(Sender: TObject);
procedure SpeedButton4Click(Sender: TObject);
procedure ListBox1DblClick(Sender: TObject);
procedure ListBox2DblClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
procedure CheckValid;//设置按钮有效性
procedure MoveItems(FromL,ToL:TListBox);//剪切所选项
end;

procedure TForm3.CheckValid;
begin
if (ListBox1.Items.Count=0) or (ListBox1.SelCount=0) then
SpeedButton1.Enabled:=False
else
SpeedButton1.Enabled:=True;
if (ListBox1.Items.Count=0) then
SpeedButton2.Enabled:=False
else
SpeedButton2.Enabled:=True;
if (ListBox2.Items.Count=0) or (ListBox2.SelCount=0) then
SpeedButton3.Enabled:=False
else
SpeedButton3.Enabled:=True;
if (ListBox2.Items.Count=0) then
SpeedButton4.Enabled:=False
else
SpeedButton4.Enabled:=True;
end;

procedure TForm3.ListBox1Click(Sender: TObject);
begin
CheckValid;
end;

procedure TForm3.MoveItems(FromL, ToL:TListBox);
var
i:Integer;
begin
for i:=0 to FromL.Items.Count-1do
if FromL.Selected then
ToL.Items.Add(FromL.Items);
for i:=FromL.Items.Count-1do
wnto 0do
if FromL.Selected then
FromL.Items.Delete(i);
CheckValid;
end;

procedure TForm3.SpeedButton1Click(Sender: TObject);
begin
MoveItems(ListBox1, ListBox2);
end;

procedure TForm3.SpeedButton3Click(Sender: TObject);
begin
MoveItems(ListBox2, ListBox1);
end;

procedure TForm3.SpeedButton2Click(Sender: TObject);
var i:Integer;
begin
for i:=0 to ListBox1.Items.Count-1do
ListBox1.Selected:=True;
MoveItems(ListBox1, ListBox2);
end;

procedure TForm3.SpeedButton4Click(Sender: TObject);
var i:Integer;
begin
for i:=0 to ListBox2.Items.Count-1do
ListBox2.Selected:=True;
MoveItems(ListBox2, ListBox1);
end;

procedure TForm3.ListBox1DblClick(Sender: TObject);
begin
MoveItems(ListBox1, ListBox2);
end;

procedure TForm3.ListBox2DblClick(Sender: TObject);
begin
MoveItems(ListBox2, ListBox1);
end;

end.
 
I是否应从1开始计数,即i:=1.
 
这跟D5无关,你试一下D4,应该同样报错。跟I从几开始也无关。
试一下 ListBox1.Selected[1]:=True;
也会出错!
这是因为Selected[]是用来查寻状态而不能被写入。


 
呵呵,问题出在你没有把listbox的MultiSelect设为True,不支持多选.
我把你的代码照此改动了一下,在D4,D5上均已通过.
 
真的耶!
又学了一招 :)
 
to cAkk:
我原来设的ListBox1和ListBox2的MultiSelect就为True;
而且,开始时ListBox1中有8项,ListBox1中为空,
当我按SpeedButton2时(将所有1->2)是正确的(这时给每个ListBox1.Selected:=True),
此时ListBox1中空,ListBox2中有8项。
然后我按SpeedButton4时(将所有2->1),就出现我所说的错误,ListBox2中明明
有8项,却抱错。

 
今天把你的代码全部拷贝下来,在D4里面通过,没有问题.
 
难道在D4中正确,在D5中出错吗?
 
晚上我用D5再给你测试一下.
 
在D5下测试过了,没有问题! 我是把你的代码原封不动贴过来的,唯一改动了
form3改成form1
你确信"ListBox1和ListBox2的MultiSelect就为True"吗?
 
我临时删除了部分与数据库有关的类容
单元:
unit dlgCommision;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
dmRds, StdCtrls, DBCtrls, Buttons, Db, DBClient;
type
TCommisionDlg = class(TForm)
SpeedButton1: TSpeedButton;
SpeedButton2: TSpeedButton;
SpeedButton3: TSpeedButton;
SpeedButton4: TSpeedButton;
Button1: TButton;
Button2: TButton;
ListBox1: TListBox;
Label1: TLabel;
Label2: TLabel;
ListBox2: TListBox;
procedure FormCreate(Sender: TObject);
procedure ListBox1Click(Sender: TObject);
procedure SpeedButton1Click(Sender: TObject);
procedure SpeedButton3Click(Sender: TObject);
procedure SpeedButton2Click(Sender: TObject);
procedure SpeedButton4Click(Sender: TObject);
procedure ListBox1DblClick(Sender: TObject);
procedure ListBox2DblClick(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
procedure CheckValid;
procedure MoveItems(FromL,ToL:TListBox);
end;

var
CommisionDlg:TCommisionDlg;
implementation
{$R *.DFM}

procedure TCommisionDlg.FormCreate(Sender: TObject);
begin
CheckValid;
end;

procedure TCommisionDlg.CheckValid;
begin
if (ListBox1.Items.Count=0) or (ListBox1.SelCount=0) then
SpeedButton1.Enabled:=False
else
SpeedButton1.Enabled:=True;
if (ListBox1.Items.Count=0) then
SpeedButton2.Enabled:=False
else
SpeedButton2.Enabled:=True;
if (ListBox2.Items.Count=0) or (ListBox2.SelCount=0) then
SpeedButton3.Enabled:=False
else
SpeedButton3.Enabled:=True;
if (ListBox2.Items.Count=0) then
SpeedButton4.Enabled:=False
else
SpeedButton4.Enabled:=True;
end;

procedure TCommisionDlg.ListBox1Click(Sender: TObject);
begin
CheckValid;
end;

procedure TCommisionDlg.MoveItems(FromL, ToL:TListBox);
var
i:Integer;
begin
for i:=0 to FromL.Items.Count-1do
if FromL.Selected then
ToL.Items.Add(FromL.Items);
for i:=FromL.Items.Count-1do
wnto 0do
if FromL.Selected then
FromL.Items.Delete(i);
CheckValid;
end;

procedure TCommisionDlg.SpeedButton1Click(Sender: TObject);
begin
MoveItems(ListBox1, ListBox2);
end;

procedure TCommisionDlg.SpeedButton3Click(Sender: TObject);
begin
MoveItems(ListBox2, ListBox1);
end;

procedure TCommisionDlg.SpeedButton2Click(Sender: TObject);
var i:Integer;
begin
for i:=0 to ListBox1.Items.Count-1do
ListBox1.Selected:=True;
MoveItems(ListBox1, ListBox2);
end;

procedure TCommisionDlg.SpeedButton4Click(Sender: TObject);
var i:Integer;
begin
for i:=0 to ListBox2.Items.Count-1do
ListBox2.Selected:=True;
MoveItems(ListBox2, ListBox1);
end;

procedure TCommisionDlg.ListBox1DblClick(Sender: TObject);
begin
MoveItems(ListBox1, ListBox2);
end;

procedure TCommisionDlg.ListBox2DblClick(Sender: TObject);
begin
MoveItems(ListBox2, ListBox1);
end;

end.

DFM文件:
object CommisionDlg: TCommisionDlg
Left = 235
Top = 153
BorderStyle = bsDialog
ClientHeight = 350
ClientWidth = 534
Color = clBtnFace
Font.Charset = GB2312_CHARSET
Font.Color = clWindowText
Font.Height = -14
Font.Name = '宋体'
Font.Style = []
OldCreateOrder = False
OnCreate = FormCreate
PixelsPerInch = 96
TextHeight = 14
object SpeedButton1: TSpeedButton
Left = 256
Top = 56
Width = 23
Height = 22
Caption = '>'
OnClick = SpeedButton1Click
end
object SpeedButton2: TSpeedButton
Left = 256
Top = 107
Width = 23
Height = 22
Caption = ' 》'
OnClick = SpeedButton2Click
end
object SpeedButton3: TSpeedButton
Left = 256
Top = 157
Width = 23
Height = 22
Caption = '<'
OnClick = SpeedButton3Click
end
object SpeedButton4: TSpeedButton
Left = 256
Top = 208
Width = 23
Height = 22
Caption = '《'
OnClick = SpeedButton4Click
end
object Label1: TLabel
Left = 56
Top = 16
Width = 70
Height = 14
Caption = '未授权类:'
end
object Label2: TLabel
Left = 320
Top = 16
Width = 70
Height = 14
Caption = '已授权类:'
end
object Button1: TButton
Left = 104
Top = 296
Width = 75
Height = 25
Caption = '确定'
ModalResult = 1
TabOrder = 0
end
object Button2: TButton
Left = 344
Top = 296
Width = 75
Height = 25
Caption = '取消'
ModalResult = 2
TabOrder = 1
end
object ListBox1: TListBox
Left = 56
Top = 40
Width = 161
Height = 217
ItemHeight = 14
Items.Strings = (
'信息类0'
'信息类1'
'信息类2'
'信息类3'
'信息类4'
'信息类5'
'信息类6'
'信息类7')
MultiSelect = True
Sorted = True
TabOrder = 2
OnClick = ListBox1Click
OnDblClick = ListBox1DblClick
end
object ListBox2: TListBox
Left = 320
Top = 40
Width = 161
Height = 217
ItemHeight = 14
Sorted = True
TabOrder = 3
OnClick = ListBox1Click
OnDblClick = ListBox2DblClick
end
end
 
老兄!!!我一再问你是否"ListBox1和ListBox2的MultiSelect设为True"了吗?
你说是的!
我根本不用测试你的代码! 你自己看看dfm文件里面,listbox2的MultiSelect
是true吗????!!!!!!
真没办法!!!!
BTW: listbox.select[]这个属性只有当listbox可以多选时才有效,

 
另: 看了你的程序的样子,好像是要做一个股票行情软件的server端? 是不是牙?
 
哎呀,你看我多粗心,光改了listbox1的MultiSelect:=True,忘了ListBox2了,
多谢大虾指点。
我做的东西更无线股票接收机有点关系。的确是Server端,包括数据发送,及费用
管理等。
 
后退
顶部