如何引用名称在运行时确定的控件? [英] How can I refer to a control whose name is determined at runtime?

查看:107
本文介绍了如何引用名称在运行时确定的控件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为一种自学习练习,我已经在一个2x3矩形中形成了一个包含六个面板的表单,我希望它们在可见和不可见之间一个接一个地切换。我试图通过使用某种for循环来做到这一点。我当然可以这样写:

  Panel1.Visible:= true; 
Panel1.Visible:= false;
Panel2.Visible:= true;
Panel2.Visible:= false;
Panel3.Visible:= true;
等等

但是这需要相当多的打字,而且效率非常低我决定我希望它在每个步骤之间等待100ms。例如,我必须编辑所有六个步骤等待。这是可行的六个步骤,但也许另一次我想做一百次!所以我想,还必须有一种方法来使用for循环,其中变量从1到6变化,并在对象标识符中使用。所以这样会是这样的:

 为变量:= 1到6开始
面板+ variable.Visible: = true;
Panel + variable.Visible:= false;
结束

现在,这显然不起作用,但我希望有人能告诉我这是否在事实可能,如果是,如何。也许我可以使用字符串作为标识符?我的解释可能非常糟糕,因为我不知道所有的技术术语,但我希望代码解释一下。

解决方案

你可以通过面板的所有者组件数组循环。

  var 
i:整数;
TmpPanel:TPanel;
begin
{此示例循环遍历表单上的所有组件,并将
每个面板的Visible属性切换为与其所具有的值相反的值(IOW,
如果它是True,它被切换到False,如果它是False,它被切换为True)。如果组件[i]是TPanel然后
begin
TmpPanel:= TPanel(Components [i]);
for i:= 0 to ComponentCount - 1 do

TmpPanel.Visible:= not TmpPanel.Visible; //切换true和false
结束;
结束

您还可以使用 FindComponent 如果你想要一个非常具体类型的组件名称。例如,如果您有6个面板,并且其名称为 Panel1 Panel2 等等: p>

  var 
i:整数;
TmpPanel:TPanel;
begin
for i:= 1 to 6 do
begin
TmpPanel:= FindComponent('Panel'+ IntToStr(i))as TPanel;
如果TmpPanel<>否则//我们发现它
TmpPanel.Visible:= not TmpPanel.Visible;
结束
结束


As a kind of self-study exercise, I've made a form wich contains six panels in a 2x3 rectangle and I want them to switch between visible and invisible one after another. I'm trying to do so by using a for loop of some kind. I could of course write something like:

Panel1.Visible := true;
Panel1.Visible := false;
Panel2.Visible := true;
Panel2.Visible := false;
Panel3.Visible := true;
etc. etc.

But this takes quite a lot of typing and is pretty inefficient when I decide I want it to wait for 100ms between each step. For example, I'd then have to edit all the six steps to wait. This is doable for six steps, but maybe another time I want to do it a hundred times! So I'm thinking there must also be a way to use a for loop for this, where a variable varies from 1 to 6 and is used in the object identifier. So it would something like this:

for variable := 1 to 6 do begin
Panel + variable.Visible := true;
Panel + variable.Visible := false;
end;

Now, this obviously doesn't work, but I hope somebody here can tell me if this is in fact possible and if yes, how. Maybe I can use a string as the identifier? My explanation is probably pretty bad because I don't know all the technical terms but I hope the code explains something.

解决方案

You can loop through the panel's Owner's Components array.

var
  i: Integer;
  TmpPanel: TPanel;
begin
  { This example loops through all of the components on the form, and toggles the
    Visible property of each panel to the value that is opposite of what it has (IOW,
    if it's True it's switched to False, if it's False it's switched to True). }
  for i := 0 to ComponentCount - 1 do                  
    if Components[i] is TPanel then                    
    begin
      TmpPanel := TPanel(Components[i]);
      TmpPanel.Visible := not TmpPanel.Visible;     // Toggles between true and false
    end;
end;

You can also use the FindComponent method, if you want a very specific type of component by name. For instance, if you have the 6 panels, and their names are Panel1, Panel2, and so forth:

var
  i: Integer;
  TmpPanel: TPanel;
begin
  for i := 1 to 6 do
  begin
    TmpPanel := FindComponent('Panel' + IntToStr(i)) as TPanel;
    if TmpPanel <> nil then      // We found it
      TmpPanel.Visible := not TmpPanel.Visible;
  end;
end;

这篇关于如何引用名称在运行时确定的控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆