各们大富翁,帮帮忙。。。(50分)

  • 主题发起人 主题发起人 wab
  • 开始时间 开始时间
W

wab

Unregistered / Unconfirmed
GUEST, unregistred user!
我在扩展了TTreeview的代码中定义了一个Active的属性,并且在Active 的read的代码中
调用到ADOConnection控件读取数据库,当将Active初始值设为True时,运行时就会出错,
而初始设为False,在运行表单后执行Active:=true又能正常运行,这是什么原因啊??
 
你得看看哪里的代码与Active 有关,检查一下。
 
笨一点,将源代码贴出来,大家帮我诊断诊断。。。


unit Pub;
interface

uses SysUtils, Types, Classes, ComCtrls, Graphics, Controls, ADODB;

type
TkqTreeView = class(TTreeView)
private
FOrganization: string;
FConn: TADOConnection;
FActive: Boolean;
FDeptTable: WideString;
FEmployeeTable: WideString;
FinCludeEmp:Boolean;
procedure AddTreeView(ParentNode: TTreeNode; DeptRs: _Recordset; Level: Integer);
procedure SetOrganization(Value: string);
procedure SetConn(Value: TADOConnection);
procedure SetActive(Value: Boolean);
procedure SetDeptTable(Value: WideString);
procedure SetEmployeeTable(Value: WideString);
procedure SetIncludeEmp(Value :Boolean);
protected
public
constructor Create(Aowner: TComponent); override;
destructor Destroy; override;
function AddTreeNode(ParentNode: TTreeNode; Text: string; xType: Integer): TTreeNode;
procedure RefreshTreeView;
published
property Organization: string read FOrganization write SetOrganization;
property Connection: TADOConnection read FConn write SetConn;
property Active: Boolean read FActive write SetActive;
property DeptTable: WideString read FDeptTable write SetDeptTable;
property EmployeeTable: WideString read FEmployeeTable write SetEmployeeTable;
property IncludeEmployee :Boolean read FincludeEmp write setIncludeEmp;


end;

procedure Register;

implementation

constructor TkqTreeView.Create(Aowner: TComponent);
begin
inherited Create(AOwner);
FActive := False;
FOrganization := '#########';
FDeptTable := 'Dept';
FEmployeeTable := 'PersonnelTable';
FincludeEmp := False;
end;

destructor TkqTreeView.Destroy;
begin
inherited Destroy;
end;

procedure Register;
begin
RegisterComponents('Click', [TkqTreeView]);
end;

procedure TkqTreeView.SetOrganization(Value: string);
begin
FOrganization := Value;
if Items.Count > 0 then
Items[0].Text := Value;
end;

procedure TkqTreeView.SetConn(Value: TADOConnection);
begin
FConn := Value;
if FActive then SetActive(False);
end;

procedure TkqTreeView.SetActive(Value: Boolean);
begin
FActive := Value;
if Value then RefreshTreeView
else Items.Clear;
end;

procedure TkqTreeView.SetDeptTable(Value: WideString);
begin
FDeptTable := Value;
if FActive then SetActive(False);
end;

procedure TkqTreeView.SetEmployeeTable(Value: WideString);
begin
FEmployeeTable := Value;
if FActive then SetActive(False);
end;

//向目录树添加一个树结点,类型根据xType决定
function TkqTreeView.AddTreeNode(ParentNode: TTreeNode; Text: string; xType: Integer): TTreeNode;
begin
Result := Items.AddChild(ParentNode, Text);
Result.ImageIndex := xType;
Result.SelectedIndex := xType;
Result.StateIndex := xType;
Result.Data := Pointer(xType);
end;

procedure TkqTreeView.AddTreeView(ParentNode: TTreeNode; DeptRs: _Recordset; Level: Integer);
var
Rs: _Recordset;
i, j: Integer;
Node: TTreeNode;
bh,Name: string;
SQL: string;
begin
for i := 0 to DeptRs.RecordCount - 1 do
begin
bh := DeptRs.Fields['ID'].Value;
Name :=DeptRs.Fields['BmName'].Value;
//增加一个新结点
Node := AddTreeNode(ParentNode, bh + ' ' + Name, 1);

SQL := 'SELECT * FROM ' + FDeptTable + ' WHERE bmLevel=' + IntToStr(Level+1) + ' AND ParentID="' + bh + '"';
// AddTreeView(Node, MainForm.ADOConn.Execute('exec _GetDeptBySub WITH "' + bh + '"'), Level+1);
AddTreeView(Node, FConn.Execute(SQL), Level+1);
If FincludeEmp then //是否包含人员信息
Begin
Rs := FConn.Execute('select * from ' + FEmployeeTable + ' where ssbm="' + bh + '"');
for j := 0 to Rs.RecordCount - 1 do
begin
//增加一个新结点
AddTreeNode(Node, Rs.Fields['bh'].Value + ' ' + Rs.Fields['xm'].Value, 2);
Rs.MoveNext;
end;
end;
DeptRs.MoveNext;

end;
end;

procedure TkqTreeView.RefreshTreeView;
begin
Items.Clear;
with Items.Add(nil, FOrganization) do
begin
Data := Pointer(0);
ImageIndex := 0;
SelectedIndex := 0;
StateIndex := 0;
end;

AddTreeView(Items.GetFirstNode, FConn.Execute('SELECT * FROM ' + FDeptTable + ' WHERE bmLevel=0'), 0);

FullExpand;
end;

procedure TkqTreeView.SetIncludeEmp(Value: Boolean);
begin
FincludeEmp := Value;
end;

end.
 
怎没人帮我看看啊??[V]
 
具体是什么错误啊
有可能是某些对象没有初始化
 
要靠自己才行啊!
 
To daoba_wolf:
大哥,我就搞了半天都没搞出来,才贴出来的啊,我现在只能在表单的Create代码将
其的Active 设为True ,来激活它。可我就是搞不懂如何解决这个问题??
 

设计时为 True 时,没有执行相应的代码,所以你的程序会出错。
程序运行时设置为 true ,执行了 set...中的代码,所以不会错。

这样来看,你的最终处理是合理的。
 
太长了没时间,帮你up一下
 
也不告诉出错信息,怎么帮你,我们的时间也是有限的
 
问题解决了,分分了。。。
 

Similar threads

回复
0
查看
848
不得闲
回复
0
查看
1K
不得闲
S
回复
0
查看
1K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
900
SUNSTONE的Delphi笔记
S
后退
顶部