Recent

Author Topic: How to populate TComboBox on FormCreate  (Read 1270 times)

facido

  • New Member
  • *
  • Posts: 15
How to populate TComboBox on FormCreate
« on: July 16, 2018, 08:29:47 am »
Hi,
I am a beginner in Lazarus. I have simple testing codes to bring up a form with combo box on it. What I am trying to do is to fill the items on combo box when the form shows up.
The problem is when the form shows up, the combo box is empty.

The calling the form is as follows:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. begin
  3.   AddNewEntry.ShowModal;
  4. end;  
  5.  

The form with combo box is as follows:
Code: Pascal  [Select][+][-]
  1. unit AddEntry;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses
  5.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls;
  6. type
  7.   { TAddNewEntry }
  8.   TAddNewEntry = class(TForm)
  9.     Category: TComboBox;
  10.     procedure OnCreate(Sender: TObject);
  11.   private
  12.   public
  13.   end;
  14.  
  15. var
  16.   AddNewEntry: TAddNewEntry;
  17. implementation
  18. {$R *.lfm}
  19. procedure TAddNewEntry.OnCreate(Sender: TObject);
  20. begin
  21.    Category.Items.Clear;
  22.    Category.Items.Add('Item1');
  23.    Category.Items.Add('Item2');
  24. end;
  25.  
  26. end.
  27.  

Why is the combo box is empty?
I tried to put the combo box filling codes inside "OnShow" and "OnActivate", but the result is the same.

I attached this simple testing codes here.

GetMem

  • Hero Member
  • *****
  • Posts: 3757
Re: How to populate TComboBox on FormCreate
« Reply #1 on: July 16, 2018, 08:37:56 am »
Your code is never executed, instead of OnCreate you need FormCreate. Just go to the Object Inspector/Events and double click the OnCreate event.
Code: Pascal  [Select][+][-]
  1. unit AddEntry;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses
  5.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls;
  6. type
  7.   { TAddNewEntry }
  8.   TAddNewEntry = class(TForm)
  9.     Category: TComboBox;
  10.     procedure FormCreate(Sender: TObject);
  11. //    procedure OnCreate(Sender: TObject);
  12.   private
  13.   public
  14.   end;
  15.  
  16. var
  17.   AddNewEntry: TAddNewEntry;
  18. implementation
  19. {$R *.lfm}
  20.  
  21. {procedure TAddNewEntry.OnCreate(Sender: TObject);
  22. begin
  23.    Category.Items.Clear;
  24.    Category.Items.Add('Item1');
  25.    Category.Items.Add('Item2');
  26. end;}
  27.  
  28. { TAddNewEntry }
  29.  
  30. procedure TAddNewEntry.FormCreate(Sender: TObject);
  31. begin
  32.   Category.Items.Clear;
  33.   Category.Items.Add('Item1');
  34.   Category.Items.Add('Item2');
  35. end;

facido

  • New Member
  • *
  • Posts: 15
Re: How to populate TComboBox on FormCreate
« Reply #2 on: July 16, 2018, 08:50:52 am »
Thanks a lot.

 

TinyPortal © 2005-2018