TComboBox下拉取值

王朝delphi·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

新建一个类,存储我们需要的数据:

TItemEx=class(TObject)

public

caption:string;

StringValue:string;

end;

//使用adoquery中的值填充combobox

function FillInComBoBoxWithAdoQuery(objAdoQuery:TAdoQuery;objComBoBox:TComboBox;sql:string;captionFieldName:string;

valueFieldName:string;noAsFirst:boolean):boolean;

//当noAsFirst为true是,combobox的第一项是'无'

var

objItemEx:TItemEx;

begin

objComBoBox.Clear;

objComBoBox.ItemIndex:=-1;

if noAsFirst

then begin

objItemEx:=TItemEx.Create;

objItemEx.caption:='无';

objItemEx.StringValue:='';

objComBoBox.Items.AddObject(objItemEx.caption,objItemEx);

objComBoBox.ItemIndex:=0;

end;

objAdoQuery.Close;

objAdoQuery.SQL.Clear;

objAdoQuery.SQL.Add(sql);

objAdoQuery.Open;

objAdoQuery.First;

while not objAdoQuery.Eof do

begin

objItemEx:=TItemEx.Create;

objItemEx.caption:=objAdoQuery.FieldByName(captionFieldName).AsString;

objItemEx.StringValue:=objAdoQuery.FieldByName(valueFieldName).AsString;

objComBoBox.Items.AddObject(objItemEx.caption,objItemEx);

objAdoQuery.Next;

end;

objAdoQuery.close;

result:=true;

end;

//取得comboobx中被选定向的制

function GetComBoBoxSelectedStringValue(objComBoBox:TComboBox):string;

var

objItemEx:TItemEx;

begin

if (objComBoBox.ItemIndex>-1 )

then begin

objItemEx:=(objComBoBox.Items.Objects[objComBoBox.ItemIndex] as TItemEx);

result:=objItemEx.StringValue;

end

else begin

result:='';

end;

end;

listbox的解决方法与此类似。

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航