动态创建Tpagecontrol实例

时间:2017-12-20 18:34:35

标签: delphi delphi-xe7 tpagecontrol

我搜索过并搜索过,但没有找到任何例子。我想动态创建PageControl个实例,每个实例都有自己的setoff TTabsheets。我没有得到Delphi IDE的投诉,但我得到了:

  

模块FormApplication.exe中的访问冲突写入地址00000000

我有什么遗失的吗?

procedure TForm1.FormCreate(Sender: TObject);
type
  ABC_Status_Object = record
    ABC_PageControl_instance: TPageControl;
    quickStat_instance: TTabsheet;
    detailStat_instance: TTabsheet;
    abc_light: TShape;
  end;

var
  ABC_Status: array of ABC_Status_Object;
  I: Integer;
  Frac, Icnt: Extended;

begin
  inifile := TIniFile.Create(ChangeFileExt(Application.ExeName,'.ini'));
  loadGlobalConfig;

  Frac := 100/NUM/100;
  for I := 0 to NUM do
  begin
    {// Create the Tabs: }
    ABC_Status[I].ABC_PageControl_instance := TPageControl.Create(self);
    ABC_Status[I].ABC_PageControl_instance.Parent := self;
    ABC_Status[I].quickStat_instance := TTabsheet.Create(ABC_Status[I].ABC_PageControl_instance);
    ABC_Status[I].detailStat_instance := TTabsheet.Create(ABC_Status[I].ABC_PageControl_instance);
    ABC_Status[I].quickStat_instance.PageControl := ABC_Status[I].ABC_PageControl_instance;
    ABC_Status[I].detailStat_instance.PageControl := ABC_Status[I].ABC_PageControl_instance;

    {// Set the attributes of each instance of PageControl, including the tabs: }
    ABC_Status[I].ABC_PageControl_instance.Visible := TRUE;
    ABC_Status[I].ABC_PageControl_instance.Top := 0;
    if(NUM = 1) then
      ABC_Status[I].ABC_PageControl_instance.Width := ClientWidth;

    if(NUM > 1) AND (NUM < 4) then
    begin
      Icnt := 100/(I+1)/100;

      ABC_Status[I].ABC_PageControl_instance.Width := Trunc(ClientWidth*Frac);
      ABC_Status[I].ABC_PageControl_instance.Left := 30;
    end;
    ABC_Status[I].quickStat_instance.Caption := 'Quick Status';
    ABC_Status[I].quickStat_instance.Visible := TRUE;
    ABC_Status[I].detailStat_instance.Caption := 'Details';
    ABC_Status[I].detailStat_instance.Visible := TRUE;
  end;

end;

1 个答案:

答案 0 :(得分:5)

您没有分配数组。在访问阵列之前,需要添加以下内容:

SetLength(ABC_Status, NUM+1);

+1是因为您使用的非标准循环边界。

同样100/100 = 1所以表达式100/NUM/100似乎很奇怪。你也可以写1/NUM

您使用非标准Extended类型似乎也很奇怪。我不太了解这一点。

可能还有很多错误,但我会在这里停下来。