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"
我估计是指针错了,但不知道如何改,为什么错。望赐教。谢谢
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"
我估计是指针错了,但不知道如何改,为什么错。望赐教。谢谢