我的代码也是这样写的,可是一添加组件Delphi6就退出。
unit DBComboFilter;
interface
uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Db, DbCtrls;
type
TDBComboFilter = class(TComboBox)
private
FDataLink: TFieldDataLink;
FDataLinkFilter: TFieldDataLink;
FFilter: string;
FFilterHaveNot: boolean;
FFilterHaveNotStr: string;
function GetDataSource : TDataSource;
function GetDataField : string;
function GetDataSourceFilter: TDataSource;
function GetFilter: string;
function GetFilterHaveNot: boolean;
function GetFilterHaveNotStr: string;
procedure SetDataSource(Value : TDataSource);
procedure SetDataField(const Value : string);
procedure SetDataSourceFilter(Value : TDataSource);
procedure SetFilter(const Value : string);
procedure SetFilterHaveNot(const Value : boolean);
procedure SetFilterHaveNotStr(const Value : string);
procedure DataChange(Sender : Tobject);
procedure CMChanged(var Message: TMessage); message CM_CHANGED;
protected
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
property DataSource: TDataSource read GetDataSource write SetDataSource;
property DataField: string read GetDataField write SetDataField;
property DataSourceFilter: TDataSource read GetDataSourceFilter write SetDataSourceFilter;
property Filter: string read GetFilter write SetFilter;
property FilterHaveNot : boolean read GetFilterHaveNot write SetFilterHaveNot;
property FilterHaveNotStr : string read GetFilterHaveNotStr write SetFilterHaveNotStr;
end;
procedure Register;
implementation
constructor TDBComboFilter.Create;
begin
inherited Create(AOwner);
FDataLink := TFieldDataLink.Create;
FDataLink.OnDataChange := DataChange;
FDataLinkFilter := TFieldDataLink.Create;
FFilterHaveNot := True;
FFilterHaveNotStr := '';
end;
destructor TDBComboFilter.Destroy;
begin
FDataLink.Free;
FDataLink := nil;
FDataLinkFilter.Free;
FDataLinkFilter := nil;
inherited Destroy;
end;
function TDBComboFilter.GetDataSource: TDataSource;
begin
Result := FDataLink.DataSource;
end;
procedure TDBComboFilter.SetDataSource(Value: TDataSource);
begin
FDataLink.DataSource := Value;
end;
function TDBComboFilter.GetDataField: string;
begin
Result := FDataLink.FieldName;
end;
procedure TDBComboFilter.SetDataField(const Value: string);
begin
FDataLink.FieldName := Value;
end;
function TDBComboFilter.GetDataSourceFilter: TDataSource;
begin
Result := FDataLinkFilter.DataSource;
end;
procedure TDBComboFilter.SetDataSourceFilter(Value: TDataSource);
begin
FDataLinkFilter.DataSource := Value;
end;
function TDBComboFilter.GetFilter: string;
begin
Result := FFilter;
end;
procedure TDBComboFilter.SetFilter(const Value: string);
begin
FFilter := Value;
end;
function TDBComboFilter.GetFilterHaveNot : boolean;
begin
Result := FFilterHaveNot;
end;
procedure TDBComboFilter.SetFilterHaveNot(const Value : boolean);
begin
FFilterHaveNot := Value;
end;
function TDBComboFilter.GetFilterHaveNotStr: string;
begin
Result := FFilterHaveNotStr;
end;
procedure TDBComboFilter.SetFilterHaveNotStr(const Value : string);
begin
FFilterHaveNotStr := Value;
end;
procedure TDBComboFilter.DataChange(Sender : Tobject);
var
bok: TBookMark;
begin
if FDataLink.Field <> nil then
begin
//Items.BeginUpdate;
Items.Clear;
if FFilterHaveNot then Items.Add(FFilterHaveNotStr);
with FDataLink.DataSource.DataSet do
begin
bok := GetBookmark;
first;
while not eof do
begin
Items.Add(FieldByName(FDataLink.FieldName).Value);
next;
end;
GotoBookMark(bok);
FreeBookMark(bok);
bok := nil;
end;
//Items.EndUpdate;
end;
end;
procedure TDBComboFilter.CMChanged(var Message: TMessage);
begin
if FDataLink.Field <> nil then
begin
with FDataLink.DataSource.DataSet do
begin
Filtered := false;
if Items[ItemIndex] <> FFilterHaveNotStr then
begin
Filter := Items[ItemIndex];
Filtered := true;
end;
end;
end;
end;
procedure Register;
begin
RegisterComponents('Other Controls', [TDBComboFilter]);
end;
end.