写入combobox数据的函数是这样的:
Procedure cb_additem(adocon:TadoConnection;combo: TComboBox;sql: String;sField:string;sID:string);
var
i:integer;
ado:TADODataSet;
arr_id:array of integer;
begin combo.Clear;
ado := TADODataSet.Create(nil);
ado.Connection := adocon;
ado.CommandText := Str;
ado.Open;
combo.clear;
ado.first;
SetLength(arr_id,ado.RecordCount);
for i := 1 to ado.RecordCount do
begin
arr_id[i]:=ado.FieldByName(sID).AsInteger;
combo.Items.AddObject(ado.FieldByName(sID).AsString+'|'+ado.FieldByName(sField).AsString,Tobject(arr_id[i]));
ado.Next;
end;
combo.ItemIndex := 0;
ado.close;
ado.Free;
end;function cb_getid(combo: TComboBox):integer;
var id:integer;
begin
//showmessage('index:'+inttostr(combo.itemindex));
id:=Integer(combo.items.objects[combo.itemindex]);
result:=id;
end;function cb_getindex(combo: TComboBox;id:string):integer;
var sid,i,j,tid:integer;
begin
j:=0;
tid:=StrToint(id);
for i:=0 to (combo.Items.Count-1) do
begin
sid:=Integer(combo.items.objects[i]);
if sid=tid then
begin j:=1; break; end;
end;
if j=1 then result:=i else result:=0;end;//-----------------------------------------------------
把ID写入数据表是这样的:
DM.AdoQ['fd_fac_bankno']:=BankNoEt.Text;
DM.AdoQ.FieldByName('fd_fac_typeid').AsInteger:= cb_getid(TypeComb);
运行有时正常,有时退出时,会说内存出错.
断点出错就在写入的这一句,奇怪.
大家帮帮忙.
Procedure cb_additem(adocon:TadoConnection;combo: TComboBox;sql: String;sField:string;sID:string);
var
i:integer;
ado:TADODataSet;
arr_id:array of integer;
begin combo.Clear;
ado := TADODataSet.Create(nil);
ado.Connection := adocon;
ado.CommandText := Str;
ado.Open;
combo.clear;
ado.first;
SetLength(arr_id,ado.RecordCount);
for i := 1 to ado.RecordCount do
begin
arr_id[i]:=ado.FieldByName(sID).AsInteger;
combo.Items.AddObject(ado.FieldByName(sID).AsString+'|'+ado.FieldByName(sField).AsString,Tobject(arr_id[i]));
ado.Next;
end;
combo.ItemIndex := 0;
ado.close;
ado.Free;
end;function cb_getid(combo: TComboBox):integer;
var id:integer;
begin
//showmessage('index:'+inttostr(combo.itemindex));
id:=Integer(combo.items.objects[combo.itemindex]);
result:=id;
end;function cb_getindex(combo: TComboBox;id:string):integer;
var sid,i,j,tid:integer;
begin
j:=0;
tid:=StrToint(id);
for i:=0 to (combo.Items.Count-1) do
begin
sid:=Integer(combo.items.objects[i]);
if sid=tid then
begin j:=1; break; end;
end;
if j=1 then result:=i else result:=0;end;//-----------------------------------------------------
把ID写入数据表是这样的:
DM.AdoQ['fd_fac_bankno']:=BankNoEt.Text;
DM.AdoQ.FieldByName('fd_fac_typeid').AsInteger:= cb_getid(TypeComb);
运行有时正常,有时退出时,会说内存出错.
断点出错就在写入的这一句,奇怪.
大家帮帮忙.
ado.CommandText := Str; //这个str是全局的?参数sql好像没有用到
cb_additem(DM.AdoCon,TypeComB,'select * from tb_factype order by fd_fty_id asc','fd_fty_name','fd_fty_id');
for i := 1 to ado.RecordCount do
begin
arr_id[i]:=ado.FieldByName(sID).AsInteger; //这里的i从1开始,那么到循环的最后有可能超出arr_id的下标索引,导致出错
//这句能贴全吗
combo.Items.AddObject(ado.FieldByName(sID).AsString+'|'+ado.FieldByName(sField).AsString,Tobject(arr_id
begin
arr_id[i]:=ado.FieldByName(sID).AsInteger;
combo.Items.AddObject(ado.FieldByName(sID).AsString+'|'+ado.FieldByName(sField).AsString,Tobject(arr_id[i]));
ado.Next;
end; 楼主的这个循环会出错的!
就是取数组里的内容时下标越界。delphi里的数组和c/c++一样,下标从零开始以后使用任何有count或者recordcout属性的控件,要遍历,都要写
for i:=0 to XX.count-1 do //或者是XX.recordcount-1
begin
//....要执行的操作
end;如果不用for而用while,比如用TQuery控件qry1,查询好数据后
while not eof do
begin
//要执行的操作
next;
end;楼主要反省一下了,这种小细节应该要时时注意的