请问HeyTommy,这段源码为什么去了Button会出错? (50分)

  • 主题发起人 主题发起人 bluehouse
  • 开始时间 开始时间
B

bluehouse

Unregistered / Unconfirmed
GUEST, unregistred user!
unit Unit1;

interface

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

type
TForm1 = class(TForm)

Label1: TLabel;

private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

var
TempReg:TRegistry;
TempValue:string;
begin
TempReg:=TRegistry.Create;
try
TempReg.RootKey:=HKEY_LOCAL_MACHINE;
if TempReg.OpenKey('/Software/Microsoft/Windows/CurrentVersion/',false) then
begin
if tempreg.ValueExists('ProductId') then
begin
TempValue:=TempReg.ReadString('ProductId');
label1(出错).caption:=tempvalue;
end
else
ShowMessage('ProductID is not exist!');
end;
finally
TempReg.CloseKey;
TempReg.Free;
end;
end;

end.
 
unit Unit1;

interface

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

type
TForm1 = class(TForm)
CheckBox1: TCheckBox;
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}
var
TempReg:TRegistry;
TempValue:string;
begin
TempReg:=TRegistry.Create;
try
TempReg.RootKey:=HKEY_LOCAL_MACHINE;
if TempReg.OpenKey('/Software/Microsoft/Windows/CurrentVersion/',false) then
begin
if tempreg.ValueExists('ProductId') then
begin
TempValue:=TempReg.ReadString('ProductId');
checkbox1.checked:=true;
end
else
ShowMessage('ProductID is not exist!');
end;
finally
TempReg.CloseKey;
TempReg.Free;
end;
end;

end.
这个呢?一样地方出错!
 
我想叫他在不加时钟的情况下,一打开程序便显示?
 
当然出错了,你在单元初始化的时候引用TForm1的的实例,可是这时候TForm1的实例
还没有创建呢。
 
再详细点,我想叫他在不加时钟的情况下,一打开程序便显示?帮改码,给你加分!
 
如果你想在Form1上显示,那么只要把这些代码放到Form1的OnCreate事件里就可以了
 
改一下源码吧!谢!
 
unit Unit1;

interface

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

type
TForm1 = class(TForm)
Label1: TLabel;
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation
uses
Registry;
{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
var
TempReg:TRegistry;
TempValue:string;
begin
TempReg:=TRegistry.Create;
try
TempReg.RootKey:=HKEY_LOCAL_MACHINE;
if TempReg.OpenKey('/Software/Microsoft/Windows/CurrentVersion/',false) then
begin
if tempreg.ValueExists('ProductId') then
begin
TempValue:=TempReg.ReadString('ProductId');
label1.caption:=tempvalue;
end
else
ShowMessage('ProductID is not exist!');
end;
finally
TempReg.CloseKey;
TempReg.Free;
end;
end;

end.
 
-------------------------------------------------------------------------------
下面代码程序,label显示序列号
unit Unit1;

interface

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

type
TForm1 = class(TForm)
Button1: TButton;
Label1: TLabel;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
TempReg:TRegistry;
TempValue:string;
begin
TempReg:=TRegistry.Create;
try
TempReg.RootKey:=HKEY_LOCAL_MACHINE;
if TempReg.OpenKey('/Software/Microsoft/Windows/CurrentVersion/',false) then
begin
if tempreg.ValueExists('ProductId') then
begin
TempValue:=TempReg.ReadString('ProductId');
Label1.Caption:=TempValue;
end
else
ShowMessage('ProductID is not exist!');
end;
finally
TempReg.CloseKey;
TempReg.Free;
end;
end;
 
下面代码程序,label却不显示,为什么?

unit Unit1;

interface

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

type
TForm1 = class(TForm)
Label1: TLabel;
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
var
TempReg:TRegistry;
TempValue:string;
begin
TempReg:=TRegistry.Create;
try
TempReg.RootKey:=HKEY_LOCAL_MACHINE;
if TempReg.OpenKey('/Software/Microsoft/Windows/CurrentVersion/',false) then
begin
if tempreg.ValueExists('ProductId') then
begin
TempValue:=TempReg.ReadString('ProductId');
Label1.Caption:=TempValue;
end
else
ShowMessage('ProductID is not exist!');
end;
finally
TempReg.CloseKey;
TempReg.Free;
end;
end;
end.

end.

 
你没有给OnCreate事件赋值!!
 
你,你,你,,你怎么问起这个问题了,,,你看你的原码:

implementation

{$R *.dfm}

var
TempReg:TRegistry;
TempValue:string;
begin
TempReg:=TRegistry.Create;
try
TempReg.RootKey:=HKEY_LOCAL_MACHINE;
。。。
。。。

你这段代码郎个让编译器处理嘛,,,前面少了函数头塞,,,

在implementation 后面的是实现部分,,,你不可以把函数头丢了,,

至于这段代码:
我原封不动的用,没有错:我的完整单元代码如下:
unit Unit1;

interface

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

type
TForm1 = class(TForm)
Label1: TLabel;
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
var
TempReg:TRegistry;
TempValue:string;
begin
TempReg:=TRegistry.Create;
try
TempReg.RootKey:=HKEY_LOCAL_MACHINE;
if TempReg.OpenKey('/Software/Microsoft/Windows/CurrentVersion/',false) then
begin
if tempreg.ValueExists('ProductId') then
begin
TempValue:=TempReg.ReadString('ProductId');
Label1.Caption:=TempValue;
end
else
ShowMessage('ProductID is not exist!');
end;
finally
TempReg.CloseKey;
TempReg.Free;
end;
end;


end.

兄台:你问的问题实在是太入门级的问题了,,,你是不是才开始用DELPHI??


 
HeyTommy兄台,你编译一下,该程序为何Label1不显示应该显示的内容?虽然语法没错,但却没有功能?
 
好嘛,,给个信箱,我把编译过的整个工程给你,你自己去看看吧!!!

我的平台是win2000 professional,delphi 6
 
QQ:108438328
 
我仔细看了,呵呵,,,,

你看你的整个单元的结尾:
怎么有行这样的语句呢?

end.

end.

只需要一个[red]end.[/red]的嘛,,它表示的是一个单元中的程序的结束的嘛, ,,只能要一个
,多了要出错!!!!
 
多复制了一个,我复制了一百万个,编译通过,没影响啊!呵呵.......
 
你怎么能这么想呢?
你是可以加一摩多的结尾,,,编译器会给忽略,,,但也太那个了吧??
 
HeyTommy兄台,再次请你编译一下,该程序为何Label1不显示应该显示的内容?虽然语法没错,但却没有功能?
 
根本不是这个原因, 你到Form的事件里双击一下OnCreate就可以了
 
后退
顶部