获取Table中数据出现的问题(100分)(100分)

  • 主题发起人 主题发起人 winbowl1122
  • 开始时间 开始时间
W

winbowl1122

Unregistered / Unconfirmed
GUEST, unregistred user!
在触发ListBox的OnDrawItem事件时,速度极慢,怎样优化程序,非常感谢。我的程序如下:
strName := listbox1.Items.Strings[index];
table2.First;
for i:=0 to table2.RecordCount - 1 do begin
if(strName=table2.FieldByName('busname').AsString) then begin
strMPI := table2.FieldByName('mpi').AsString;
break;
end;
table2.Next;
end;
if(strMPI<>'') then begin
listbox1.Canvas.Brush.Color := clgray;
listbox1.Canvas.Font.Color := clred;
listbox1.Canvas.TextOut(rect.Left,rect.Top,ListBox1.Items[index]);
end
else begin
listbox1.Canvas.Font.Color := clblack;
listbox1.Canvas.TextOut(rect.Left,rect.Top,ListBox1.Items[index]);
end;
table2.close;
 
strName := listbox1.Items.Strings[index];

Table2.DisableControls;

table2.First;
for i:=0 to table2.RecordCount - 1 do begin
if(strName=table2.FieldByName('busname').AsString) then begin
strMPI := table2.FieldByName('mpi').AsString;
break;
end;
table2.Next;
end;

Table2.EnableControls;

if(strMPI<>'') then begin
listbox1.Canvas.Brush.Color := clgray;
listbox1.Canvas.Font.Color := clred;
listbox1.Canvas.TextOut(rect.Left,rect.Top,ListBox1.Items[index]);
end
else begin
listbox1.Canvas.Font.Color := clblack;
listbox1.Canvas.TextOut(rect.Left,rect.Top,ListBox1.Items[index]);
end;
table2.close;
 
没有看明白你到底要干什么。不过可以这样邪:

strName := listbox1.Items.Strings[index];
table2.First;
if table2.locate('busname',strName,[]) then
strMPI := table2.FieldByName('mpi').AsString;
if(strMPI<>'') then begin
listbox1.Canvas.Brush.Color := clgray;
listbox1.Canvas.Font.Color := clred;
listbox1.Canvas.TextOut(rect.Left,rect.Top,ListBox1.Items[index]);
end
else begin
listbox1.Canvas.Font.Color := clblack;
listbox1.Canvas.TextOut(rect.Left,rect.Top,ListBox1.Items[index]);
end;
 
我想速度慢的一个原因是这一段
table2.First;
for i:=0 to table2.RecordCount - 1 do begin
if(strName=table2.FieldByName('busname').AsString) then begin
strMPI := table2.FieldByName('mpi').AsString;
break;
end;
table2.Next;
end;

你用Query或是用Locate来查找试试
 
多人接受答案了。
 
后退
顶部