我做一个程序,动态创建 Panel 上面有 一个ListBox和一个Label当我要动态创建20个Panel,并要给其中的第5个Panel的 ListBox和Label赋值,应该怎么做??另外:如何响应第X个ListBox的Click事件?我希望的结果是,点第X个ListBox,相应的,第X个Label的值等于 该ListBox的选项。谢谢各位大虾了!小弟分数不多,只能给我那么多了....

解决方案 »

  1.   

    動態建立的時候,賦予每個動態建立的控件 tag ,i := (Sender as T...).tag;將 Tag 相同的控件一一對應就可以了;
      

  2.   

    动态创建时给每个控件赋tag,然后:
      
    if MyControl.tag=20 then
    begin
      if MyControl is TListBox then
        MyControl.OnClick:=TForm1.Form1Click;//指定响应句柄
    end;
      

  3.   

    //参考如下代码:
    type
      TForm1 = class(TForm)
    //...
      private
        procedure ListBoxClick(Sender: TObject);
      end;//...procedure TForm1.FormCreate(Sender: TObject);
    var
      vPanel: TPanel;
      vLabel: TLabel;
      vListBox: TListBox;
      I: Integer;
    begin
      for I := 0 to 20 - 1 do
      begin
        vPanel := TPanel.Create(Self);
        vPanel.Parent := Self;
        vPanel.Width := 40;
        vPanel.Height := 150;
        vPanel.Left := I * vPanel.Width;    vLabel := TLabel.Create(Self);
        vLabel.Parent := vPanel;
        vLabel.Align := alTop;
        vLabel.Caption := '1';    vListBox := TListBox.Create(Self);
        vListBox.Parent := vPanel;
        vListBox.Align := alClient;
        vListBox.OnClick := ListBoxClick;
        vListBox.Tag := Integer(vLabel);    vListBox.Items.Text := '1'#13#10'2'#13#10'3'#13#10'4'#13#10'5';
        vListBox.ItemIndex := 0;
      end;
    end;procedure TForm1.ListBoxClick(Sender: TObject);
    begin
      TLabel(TListBox(Sender).Tag).Caption :=
        TListBox(Sender).Items[TListBox(Sender).ItemIndex];
    end;