Recent

Author Topic: [SOLVED]I want to create 10 TComboBoxes programatically and getting error 210  (Read 5376 times)

Knipfty

  • Full Member
  • ***
  • Posts: 232
Hi I am trying to create 10 TComboBoxes in code instead of at design time and am getting runtime error 210 which indicates that the object(s) are not being intialized.  So clearly I'm doing something wrong.  BTW, this is the 1st time I am trying something like this.  Here is the sample code:

Code: [Select]
Var
   cba: array[1..10] of TComboBox;
   I: integer;
begin
  for I:=1 to 10 do
  begin
    TComboBox.Create(cba[I]);
    with cba[I] do
    begin
      visible:=true;
      enabled:=true;
      Left := 55;
      Height := 23;
      Top := 55+10*I;
      Width := 118;
      ItemHeight := 15;
      TabOrder := I+1;
      Text := 'CB'+IntToStr(I);
      Name := 'CB'+IntToStr(I);
    end;
  end;
end.

As soon as the state "visible := true" is hit, the program errors out.

So what am I doing wrong?

Also am I approaching this wrong?

Thanks

Knipfty
« Last Edit: August 07, 2012, 02:56:50 am by Knipfty »
64-bit Lazarus 2.2.0 FPC 3.2.2, 64-bit Win 11

ps

  • Full Member
  • ***
  • Posts: 136
    • CSS
Re: I want to create 10 TComboBoxes programatically and getting error 210
« Reply #1 on: August 06, 2012, 09:22:15 pm »
You forgot create object.
ex.: cba[1] := TComboBox.Create(Form1); //where Form1 is parent for TComboBox

Code: [Select]
var
   cba: array[1..10] of TComboBox;
   I: integer;
begin
  for I:=1 to 10 do
  begin
    cba[I] := TComboBox.Create(Form1); //user your parent instead Form1
    with cba[I] do
    begin
      visible:=true;
      enabled:=true;
      Left := 55;
      Height := 23;
      Top := 55+10*I;
      Width := 118;
      ItemHeight := 15;
      TabOrder := I+1;
      Text := 'CB'+IntToStr(I);
      Name := 'CB'+IntToStr(I);
    end;
  end;
end.
« Last Edit: August 06, 2012, 09:24:11 pm by ps »
Small simple CSS/box model implementation: https://github.com/pst2d/csscontrols/tree/dev

Knipfty

  • Full Member
  • ***
  • Posts: 232
Re: I want to create 10 TComboBoxes programatically and getting error 210
« Reply #2 on: August 06, 2012, 10:09:41 pm »
Ok!  That got rid of the run time error.  And I'm gald it was really easy to fix.  However, now when I run the program, nothing shows up on the form.

Code: [Select]
unit main;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, DBGrids,
  DbCtrls, Grids, StdCtrls, adscnnct, adstable, db, ShellAPI;

type

  { TForm1 }

  TForm1 = class(TForm)
  private
    { private declarations }
  public
    { public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.lfm}

Var
   cba: array[1..10] of TComboBox;
   I: integer;
begin
  for I:=1 to 10 do
  begin
    cba[I]:=TComboBox.Create(Form1);
    with cba[I] do
    begin
      visible:=true;
      enabled:=true;
      Left := 55;
      Height := 23;
      Top := 55+10*I;
      Width := 118;
      ItemHeight := 15;
      TabOrder := I+1;
      Text := 'CB'+IntToStr(I);
      Name := 'CB'+IntToStr(I);
    end;
  end;
end.

To me, it looks like there should be 10 combo boxes running down the form.  Each 10 pixels below the last.

Thanks

Knipfty
64-bit Lazarus 2.2.0 FPC 3.2.2, 64-bit Win 11

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: I want to create 10 TComboBoxes programatically and getting error 210
« Reply #3 on: August 06, 2012, 10:23:24 pm »
You should place that code in formCreate event. If you want to execute code in the unit, place it last after initialization
Code: [Select]
interface
...
implementation
...
procedure TForm1.FormCreate(Sender: TObject);
begin
  // Create components here. They will be automatically freed on form destroy.
end;

initialization

// Place startup code here, that doesn't relate to TForm

finalization

// Free non-form related objects

end.

Knipfty

  • Full Member
  • ***
  • Posts: 232
Re: I want to create 10 TComboBoxes programatically and getting error 210
« Reply #4 on: August 06, 2012, 10:30:56 pm »
OK.  I did that and still nothing.

Code: [Select]
unit main;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, DBGrids,
  DbCtrls, Grids, StdCtrls, adscnnct, adstable, db, ShellAPI;

type

  { TForm1 }

  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.lfm}


{ TForm1 }

procedure TForm1.FormCreate(Sender: TObject);
Var
   cba: array[1..10] of TComboBox;
   I: integer;

begin
  for I:=1 to 10 do
  begin
    cba[I]:=TComboBox.Create(Form1);
    with cba[I] do
    begin
      visible:=true;
      enabled:=true;
      Left := 55;
      Height := 23;
      Top := 55+10*I;
      Width := 118;
      ItemHeight := 15;
      TabOrder := I+1;
      Text := 'CB'+IntToStr(I);
      Name := 'CB'+IntToStr(I);
    end;
  end;

end;

begin
end.
64-bit Lazarus 2.2.0 FPC 3.2.2, 64-bit Win 11

eny

  • Hero Member
  • *****
  • Posts: 1634
Re: I want to create 10 TComboBoxes programatically and getting error 210
« Reply #5 on: August 06, 2012, 10:37:48 pm »
Code: [Select]
cba[I]:=TComboBox.Create(Form1);
 cba[I].parent := self;
All posts based on: Win10 (Win64); Lazarus 2.0.10 'stable' (x64) unless specified otherwise...

Knipfty

  • Full Member
  • ***
  • Posts: 232
Re: I want to create 10 TComboBoxes programatically and getting error 210
« Reply #6 on: August 06, 2012, 10:44:26 pm »
Hi eny,

THANKS!

Can you explain why I need that statement.  IT made all the difference.

Knipfty
64-bit Lazarus 2.2.0 FPC 3.2.2, 64-bit Win 11

eny

  • Hero Member
  • *****
  • Posts: 1634
All posts based on: Win10 (Win64); Lazarus 2.0.10 'stable' (x64) unless specified otherwise...

Knipfty

  • Full Member
  • ***
  • Posts: 232
Re: I want to create 10 TComboBoxes programatically and getting error 210
« Reply #8 on: August 07, 2012, 01:34:01 am »
Thanks again!
64-bit Lazarus 2.2.0 FPC 3.2.2, 64-bit Win 11

 

TinyPortal © 2005-2018