Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Delphi 是否可以在单独的单元中使用表单属性?_Delphi_Delphi Units - Fatal编程技术网

Delphi 是否可以在单独的单元中使用表单属性?

Delphi 是否可以在单独的单元中使用表单属性?,delphi,delphi-units,Delphi,Delphi Units,我正在使用delphi创建一个游戏,希望将我的一些代码移动到一个单独的单元中,但是此代码使用表单中的属性。这可能吗 我正在使用VCL表单应用程序创建一个游戏,目前我所有的游戏算法代码都在表单单元中。这并没有什么问题,因为我的程序运行得很好,只是看起来很凌乱,有人建议我把算法代码放在一个单独的单元中。我已经尝试将代码移动到一个新单元中,但是无论我尝试什么,都会出现语法错误 这是我的主单元中的代码,其中Grid是来自表单的TStringGrid,GridSize是来自第二个单元的过程: proced

我正在使用delphi创建一个游戏,希望将我的一些代码移动到一个单独的单元中,但是此代码使用表单中的属性。这可能吗

我正在使用VCL表单应用程序创建一个游戏,目前我所有的游戏算法代码都在表单单元中。这并没有什么问题,因为我的程序运行得很好,只是看起来很凌乱,有人建议我把算法代码放在一个单独的单元中。我已经尝试将代码移动到一个新单元中,但是无论我尝试什么,都会出现语法错误

这是我的主单元中的代码,其中Grid是来自表单的TStringGrid,GridSize是来自第二个单元的过程:

procedure TGame.NewGame;
begin
  Grid.Width:=GridSize(Grid.ColCount);
  Grid.Height:=GridSize(Grid.RowCount);
end;
procedure ClearGrid;
var
  i,j: integer;
begin
  for i := 0 to Grid.ColCount-1 do
  begin
    for j := 0 to Grid.RowCount-1 do
    begin
      Grid.Cells[i,j]:='';
    end;
  end;
end;
这是第二个单元代码:

unit UGameGenerator;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, 
  System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.Grids, Vcl.Menus, 
  Vcl.StdCtrls;

implementation

function GridSize(size: integer): integer;
begin
  result:=291+36*(size-8);
end;

end.
编辑:

这是来自第二个单元的代码:

procedure TGame.NewGame;
begin
  Grid.Width:=GridSize(Grid.ColCount);
  Grid.Height:=GridSize(Grid.RowCount);
end;
procedure ClearGrid;
var
  i,j: integer;
begin
  for i := 0 to Grid.ColCount-1 do
  begin
    for j := 0 to Grid.RowCount-1 do
    begin
      Grid.Cells[i,j]:='';
    end;
  end;
end;

编译器需要以某种方式找到
GridSize
的声明。为此,请遵循以下指南:

  • 在主窗体中,将
    UGameGenerator
    添加到使用列表中:

    unit MainForm;
    
    interface
    
    uses
      ...,UGameGenerator; // Add here or in the implementation section
    
    ...
    implementation
    
    ...
    
    end.
    
  • 在您的
    UGameGenerator
    单元中,公开接口中其他程序部件中使用的所有类型/功能/过程:

    unit UGameGenerator;  
    
    interface
    
    uses
      ...,...;
    
    function GridSize(size: integer): integer;  
    
    implementation
    
    function GridSize(size: integer): integer;
    begin
      result:=291+36*(size-8);
    end;        
    
    end.
    

  • 提示:在设计单独的单元时,避免直接使用来自其他单元的变量。而是将它们作为过程/函数调用中的参数传递

    否则,您可能会在循环引用方面遇到很多麻烦


    在更新的问题中,声明
    过程ClearGrid(aGrid:TStringGrid)并将网格作为参数传递。

    编译器需要以某种方式找到
    GridSize
    的声明。为此,请遵循以下指南:

  • 在主窗体中,将
    UGameGenerator
    添加到使用列表中:

    unit MainForm;
    
    interface
    
    uses
      ...,UGameGenerator; // Add here or in the implementation section
    
    ...
    implementation
    
    ...
    
    end.
    
  • 在您的
    UGameGenerator
    单元中,公开接口中其他程序部件中使用的所有类型/功能/过程:

    unit UGameGenerator;  
    
    interface
    
    uses
      ...,...;
    
    function GridSize(size: integer): integer;  
    
    implementation
    
    function GridSize(size: integer): integer;
    begin
      result:=291+36*(size-8);
    end;        
    
    end.
    

  • 提示:在设计单独的单元时,避免直接使用来自其他单元的变量。而是将它们作为过程/函数调用中的参数传递

    否则,您可能会在循环引用方面遇到很多麻烦


    在更新的问题中,声明
    过程ClearGrid(aGrid:TStringGrid)并将网格作为参数传递。

    一点代码和一个错误示例可能会有所帮助。啊,是的。对不起,忘记添加了。现在问题应该包括一些代码。一些代码和一个错误示例可能会有所帮助。啊,是的。对不起,忘记添加了。现在的问题应该包括一些代码。非常感谢。我忘了在界面上添加函数。我在这个问题上添加了更多的代码。有没有可能帮上忙?将GUI与程序逻辑分离是程序员必须学习的最重要的事情之一。我更新了答案,并提示如何开始。准备好投入一些精力来掌握这一部分。最终会有回报的,好的。非常感谢,非常感谢。我忘了在界面上添加函数。我在这个问题上添加了更多的代码。有没有可能帮上忙?将GUI与程序逻辑分离是程序员必须学习的最重要的事情之一。我更新了答案,并提示如何开始。准备好投入一些精力来掌握这一部分。最终会有回报的,好的。多谢各位