Pascal 和 Delphi 语法错误?

发布于 2024-08-27 07:11:42 字数 1543 浏览 5 评论 0原文

这是 inno setup 的代码部分。我的目的是制作两个复选框,一次选择一个。 但这段代码返回错误。

[代码]部分:

procedure CheckBoxOnClick(Sender: TObject);
var
Box2,CheckBox: TNewCheckBox;
begin
if CheckBox.Checked then
   CheckBox.State := cbUnchecked;
   Box2.State := cbChecked;
else                                //THIS LINE RETURNS AN ERROR: "Identifier Expected."
   CheckBox.State := cbChecked;
   Box2.State := cbUnchecked;
end;

procedure Box2OnClick(Sender: TObject);
var
Box2,CheckBox: TNewCheckBox;
begin
if Box2.Checked then
   CheckBox.State := cbChecked;
   Box2.State := cbUnchecked;
else                               //same error
   CheckBox.State := cbUnchecked;
   Box2.State := cbChecked;
end;

procedure CreateTheWizardPages;
var
  Page: TWizardPage;
  Box2,CheckBox: TNewCheckBox;
begin
  { TButton and others }

  Page := CreateCustomPage(wpWelcome, '', '');

  CheckBox := TNewCheckBox.Create(Page);
  CheckBox.Top :=ScaleY(8)+ScaleX(50);
  CheckBox.Width := Page.SurfaceWidth;
  CheckBox.Height := ScaleY(17);
  CheckBox.Caption := 'Do this';
  CheckBox.Checked := True;
  CheckBox.OnClick := @CheckBoxOnClick;
  CheckBox.Parent := Page.Surface;

  Box2 := TNewCheckBox.Create(Page);
  Box2.Top :=ScaleY(8)+ScaleX(70);
  Box2.Width := Page.SurfaceWidth;
  Box2.Height := ScaleY(17);
  Box2.Caption := 'No,Thanks.';
  Box2.Checked := False;
  Box2.OnClick := @Box2OnClick;
  Box2.Parent := Page.Surface;
end;


procedure InitializeWizard();
//var
begin
  { Custom wizard pages }
  CreateTheWizardPages;
end;

请告诉我在哪里更改..

This is the code section from inno setup.My intention is to make two Checkbox where at a time one is being selected.
But this code return error.

[code] section:

procedure CheckBoxOnClick(Sender: TObject);
var
Box2,CheckBox: TNewCheckBox;
begin
if CheckBox.Checked then
   CheckBox.State := cbUnchecked;
   Box2.State := cbChecked;
else                                //THIS LINE RETURNS AN ERROR: "Identifier Expected."
   CheckBox.State := cbChecked;
   Box2.State := cbUnchecked;
end;

procedure Box2OnClick(Sender: TObject);
var
Box2,CheckBox: TNewCheckBox;
begin
if Box2.Checked then
   CheckBox.State := cbChecked;
   Box2.State := cbUnchecked;
else                               //same error
   CheckBox.State := cbUnchecked;
   Box2.State := cbChecked;
end;

procedure CreateTheWizardPages;
var
  Page: TWizardPage;
  Box2,CheckBox: TNewCheckBox;
begin
  { TButton and others }

  Page := CreateCustomPage(wpWelcome, '', '');

  CheckBox := TNewCheckBox.Create(Page);
  CheckBox.Top :=ScaleY(8)+ScaleX(50);
  CheckBox.Width := Page.SurfaceWidth;
  CheckBox.Height := ScaleY(17);
  CheckBox.Caption := 'Do this';
  CheckBox.Checked := True;
  CheckBox.OnClick := @CheckBoxOnClick;
  CheckBox.Parent := Page.Surface;

  Box2 := TNewCheckBox.Create(Page);
  Box2.Top :=ScaleY(8)+ScaleX(70);
  Box2.Width := Page.SurfaceWidth;
  Box2.Height := ScaleY(17);
  Box2.Caption := 'No,Thanks.';
  Box2.Checked := False;
  Box2.OnClick := @Box2OnClick;
  Box2.Parent := Page.Surface;
end;


procedure InitializeWizard();
//var
begin
  { Custom wizard pages }
  CreateTheWizardPages;
end;

Please tell me where to change..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

莫多说 2024-09-03 07:11:42

在 Pascal 中,thenelse 之后必须跟随单个语句或块。

这就是解析器解释代码的方式:

  1. 如果条件为 true,则将执行 CheckBox.State := cbUnchecked; 行。通过该语句,if 子句就完成了。
  2. 那么 Box2.State := cbChecked; 将始终被执行。
  3. else 不属于任何当前打开的 if 语句 -> 语法错误

您必须将代码括在一个块中,如下所示:

procedure CheckBoxOnClick(Sender: TObject);
var
  Box2,CheckBox: TNewCheckBox;
begin
  if CheckBox.Checked then
  BEGIN
     CheckBox.State := cbUnchecked;
     Box2.State := cbChecked;
  END else                               
  BEGIN
     CheckBox.State := cbChecked;
     Box2.State := cbUnchecked;
  END;
end;

In Pascal after then and else a single statement or a block must follow.

This is how the parser interprets your code:

  1. In case the condition is true, the line CheckBox.State := cbUnchecked; will be executed. With that statement the if clause is finished.
  2. Then Box2.State := cbChecked; will always be executed.
  3. The else does not belong to any currently open if statement -> Syntax error

You have to enclose the code in a block, like this:

procedure CheckBoxOnClick(Sender: TObject);
var
  Box2,CheckBox: TNewCheckBox;
begin
  if CheckBox.Checked then
  BEGIN
     CheckBox.State := cbUnchecked;
     Box2.State := cbChecked;
  END else                               
  BEGIN
     CheckBox.State := cbChecked;
     Box2.State := cbUnchecked;
  END;
end;
揽清风入怀 2024-09-03 07:11:42

很简单。在 then 之后添加一个 begin ... end 子句。


if CheckBox.Checked then
BEGIN
   CheckBox.State := cbUnchecked;
   Box2.State := cbChecked;
END
else                                

very simple. Add a begin ... end clause after your then.


if CheckBox.Checked then
BEGIN
   CheckBox.State := cbUnchecked;
   Box2.State := cbChecked;
END
else                                
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文