关于TREEVIEW的一个问题,各位帮忙呀(50分)

M

mylem

Unregistered / Unconfirmed
GUEST, unregistred user!
我用showmodal的方法显示了一个窗口abc,它上面有一个Treeview控件treeview1,
并显示一个树形列表,我要向树形表中添加项目,为此点击abc窗口上的"新建"按钮
用showmodal方法再出现一个窗口qwe,在新窗口中得到数据后,我想更新前一窗口abc中
的Treeview1中的内容,所以在qwe窗口关闭前我用abc.treeview1.items.clear;方法
清除treeview中的原有内容,并重新添加所有项目,但abc.treeview1.items.clear
总出错,这是为什么呢?我该怎么办呀?
 
把你的代码贴上来吧。
 
在 qwe 的 implementation 后添加
uses abc;
 
这么初等的问题我想mylem早想到了。
 
可以传变量来解决,清除和增加都回到ABC窗口来完成就不会出错了
 
想法没错呀,你把代码贴出来看看吧
 
最好连你的窗体的属性文件也提交一下。
 
abc必须是全局变量。
 
TO:mylem
你两个showmodal的窗体都是动态创建的是不是,如果是
那 (Tabc(qwe.Owner)).treeview1.items.clear;
或(Tabc(self.Owner)).treeview1.items.clear;
不过你建qwe的时候要是:qwe:=Tqwe.Create(abc);
 
代码如下:
unit manager;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComCtrls, DB, ADODB;
type
TManagerFrm = class(TForm)
TreeView1: TTreeView;
CloseBtn: TButton;
GroupBox1: TGroupBox;
NewBtn: TButton;
EditBtn: TButton;
DelBtn: TButton;
procedure FormCreate(Sender: TObject);
procedure CloseBtnClick(Sender: TObject);
procedure NewBtnClick(Sender: TObject);
procedure EditBtnClick(Sender: TObject);
procedure IniTreeView1();
procedure DelBtnClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }

end;

var
ManagerFrm: TManagerFrm;
NewEditFlag:boolean
/////////////用来标记是新建还是编辑操作员,True为新建
implementation
uses newoperator,datamodule;
{$R *.dfm}

procedure TManagerFrm.CloseBtnClick(Sender: TObject);
begin
close;
end;

procedure TManagerFrm.NewBtnClick(Sender: TObject);
var
NewOperator:TnewoperatorFrm;
begin
neweditflag:=true
///////新建操作员
NewOperator:=Tnewoperatorfrm.Create(application);
Newoperator.ShowModal

end;

procedure TManagerFrm.EditBtnClick(Sender: TObject);

var
NewOperator:TnewoperatorFrm;
Operatorname:string;
begin
neweditflag:=false
/////////编辑操作员

Operatorname:=treeview1.Selected.Text

with datamodule1.ManagerADOQuery1 do
if recordcount<>0 then
begin
first;
////////////////////查找当前所选操作员信息
while (fieldbyname('Operator_name').Value<>Operatorname) and (not eof) do
next;
if not eof then /////////找到了
begin
NewOperator:=Tnewoperatorfrm.Create(application);
///////////用原操作员作信息初始化各控件的值
newoperator.ComboBox1.Text:=fieldbyname('OperatorRightText').Value
newoperator.operatorIDedit.Text:=fieldbyname('Operator_ID').Value;
newoperator.operatornameedit.Text:=fieldbyname('Operator_name').Value;
newoperator.operatorpasswordedit.Text:=fieldbyname('Operator_password').Value;
newoperator.affirmpasswordedit.Text:=fieldbyname('Operator_password').Value;
newoperator.recordid:=fieldbyname('ID').Value;
Newoperator.Showmodal
end;
end;
///////////////////////////////////////

end;

procedure TManagerFrm.IniTreeView1;
var
aNode: TTreeNode;
begin
treeview1.Items.Clear
////////////////////显示为此语句出错
with datamodule1.ManagerADOQuery1 do ////用来查询操作员类别,如管理员还是一般操作员
begin
CLOSE;
sql.Clear;
sql.add('select * from operator');
TRY
OPEN;
EXCEPT
EXECSQL
end
////执行查询

if recordcount<>0 then
begin
first;
/////////////////将各类别列于treeview中
while (not eof) do
begin
aNode := TreeView1.Items.GetFirstNode;
while (aNode<>nil) do ///////找到节点要插入的父节点。
if aNode.Text=FieldByName('Operator_name').AsString then
break
else
aNode := aNode.GetNextSibling;

if aNode=nil then
aNode := Treeview1.items.AddChild(nil,FieldByName('OperatorRightText').AsString);

Treeview1.items.AddChild(aNode, FieldByName('Operator_name').AsString);
next;
end;
///////////////////////////////////////
end;

end;

end;///// 过程结束

procedure TManagerFrm.FormCreate(Sender: TObject);
begin
IniTreeview1;
end;


end.


unit NewOperator;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TNewOperatorFrm = class(TForm)
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
Label5: TLabel;
ComboBox1: TComboBox;
OperatorIDEdit: TEdit;
OperatorNameEdit: TEdit;
OperatorPasswordEdit: TEdit;
AffirmPasswordEdit: TEdit;
OKBtn: TButton;
CancelBtn: TButton;
procedure CancelBtnClick(Sender: TObject);
procedure OKBtnClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
RecordID:longint;//////////编辑记录的ID号,即数据库中某记录的唯一标识
end;
var
NewOperatorFrm: TNewOperatorFrm;
implementation
uses manager, DataModule;
{$R *.dfm}

procedure TNewOperatorFrm.CancelBtnClick(Sender: TObject);
begin
close;
end;

procedure TNewOperatorFrm.OKBtnClick(Sender: TObject);
var
ORight:integer
////////保存操作员的权限,值为1、2。1为管理员,2为一般操作员
begin
/////////首先检查输入的内容
if (combobox1.Text='') or (operatorIDedit.text='') or (operatornameedit.Text='') or (operatorpasswordedit.Text ='') or (affirmpasswordedit.Text='') then
begin
messagedlg('操作员帐号信息各栏目均不能为空,请重新填写!',mtInformation,[mbOk], 0);
exit;
end;
if operatorpasswordedit.Text <> affirmpasswordedit.Text then
begin
messagedlg('密码与确认密码不同,请重新输入密码!',mtInformation,[mbOk], 0);
exit;
end;
///// ////////////////////////
if combobox1.Text='系统管理员' then
ORight:=1 //////// 1为管理员
else
ORight:=2
////////2为一般操作员
/////////////////////////////////
if manager.NewEditFlag =true then
with datamodule1.ManagerADOQuery1 do ////用来查询操作员类别,如管理员还是一般操作员
begin
CLOSE;
sql.Clear;
sql.Add('insert into operator(operator_name,operator_ID,operator_password,operator_right,OperatorRightText) values('''+Operatornameedit.Text+''','''+OperatorIDedit.Text+''','''+operatorpasswordedit.Text+''','+inttostr(ORight)+','''+combobox1.Text+''')');

EXECSQL
close;
end
/////////////////执行插入新记录
else
///////为编辑已有记录,则使用ID更新该记录
with datamodule1.ManagerADOQuery1 do ////用来查询操作员类别,如管理员还是一般操作员
begin
CLOSE;
sql.Clear;
sql.Add('update operator set operator_name='''+Operatornameedit.Text+''',operator_ID='''+OperatorIDedit.Text+''',operator_password='''+operatorpasswordedit.Text+''',operator_right='+inttostr(ORight)+',OperatorRightText='''+combobox1.Text+''' where ID='+inttostr(recordID)+'');
EXECSQL
close;
end


managerfrm.IniTreeView1 ////此处即刷原窗体中的Treeview中显示的值,但调用即出错
close;
end;

end.
 
To:mylem
代码已看按照代码上面做就可以了,请你把形如:
NewOperator:=Tnewoperatorfrm.Create(application);
的都该成
NewOperator:=Tnewoperatorfrm.Create(self);
按照我的那样访问就可以了,祝你好运!
 
to:socool_100
还是不行:改后的代码如下:
procedure TManagerFrm.EditBtnClick(Sender: TObject);

var
NewOperator:TnewoperatorFrm;
Operatorname:string;
begin
neweditflag:=false
/////////编辑操作员

Operatorname:=treeview1.Selected.Text

with datamodule1.ManagerADOQuery1 do
if recordcount<>0 then
begin
first;
////////////////////查找当前所选操作员信息

while (fieldbyname('Operator_name').Value<>Operatorname) and (not eof) do
next;
if not eof then /////////找到了
begin
NewOperator:=Tnewoperatorfrm.Create(self);
///////////用原操作员作信息初始化各控件的值
newoperator.ComboBox1.Text:=fieldbyname('OperatorRightText').Value
newoperator.operatorIDedit.Text:=fieldbyname('Operator_ID').Value;
newoperator.operatornameedit.Text:=fieldbyname('Operator_name').Value;
newoperator.operatorpasswordedit.Text:=fieldbyname('Operator_password').Value;
newoperator.affirmpasswordedit.Text:=fieldbyname('Operator_password').Value;
newoperator.recordid:=fieldbyname('ID').Value;

Newoperator.Showmodal
end;
end;
///////////////////////////////////////

end;

procedure TNewOperatorFrm.OKBtnClick(Sender: TObject);
var
ORight:integer
////////保存操作员的权限,值为1、2。1为管理员,2为一般操作员

begin
/////////首先检查输入的内容
if (combobox1.Text='') or (operatorIDedit.text='') or (operatornameedit.Text='') or (operatorpasswordedit.Text ='') or (affirmpasswordedit.Text='') then

begin
messagedlg('操作员帐号信息各栏目均不能为空,请重新填写!',mtInformation,[mbOk], 0);
exit;
end;
if operatorpasswordedit.Text <> affirmpasswordedit.Text then
begin
messagedlg('密码与确认密码不同,请重新输入密码!',mtInformation,[mbOk], 0);
exit;
end;
///// ////////////////////////
if combobox1.Text='系统管理员' then
ORight:=1 //////// 1为管理员
else
ORight:=2
////////2为一般操作员
/////////////////////////////////
if manager.NewEditFlag =true then
with datamodule1.ManagerADOQuery1 do ////用来查询操作员类别,如管理员还是一般操作员
begin
CLOSE;
sql.Clear;
sql.Add('insert into operator(operator_name,operator_ID,operator_password,operator_right,OperatorRightText) values('''+Operatornameedit.Text+''','''+OperatorIDedit.Text+''','''+operatorpasswordedit.Text+''','+inttostr(ORight)+','''+combobox1.Text+''')');

EXECSQL
close;
end
/////////////////执行插入新记录
else
///////为编辑已有记录,则使用ID更新该记录

with datamodule1.ManagerADOQuery1 do ////用来查询操作员类别,如管理员还是一般操作员
begin
CLOSE;
sql.Clear;


sql.Add('update operator set operator_name='''+Operatornameedit.Text+''',operator_ID='''+OperatorIDedit.Text+''',operator_password='''+operatorpasswordedit.Text+''',operator_right='+inttostr(ORight)+',OperatorRightText='''+combobox1.Text+''' where ID='+inttostr(recordID)+'');

EXECSQL
close;
end
close;
(Tmanagerfrm(newoperatorfrm.Owner)).TreeView1.Items.Clear
////此语句出错,提示如下:
managerfrm.IniTreeView1

end;
错误提示:project sdiapp.exe raised exception class eaccessviolation with
message 'access violation at address 004fc061 in module 'sdiapp.exe'.read of
address 00000004'.process stopped.use step or run to continue.
 
managerfrm.IniTreeView1 //不能这样访问的,你把它注释掉,可不可以再告诉我。
是这句错了吧?
 
to:soocool_100
managerfrm.IniTreeView1 这句没错,IniTreeView1是我在managerfrm中定义的一个函数,
,你能在前面贴的代码中看见,把它注释掉了,还不行,提示还是
(Tmanagerfrm(newoperatorfrm.Owner)).TreeView1.Items.Clear
这句出错。
 
不是吧?
你打(Tmanagerfrm(newoperatorfrm.owner)). 以后会不会有TreeView1出来啊,如果有,
那就说明这样访问是正确的,错误是你忘了把newoperatorfrm从
Auto-create forms移到Available forms吧,你看看是不是?
 
我建议你将
managerfrm.IniTreeView1
写入NewOperatorFrm的formclose事件中试试
 
To:mylem
close;{这个Close是不是要关闭newoperatorfrm这个窗体啊,如果是把它放在最后了(
managerfrm.IniTreeView1 的后面)}
(Tmanagerfrm(newoperatorfrm.Owner)).TreeView1.Items.Clear
////此语句出错,提示如下:
 
To:mylem
看明白了,你
NewOperator:=Tnewoperatorfrm.Create(self);
Create的都不是newoperatorfrm,下面怎能引用他呢?
(Tmanagerfrm(newoperatorfrm.Owner)).TreeView1.Items.Clear
////此语句出错,提示如下:
哎!你把出错的这句改成:
(Tmanagerfrm(self.Owner)).TreeView1.Items.Clear;就可以了,很久才看得出来!
 
这回解决了,thank a lot,分分了,to socool_100 满分。
 
顶部