Dynamically create Tpagecontrol instances

259 views Asked by At

I have searched and searched but found no examples. I would like to dynamically create PageControl instances each with their own setoff TTabsheets. I get no complaints from the Delphi IDE, however I do get:

Access violation in module FormApplication.exe write of address 00000000

Is there something I am missing?

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

There are 1 answers

2
David Heffernan On

You did not allocate the array. You need to add the following before you access the array:

SetLength(ABC_Status, NUM+1);

The +1 is because of the rather non-standard loop bounds that you used.

Also 100/100 = 1 and so the expression 100/NUM/100 seems odd. You may as well write 1/NUM.

Your use of the non-standard Extended type also seems strange. I don't see much need for that.

There are probably lots more errors, but I'm stopping here.