属性加载的顺序?(100分)

  • 主题发起人 主题发起人 liujidong
  • 开始时间 开始时间
L

liujidong

Unregistered / Unconfirmed
GUEST, unregistred user!
调试的时候发现,对象创建时,FDataSetIndexIndex,FDataSetActive,
FDataSetIndexShow都读出来了,FDataSet也不是nil,但是FDataSet.Active为False
所以无法取出TDataSet中的数据,猜想和属性及对象建立的顺序有关,请高手赐教!

最好能解释一下创建时的原理,谢谢!

TSelectDataItem = class(TCustomSelectDataItem)
private
// FDataLink: TMyDataLink;
FDataSetActive: Boolean;
FDataSetIndexIndex: Integer;
FDataSetIndexShow: Integer;
FShowName: TStringList;
FIndexChar: TStringList;
FDataSet: TDataSet;

FOnChange: TDataSetNotifyEvent;

procedure SetShowName(const Value: TStringList);
procedure SetIndexChar(const Value: TStringList);
procedure DoOnChange(Sender: TObject);
procedure SetDataSet(const Value: TDataSet);
procedure SetDataSetIndexIndex(const Value: Integer);
procedure SetDataSetIndexShow(const Value: Integer);
procedure SetDataSetActive(const Value: Boolean);
function GetDataSet: TDataSet;
function GetDataSetIndexIndex: Integer;
function GetDataSetIndexShow: Integer;
function GetDataSetActive: Boolean;

procedure GiveShowName;
procedure DoOnDataSetChange(Sender: TDataSet);
protected
procedure MyChange(Sender: TDataSet);
public
constructor Create(Collection: TCollection); override;
Destructor Destroy;override;
procedure Assign(Source : TPersistent); override;

published
property ShowName : TStringList read FShowName write SetShowName;
property IndexChar : TStringList read FIndexChar write SetIndexChar;
property DataSet: TDataSet read GetDataSet write SetDataSet;
property DataSetIndexIndex: Integer read GetDataSetIndexIndex write SetDataSetIndexIndex;
property DataSetIndexShow: Integer read GetDataSetIndexShow write SetDataSetIndexShow;
property DataSetActive: Boolean read GetDataSetActive write SetDataSetActive;
end;
{ TSelectDataItem }

procedure TSelectDataItem.Assign(Source: TPersistent);
var i :Integer;
begin
if Source is TSelectDataItem then
with TSelectDataItem(Source) do
begin
// self.Name := '';
for i:=0 to ShowName.Count-1 do
self.ShowName.Add(ShowName.Strings);
for i:=0 to IndexChar.Count-1 do
self.IndexChar := IndexChar;
self.Name := Name;
end
else
inherited;
end;

constructor TSelectDataItem.Create(Collection: TCollection);
begin
inherited;
// FDataLink := TMyDataLink.Create;
// FDataLink.OnChange := DoOnDataSetChange;
FShowName := TStringList.Create;
FIndexChar := TStringList.Create;
FShowName.OnChange:=DoOnChange;
FIndexChar.OnChange:=DoOnChange;
FOnChange := DoOnDataSetChange;
// OnDataSetChange := DoOnDataSetChange;
end;

destructor TSelectDataItem.Destroy;
begin
inherited;
FShowName.Free;
FIndexChar.Free;
// SetDataSet(Nil);
// FDataLink.Free;
end;

procedure TSelectDataItem.DoOnChange(Sender: TObject);
begin
// GiveShowName;
Changed(False);
end;

procedure TSelectDataItem.DoOnDataSetChange;
begin
GiveShowName;
Changed(False);
end;

procedure TSelectDataItem.GiveShowName;
begin
FShowName.Clear;
if DataSet <> nil then
begin
if DataSetActive then
begin
if DataSet.Active then
begin
with DataSet do
begin
First;
while not Eof do
begin
FShowName.Add(DataSet.Fields[DataSetIndexIndex].AsString+'-'+DataSet.Fields[DataSetIndexShow].AsString);
Next;
end;
end;
end;
end;
end;
end;

procedure TSelectDataItem.SetDataSet(const Value: TDataSet);
begin
FDataSet := Value;
MyChange(Self.DataSet);
// GiveShowName;
// Changed(False);
end;

procedure TSelectDataItem.SetDataSetActive(const Value: Boolean);
begin
if FDataSetActive <> Value then
begin
FDataSetActive := Value;
// GiveShowName;
// Changed(False);
end;
MyChange(Self.DataSet);
end;

procedure TSelectDataItem.SetDataSetIndexShow(const Value: Integer);
begin
if FDataSetIndexShow <> value then
begin
FDataSetIndexShow := Value;
// GiveShowName;
// Changed(False);
end;
MyChange(Self.DataSet);
end;

procedure TSelectDataItem.SetDataSetIndexIndex(const Value: Integer);
begin
if FDataSetIndexIndex <> value then
begin
FDataSetIndexIndex := Value;
// GiveShowName;
// Changed(False);
end;
MyChange(Self.DataSet);
end;

procedure TSelectDataItem.SetIndexChar(const Value: TStringList);
begin
FIndexChar.Assign(Value);
end;

procedure TSelectDataItem.SetShowName(const Value: TStringList);
begin
FShowName.Assign(Value);
end;

function TSelectDataItem.GetDataSet;
begin
Result := FDataSet;
end;

function TSelectDataItem.GetDataSetIndexIndex;
begin
Result := FDataSetIndexIndex;
end;

function TSelectDataItem.GetDataSetIndexShow;
begin
Result := FDataSetIndexShow;
end;

function TSelectDataItem.GetDataSetActive;
begin
Result := FDataSetActive;
end;

procedure TSelectDataItem.MyChange(Sender: TDataSet);
begin
if Assigned(FOnChange) then
FOnChange(Sender);
end;

 
代码太长了,没仔细看,但是因为打开一个数据库需要一定的时间,所以你一样子不能读出数据
应该是正常的,不一并和对象的创建顺序有关。
 
to liujidong:
问一下,好象没看到在哪里把DataSet.Active 设为 True?
 
对不可视控件创建顺序可能是有一定的影响, 要调整的话可以鼠标右键单击form(或
panel等容器), 选择Creation order...来调整
 
多人接受答案了。
 
后退
顶部