自己重载控件 unit myDBCombo; interfaceuses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls,DB;type TcolorDBCombo = class(TComboBox) private { Private declarations } { Private declarations } FSeparator:string; FCurrentID:string; // FCurrentData:string; Fstringlist:TstringList; //在STRINGLIST中包含两部分,前部分是AINDEXFIELD,后面 FSpaceText:string; //部分是ADATAFIELD,中间用¦隔开 FAddtionalData:Boolean; bShowSpace:Boolean; bShowID:Boolean; function GetCurrentID:string; function GetCurrentData:string; function GetIndexData(const IndexID:integer):string; procedure SetKeyValue(sVal:string); protected { Protected declarations } public { Public declarations } property ItemIndex; property Datas[const IndexID:integer]:string read GetindexData; procedure FillItems(ADataset:TDataset;AShowField:string;AIndexField:string;ADataField:string); published { Published declarations } property style; property Items; property Separaror:string read Fseparator write Fseparator; property ShoeID:Boolean read bShowID write bShowID; property ShowSpace:Boolean read bShowSpace write bShowSpace; property AddtionalData:Boolean read FAddtionalData write FAddtionalData; property SpaceText:string read FSpaceText write FSpaceText; property CurrentID:string read GetCurrentID; property CurrentData:string read GetCurrentData; property KeyValue:string write SetKeyValue; property OnKeyDown; constructor create(AOwner :TComponent);override; destructor destroy;override; end;procedure Register;implementationprocedure Register; begin RegisterComponents('Standard', [TcolorDBCombo]); end;constructor TcolorDBCombo.create(AOwner:Tcomponent); begin inherited; Fseparator:=''; FCurrentID:=''; bShowSpace:=false; bShowID:=true; FAddtionalData:=False; Self.style:=csDropDownList ; FstringList:=TstringList.Create; end;destructor TcolorDBCombo.destroy; begin FstringList.Free; inherited; end;procedure TcolorDBCombo.FillItems(ADataset:TDataset;AShowField:string;AIndexField:string;ADataField:string); var ABookMark:TBookMark;begin ABookMark:=ADataSet.GetBookMark; Self.Items.clear; FstringList.clear; if ADataSet.Active then begin if bShowSpace then begin if FAddtionalData then begin Self.Items.Add(FSpaceText); FstringList.Add(''+'¦'+''); end else begin Self.Items.Add(FSpaceText); FstringList.Add(''); end; end; try ADataSet.First; while not ADataSet.Eof do begin if AIndexField<>'' then begin if not bShowID then begin Self.Items.Add(ADataSet.FieldByName(AShowField).AsString); if FAddtionalData then FstringList.Add(ADataSet.FieldByName(AIndexField).AsString+ '¦'+ADataSet.FieldByName(ADataField).AsString) else FstringList.Add(ADataSet.FieldByName(AIndexField).AsString); end else begin Self.Items.Add(ADataSet.FieldByName(AIndexField).AsString + FSeparator + ADataSet.FieldByName(AShowField).AsString); if FAddtionalData then FstringList.Add(ADataSet.FieldByName(AIndexField).AsString+ '¦'+ADataSet.FieldByName(ADataField).AsString) else FstringList.Add(ADataSet.FieldByName(AIndexField).AsString); end; end else Self.Items.Add(ADataSet.FieldByName(AShowField).AsString); ADataSet.Next; end; except MessageBox(Handle,'填写下拉框错误,请检查字段是否存在!','错误',MB_ICONERROR); end;//try..except ADataSet.GotoBookMark(ABookMark); ADataSet.FreeBook(ABookMark); end else MessageBox(Handle,Pchar(ADataSet.Name+'没有打开'),'错误',MB_ICONERROR); Self.ItemIndex:=0;//Display First Item end;function TcolorDBCombo.GetCurrentID:string; var str:string; begin if ItemIndex <> -1 then begin if FAddtionalData then begin str:= FStringList.Strings[ItemIndex]; Result:=copy(str,1,Pos('¦',str)-1); end else Result:=FStringList.Strings[ItemIndex]; end else Result := ''; end;function TcolorDBCombo.GetCurrentData:string; var str:string; begin if ItemIndex<>-1 then begin if FAddtionalData then begin str:= FStringList.Strings[ItemIndex]; Result:=copy(str,Pos('¦',str)+1,Length(str)-Pos('¦',str)); end else Result:=FStringList.Strings[ItemIndex]; end else Result:=''; end;function TcolorDBCombo.GetIndexData(const IndexID:integer):string; var str:string; begin if FAddtionalData and (IndexID<>-1) then begin str:=FStringList.Strings[IndexID]; Result:=copy(str,Pos('¦',str)+1,Length(str)-Pos('¦',str)); end else result:=''; end;procedure TcolorDBCombo.SetKeyValue(sVal: string); var Index:Integer; str:string; begin if sVal='' then exit; for Index:=0 to FStringList.Count-1 do begin if FAddtionalData then begin str:=FStringList.Strings[Index]; str:=copy(str,Pos('¦',str)+1,Length(str)-Pos('¦',str)); if str=sVal then begin self.ItemIndex:=Index; Exit; end; end else begin str:=FStringList.Strings[Index]; if str=sVal then begin self.ItemIndex:=Index; Exit; end; end; end;end;end.
我试过liuting(游子)的了,是不是我写错了,下面为错误代码为: combobox1.Items.Addobject(fidrst.fields['item'],tobject(fidrst.fields['ID'])); [error]invalid typecast strsel1:=combobox1.Items.Objects[combobox1.itemindex]; [error]imcompatible type:'string' and 'tobject'
用一个数组存放ID,ID:array [0..100] of string ,取id时: id_val:=id[combobox.itemindex] 不就行了吗?
index:=combobox.items.indexof(字符串),you know?
combobox1.Items.Addobject(fidrst.fields['item'],tobject(fidrst.fields['ID'])); [error]invalid typecast strsel1:=combobox1.Items.Objects[combobox1.itemindex]; [error]imcompatible type:'string' and 'tobject' 那么我应怎么写上面那两句?
为什么不作一个record :两个域,一个ID、一个Name。 把那些东西写道数组 of Record中,增加Items的时候,按数组的下标取Name,取出来的时候用itemindex 作数组下标取ID
rec = record id : string; name : string; end pRec = ^Rec;Write : var p:pRec; new(p); p^.id:="001"; p^.name:="go"; combobox1.items.addobject("hell0",Tobject(p))read:showmessage(pRec(combobox1.items.Objects[index])^.Fid);
ComBobox1.Items.AddObject(item, TObject(id));这样就可以对应上了,
ComBobox.Items.Objects[j]可以得到相应的值。
unit myDBCombo;
interfaceuses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls,DB;type
TcolorDBCombo = class(TComboBox)
private
{ Private declarations }
{ Private declarations }
FSeparator:string;
FCurrentID:string;
// FCurrentData:string;
Fstringlist:TstringList; //在STRINGLIST中包含两部分,前部分是AINDEXFIELD,后面
FSpaceText:string; //部分是ADATAFIELD,中间用¦隔开
FAddtionalData:Boolean;
bShowSpace:Boolean;
bShowID:Boolean; function GetCurrentID:string;
function GetCurrentData:string;
function GetIndexData(const IndexID:integer):string;
procedure SetKeyValue(sVal:string);
protected { Protected declarations }
public
{ Public declarations }
property ItemIndex;
property Datas[const IndexID:integer]:string read GetindexData;
procedure FillItems(ADataset:TDataset;AShowField:string;AIndexField:string;ADataField:string);
published
{ Published declarations }
property style;
property Items;
property Separaror:string read Fseparator write Fseparator;
property ShoeID:Boolean read bShowID write bShowID;
property ShowSpace:Boolean read bShowSpace write bShowSpace;
property AddtionalData:Boolean read FAddtionalData write FAddtionalData;
property SpaceText:string read FSpaceText write FSpaceText;
property CurrentID:string read GetCurrentID;
property CurrentData:string read GetCurrentData;
property KeyValue:string write SetKeyValue;
property OnKeyDown;
constructor create(AOwner :TComponent);override;
destructor destroy;override;
end;procedure Register;implementationprocedure Register;
begin
RegisterComponents('Standard', [TcolorDBCombo]);
end;constructor TcolorDBCombo.create(AOwner:Tcomponent);
begin
inherited;
Fseparator:='';
FCurrentID:='';
bShowSpace:=false;
bShowID:=true;
FAddtionalData:=False;
Self.style:=csDropDownList ;
FstringList:=TstringList.Create;
end;destructor TcolorDBCombo.destroy;
begin
FstringList.Free;
inherited;
end;procedure TcolorDBCombo.FillItems(ADataset:TDataset;AShowField:string;AIndexField:string;ADataField:string);
var ABookMark:TBookMark;begin
ABookMark:=ADataSet.GetBookMark;
Self.Items.clear;
FstringList.clear;
if ADataSet.Active then
begin if bShowSpace then
begin
if FAddtionalData then
begin
Self.Items.Add(FSpaceText);
FstringList.Add(''+'¦'+'');
end
else
begin
Self.Items.Add(FSpaceText);
FstringList.Add('');
end;
end;
try
ADataSet.First;
while not ADataSet.Eof do
begin
if AIndexField<>'' then
begin
if not bShowID then
begin
Self.Items.Add(ADataSet.FieldByName(AShowField).AsString);
if FAddtionalData then
FstringList.Add(ADataSet.FieldByName(AIndexField).AsString+
'¦'+ADataSet.FieldByName(ADataField).AsString)
else
FstringList.Add(ADataSet.FieldByName(AIndexField).AsString);
end
else
begin
Self.Items.Add(ADataSet.FieldByName(AIndexField).AsString +
FSeparator + ADataSet.FieldByName(AShowField).AsString);
if FAddtionalData then
FstringList.Add(ADataSet.FieldByName(AIndexField).AsString+
'¦'+ADataSet.FieldByName(ADataField).AsString)
else
FstringList.Add(ADataSet.FieldByName(AIndexField).AsString);
end;
end
else
Self.Items.Add(ADataSet.FieldByName(AShowField).AsString);
ADataSet.Next;
end;
except
MessageBox(Handle,'填写下拉框错误,请检查字段是否存在!','错误',MB_ICONERROR);
end;//try..except
ADataSet.GotoBookMark(ABookMark);
ADataSet.FreeBook(ABookMark);
end
else
MessageBox(Handle,Pchar(ADataSet.Name+'没有打开'),'错误',MB_ICONERROR);
Self.ItemIndex:=0;//Display First Item
end;function TcolorDBCombo.GetCurrentID:string;
var str:string;
begin
if ItemIndex <> -1 then
begin
if FAddtionalData then
begin
str:= FStringList.Strings[ItemIndex];
Result:=copy(str,1,Pos('¦',str)-1);
end
else
Result:=FStringList.Strings[ItemIndex];
end
else
Result := '';
end;function TcolorDBCombo.GetCurrentData:string;
var str:string;
begin
if ItemIndex<>-1 then
begin
if FAddtionalData then
begin
str:= FStringList.Strings[ItemIndex];
Result:=copy(str,Pos('¦',str)+1,Length(str)-Pos('¦',str));
end
else
Result:=FStringList.Strings[ItemIndex];
end
else
Result:='';
end;function TcolorDBCombo.GetIndexData(const IndexID:integer):string;
var str:string;
begin
if FAddtionalData and (IndexID<>-1) then
begin
str:=FStringList.Strings[IndexID];
Result:=copy(str,Pos('¦',str)+1,Length(str)-Pos('¦',str));
end
else
result:='';
end;procedure TcolorDBCombo.SetKeyValue(sVal: string);
var Index:Integer;
str:string;
begin
if sVal='' then exit;
for Index:=0 to FStringList.Count-1 do begin
if FAddtionalData then begin
str:=FStringList.Strings[Index];
str:=copy(str,Pos('¦',str)+1,Length(str)-Pos('¦',str));
if str=sVal then begin
self.ItemIndex:=Index;
Exit;
end;
end else begin
str:=FStringList.Strings[Index];
if str=sVal then begin
self.ItemIndex:=Index;
Exit;
end;
end; end;end;end.
和你一样的做法
combobox1.Items.Addobject(fidrst.fields['item'],tobject(fidrst.fields['ID']));
[error]invalid typecast
strsel1:=combobox1.Items.Objects[combobox1.itemindex];
[error]imcompatible type:'string' and 'tobject'
id_val:=id[combobox.itemindex]
不就行了吗?
[error]invalid typecast
strsel1:=combobox1.Items.Objects[combobox1.itemindex];
[error]imcompatible type:'string' and 'tobject'
那么我应怎么写上面那两句?
把那些东西写道数组 of Record中,增加Items的时候,按数组的下标取Name,取出来的时候用itemindex 作数组下标取ID
id : string;
name : string;
end
pRec = ^Rec;Write :
var p:pRec;
new(p);
p^.id:="001";
p^.name:="go";
combobox1.items.addobject("hell0",Tobject(p))read:showmessage(pRec(combobox1.items.Objects[index])^.Fid);