访问 冲突(50分)

B

beckzd

Unregistered / Unconfirmed
GUEST, unregistred user!
按delphi 的帮助写了这段代码。如下:
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComCtrls;

type
TForm1 = class(TForm)
Label1: TLabel;
Button1: TButton;
Button2: TButton;
TreeView1: TTreeView;
Edit1: TEdit;
Edit2: TEdit;
Edit3: TEdit;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
type
PMyRec = ^TMyRec;
TMyRec = record
FName: string;
LName: string;
end;



implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
MyRecPtr: PMyRec;
TreeViewIndex: LongInt;
begin
New(MyRecPtr);
MyRecPtr^.FName := Edit1.Text;
MyRecPtr^.LName := Edit2.Text;
TreeViewIndex := StrToInt(Edit3.Text);
with TreeView1 do
begin
if Items.Count = 0 then
Items.AddObject(nil, 'Item' + IntToStr(TreeViewIndex), MyRecPtr)
else if (TreeViewIndex < Items.Count) and (TreeViewIndex >= 0) then
Items.AddObject(Items[TreeViewIndex], 'Item' + IntToStr(TreeViewIndex), MyRecPtr);

end;
end;
procedure TForm1.Button2Click(Sender: TObject);

begin
label1.Caption:=PMyRec(TreeView1.Selected.Data)^.FName + ' ' +
PMyRec(TreeView1.Selected.Data)^.LName;
end;
end.
但是到按下button2时,就会出错。
出错信息是"access violation at address 0045c9d9 in module "project1.exe"
read of address 000000C"
我估计是指针错了,但不知道如何改,为什么错。望赐教。谢谢
 
TreeView1没有selected的item阿,所以就没有了
并且不要那个^
直接
label1.Caption:=PMyRec(TreeView1.Selected.Data).FName + ' ' +
PMyRec(TreeView1.Selected.Data).LName;
 
你先TreeView里选择一项,再按Button2
 
或者在你的Button2 click事件改成如下代码。
begin
if treeview1.selected=nil then
begin
showmessage('请先选择一项');
exit;
end;
label1.Caption:=PMyRec(TreeView1.Selected.Data)^.FName + ' ' +
PMyRec(TreeView1.Selected.Data)^.LName;
end;
 
另外,你的程序还有问题
你的else if 那段嘛用?
 
还有当我点了button2后,如果改变了edit1和edit2中的值,在点button1,选种后,再点
button2,label1中的字符没有变化,还是原来的。没有改成现在edit1和edit2中的值。
望赐教,谢谢。
 
那当然了,Button1是在Treeview中增加一项,Button2是显示Treeview当前当前选择
的数据。
 
但是我改了edit1和edit2中的值后。
根据:MyRecPtr^.FName := Edit1.Text;
MyRecPtr^.LName := Edit2.Text;
按下button1后他因该是变的啊。
为什么在label1中无变化了。
 
先选择新增的那一项,再按Button2。
 
什么叫新增的那一项,运行的时候只有一个接点,那有新增的。这跟新增一项有关么。
我都改变指针所指的内容了
MyRecPtr^.FName := Edit1.Text;
MyRecPtr^.LName := Edit2.Text;
怎么label1就是不变了?
 
是你的那个ELSE IF条件有问题阿,
你看看,你写第一条的时候,EDIT3写的是1了,那么添加一条成功了
写第二条的时候,你EDIT3写的是2,你看看条件根本不成立
else if (TreeViewIndex < Items.Count) and (TreeViewIndex >= 0) then
TreeViewIndex=2
Items.Count=1
不符合条件,这样就不会添加到TREEVIEW中阿,不知道你怎么实现这个的
 
那我应该如何动态的加入第二个节点了?
我试过了,如果把
else if (TreeViewIndex < Items.Count) and (TreeViewIndex >= 0) then
改动了一下,使他满足增加节点底条件,但是执行到
Items.AddObject(Items[TreeViewIndex], 'Item' + IntToStr(TreeViewIndex), MyRecPtr);
时候他总是报错,"invalid index"
 
那当然了,越界了。
 
越界了,那边越界?可否具体些,我改如何让他动态加入节点了
 
你的条件改成什么了?
你的EDIT3具体的数据是什么意思?序号还是什么?
 
当然是数据了,当加入第二个节点的时候,

Items.AddObject(Items[TreeViewIndex], 'Item' + IntToStr(TreeViewIndex), MyRecPtr);
我想其中的参数:Items[TreeViewIndex]就是他的父节点吧
 
我把你的代码COPY过来运行一下,全部可以阿,你填第二节点的时候,
EDIT3写的是什么阿?
我写0可以阿,什么都好好的阿,呵呵
就是只要EDIT3中的数字比TREEVIEW的COUNT小就可以了。
 
to tophi
我如果想在edit3中输入2,3,4等向后加一的数而不是零,
我把
else if (TreeViewIndex < Items.Count) and (TreeViewIndex >= 0) then
改为
else if (TreeViewIndex > Items.Count) and (TreeViewIndex >= 0) then
还是会有上面的,"invalid index"
错误.
 
当然不能改成这个样子阿,
那样的话,当TREEVIEW中只有1个item时,那个只有是item[0]
ITEM.COUNT=1
你却在edit3中填1,2,3等
看看
Items.AddObject(Items[1], 'Item' + IntToStr(TreeViewIndex), MyRecPtr);
当然会invalid index(只有0,没有1阿)
所以,你填第二个的时候,应该填0,第三个的时候应该填1
否则你可以吧你的TreeViewIndex := StrToInt(Edit3.Text);
该为TreeViewIndex := StrToInt(Edit3.Text)-2;
就可以了填第一个,写1,填第二个,写2了,呵呵
或者就不用那个TreeViewIndex了,直接改为
Items.AddObject(Items[Items.count-1], 'Item' + IntToStr(Items.count+1), MyRecPtr);


 
多谢 ,tophi老兄,你说的已经很详细了.可否留下你的邮箱地址,谢谢
 
顶部