Delphi 我正在寻找一种方法来更改我正在写入信息的面板。Panelsname是通过一种模式建立起来的

Delphi 我正在寻找一种方法来更改我正在写入信息的面板。Panelsname是通过一种模式建立起来的,delphi,variables,Delphi,Variables,我可能有一个非常简单的方法来解决这个问题,尽管我不知道怎么做。我对德尔福很陌生,这就是为什么我几乎没有经验。 下面是我想简化的一段代码: procedure TForm1.Asign(); begin case TileValue[1,1] of 0: Fx1y1.Color:=clBtnFace; 1: Fx1y1.Color:=clBlue; 2: Fx1y1.Color:=clMaroon; end;

我可能有一个非常简单的方法来解决这个问题,尽管我不知道怎么做。我对德尔福很陌生,这就是为什么我几乎没有经验。 下面是我想简化的一段代码:

procedure TForm1.Asign();
    begin
      case TileValue[1,1] of
        0: Fx1y1.Color:=clBtnFace;
        1: Fx1y1.Color:=clBlue;
        2: Fx1y1.Color:=clMaroon;
      end;
      case TileValue[1,2] of
        0: Fx1y2.Color:=clBtnFace;
        1: Fx1y2.Color:=clBlue;
        2: Fx1y2.Color:=clMaroon;
      end;
      case TileValue[1,3] of
        0: Fx1y3.Color:=clBtnFace;
        1: Fx1y3.Color:=clBlue;
        2: Fx1y3.Color:=clMaroon;
      end;
    end;
Fx1y1是一个面板,而x1是坐标,y1也是坐标(在“一排4游戏”中的坐标)。我试图用另一个变量替换面板名称中的x和y坐标,以便缩短代码。它应该是这样的:

procedure TForm1.Asign();
  var A,B:integer;
    begin
      for B:=1 to 6 do begin
      for A:=1 to 7(Because the 4 in a row playing field is 6 by 7) do begin
        case TileValue[A,B] of
          0: Fx{A}y{b}.Color:=clBtnFace;
          1: Fx{A}y{b}.Color:=clBlue;
          2: Fx{A}y{b}.Color:=clMaroon;
        end;
      end;
      end;
    end;

这可能吗?如果是或不是,请告诉我

您可以使用表单的
FindComponent
函数来执行所要求的操作,该函数返回提供名称的组件。因为它可以是任何组件,所以必须将结果强制转换为
TPanel
。如果没有提供名称的组件,或者(可能)不是面板,则会引发异常。为了进一步简化代码,我还将使用颜色数组

procedure TForm1.Assign;
  const Colors: array[0..2] of TColor = (clBtnFace, clBlue, clMaroon);
  var   x,y: integer;
        Panel: TPanel;
begin
  for x := 1 to 7 do
    for y := 1 to 6 do
    begin
      Panel := TPanel(FindComponent('Fx' + x.ToString + 'y' + y.ToString));
      Panel.Color := Colors[TileValue[x,y]];
    end;
end;
正如David提到的,将面板放在一个阵列中并使用它会更干净。从您显示的代码来看,似乎您已经在设计时创建了所有面板,这不是必需的。看起来您已经有了42个面板,这需要手动创建很多,如果您想使字段变大,那么它将变得更加不可行。这就是为什么最好从代码创建面板:

procedure TForm1.FormCreate(Sender: TObject);
begin
  CreatePanels;
  Assign;
end;

procedure TForm1.CreatePanels;
  var x,y: integer;
begin
  for x := 1 to 7 do
    for y := 1 to 6 do
    begin
      Panels[x,y] := TPanel.Create(Self);
      Panels[x,y].Parent := Self;
      // set the position of the panel
      Panels[x,y].Left := 10 + (x-1)*50;
      Panels[x,y].Top := 10 + (y-1)*50;
      Panels[x,y].Width := 50;
      Panels[x,y].Height := 50;
      // make sure we can assign a non-default color
      Panels[x,y].ParentBackground := false;
      // do whatever else you want to do with the panel
    end;
end;

procedure TForm1.Assign;
  const Colors: array[0..2] of TColor = (clBtnFace, clBlue, clMaroon);
  var   x,y: integer;
begin
  for x := 1 to 7 do
    for y := 1 to 6 do
      Panels[x,y].Color := Colors[TileValue[x,y]];
end;

您将在声明
TileValue
的位置声明面板数组。不仅可以更轻松地分配颜色,还可以更快地更改运动场的外观和尺寸。

您可以使用表单的
FindComponent
函数来完成所需的操作,该函数返回提供名称的组件。因为它可以是任何组件,所以必须将结果强制转换为
TPanel
。如果没有提供名称的组件,或者(可能)不是面板,则会引发异常。为了进一步简化代码,我还将使用颜色数组

procedure TForm1.Assign;
  const Colors: array[0..2] of TColor = (clBtnFace, clBlue, clMaroon);
  var   x,y: integer;
        Panel: TPanel;
begin
  for x := 1 to 7 do
    for y := 1 to 6 do
    begin
      Panel := TPanel(FindComponent('Fx' + x.ToString + 'y' + y.ToString));
      Panel.Color := Colors[TileValue[x,y]];
    end;
end;
正如David提到的,将面板放在一个阵列中并使用它会更干净。从您显示的代码来看,似乎您已经在设计时创建了所有面板,这不是必需的。看起来您已经有了42个面板,这需要手动创建很多,如果您想使字段变大,那么它将变得更加不可行。这就是为什么最好从代码创建面板:

procedure TForm1.FormCreate(Sender: TObject);
begin
  CreatePanels;
  Assign;
end;

procedure TForm1.CreatePanels;
  var x,y: integer;
begin
  for x := 1 to 7 do
    for y := 1 to 6 do
    begin
      Panels[x,y] := TPanel.Create(Self);
      Panels[x,y].Parent := Self;
      // set the position of the panel
      Panels[x,y].Left := 10 + (x-1)*50;
      Panels[x,y].Top := 10 + (y-1)*50;
      Panels[x,y].Width := 50;
      Panels[x,y].Height := 50;
      // make sure we can assign a non-default color
      Panels[x,y].ParentBackground := false;
      // do whatever else you want to do with the panel
    end;
end;

procedure TForm1.Assign;
  const Colors: array[0..2] of TColor = (clBtnFace, clBlue, clMaroon);
  var   x,y: integer;
begin
  for x := 1 to 7 do
    for y := 1 to 6 do
      Panels[x,y].Color := Colors[TileValue[x,y]];
end;

您将在声明
TileValue
的位置声明面板数组。不仅可以更轻松地分配颜色,还可以更快地更改比赛场地的外观和尺寸。

将这些面板放入阵列并索引到阵列中。将这些面板放入阵列并索引到阵列中。非常感谢。帮了我很多忙!很高兴听到!如果答案完全解决了你的问题,你可以接受它!我不知道obn能做到。给你。哇,非常感谢。帮了我很多忙!很高兴听到!如果答案完全解决了你的问题,你可以接受它!我不知道obn能做到。干得好。