取的Combobox中的所选择项的值

[入库:2005年8月18日] [更新:2007年3月24日]

本文简介:选择自 newsunet 的 blog

有时候我们需要根据combobox(listbox同样)的选择项的值进行处理,但是在delphi中的combobox的item是一个tstrings类型的对象,我们无法象c#或java中那样从combobox的选项类中继承,创建一个我们需要的类来完成任务。但是仔细研究delphi的combobox对象发现了以下的解决方法:

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

titemex=class(tobject)      caption:string;
   public

      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的解决方法与此类似。

www.sinoprise.com

本文关键:取的Combobox中的所选择项的值
  相关方案
Google
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top