我看代码看得头都晕了,我好惨呀!帮帮忙呀

解决方案 »

  1.   

    参考一下这个组件
    { author:zxi
      email:[email protected]
    }unit ZxiDbaseEdit;interfaceuses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
      StdCtrls,db,dbtables,DsgnIntf;type  TMyStringProperty = class(TStringProperty)
      public
        function GetAttributes: TPropertyAttributes; override;
        procedure GetValueList(List: TStrings);virtual; abstract;
        procedure GetValues(Proc: TGetStrProc); override;
      end;  TMyDatabaseNameProperty = class(TMyStringProperty)
      public
        procedure GetValueList(List: TStrings); override;
      end;
      TMyTableNameProperty = class(TMyStringProperty)
      public
        procedure GetValueList(List: TStrings); override;
      end;
      TZxiDbaseEdit = class(TCustomEdit)
      private
        { Private declarations }
        FDataBase:TDatabase;
        FTableName,
        FDatabaseName:String;
        procedure SetTableName(Value:String);
        procedure SetDataBaseName(Value:String);
      protected
        { Protected declarations }
      public
        { Public declarations }
      published
        { Published declarations }
        property Tablename:String read FTableName write SetTableName;
        property Database:TDatabase Read FDataBase write FDataBase;
        property DatabaseName:String read FDatabaseName write SetDataBaseName;
      end;procedure Register;implementationprocedure Register;
    begin
      RegisterComponents('张玺', [TZxiDbaseEdit]);
      RegisterPropertyEditor(TypeInfo(string), TZxiDbaseEdit, 'DatabaseName', TMyDatabaseNameProperty);
      RegisterPropertyEditor(TypeInfo(string), TZxiDbaseEdit, 'Tablename',TMyTableNameProperty);end;{ TDbFieldProperty }function TMyStringProperty.GetAttributes: TPropertyAttributes;
    begin
      Result := [paValueList, paSortList, paMultiSelect];
    end;procedure TMyStringProperty.GetValues(Proc: TGetStrProc);
    var
      I: Integer;
      Values: TStringList;
    begin
      Values := TStringList.Create;
      try
        GetValueList(Values);
        for I := 0 to Values.Count - 1 do Proc(Values[I]);
      finally
        Values.Free;
      end;
    end;{ TMyFieldNameProperty }procedure TMyDatabaseNameProperty.GetValueList(List: TStrings);
    var
      DBAdo : TZxiDbaseEdit;
      ASession:TSession;
    begin
      DBAdo := GetComponent(0) as TZxiDbaseEdit;
      ASession:=TSession.Create(nil);
      Asession.Name:='session_1';
      Asession.AutoSessionName:=true;
      try
        ASession.GetDatabaseNames(List);
      finally
        Asession.free;
      end;
    end;{ TZxiDbaseEdit }procedure TZxiDbaseEdit.SetDataBaseName(Value: String);
    begin
      if FDatabaseName<>Value then
        FDatabaseName:=Value;
    end;procedure TZxiDbaseEdit.SetTableName(Value: String);
    begin
      if FTableName<>Value then
        FTableName:=Value;
    end;{ TMyTableNameProperty }procedure TMyTableNameProperty.GetValueList(List: TStrings);
    var
      DBAdo : TZxiDbaseEdit;
      ASession:TSession;
      DbName:String;
    begin
      DBAdo := GetComponent(0) as TZxiDbaseEdit;
      DbName:=DbAdo.DatabaseName;
      if DbName='' then
        raise exception.Create('请先指定databaseName属性!');
      ASession:=TSession.Create(nil);
      Asession.Name:='session_2';
      Asession.AutoSessionName:=true;
      try
        ASession.GetTableNames(DbName,'',false,false,List);
      finally
        Asession.free;
      end;
    end;end.