我在formcreate中创建控件,
formcreate(sender:TObject);
var
  test_label:TLabel;
begin
  test_label:=TLabel.Create();
  test_label.Caption:='test_label';
  test_label.parent:=Self;
             ~~~~~~~~~~~~~~~~label是属于form的,这里出错,怎么解决呢?
  
end;

解决方案 »

  1.   

    Label:=Tlabel.Create(Self);
    Label.Parent:=Form1; 
    label.Caption:=sdf;
      

  2.   

    test_label:=TLabel.Create();
      test_label.Caption:='test_label';
      test_label.parent:=Self;
                 ~~~~~~~~~~~~~~~~label是属于form的,这里出错,怎么解决呢?
      
    当然出错,这时候FORM还没有创建呢;你需要在ONSHOW事件里;
      

  3.   

    formcreate(sender:TObject);
    var
      test_label:TLabel;
    begin
      test_label:=TLabel.Create(Self); // 
      test_label.Caption:='test_label';
      test_label.parent:=Self;
    end;
      

  4.   

    在OnCreate中可以的啊,下面代码调试通过:
    procedure TForm1.FormCreate(Sender: TObject);
    var
      test_label:TLabel;
    begin
      test_label:=TLabel.Create(Self);
      test_label.Caption:='test_label';
      test_label.parent:=Self;
      test_label.Visible := True;
    end;
      

  5.   

    Tlabel.create( );没指定所有者
      

  6.   

    哥们,可以这样。要么你在代码换个事件如onshow,要么这样写
      test_label:=TLabel.Create(Self);
      test_label.Caption:='test_label';
      test_label.parent:=Self;
      test_label.Visible := True;
      

  7.   

    问题解决了,是我自己少加了stdctrls。
    谢谢大家!!!!
    给分了。