Forum > Beginners
How to populate TComboBox on FormCreate
(1/1)
facido:
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 [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TForm1.Button1Click(Sender: TObject);begin AddNewEntry.ShowModal;end;
The form with combo box is as follows:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---unit AddEntry;{$mode objfpc}{$H+}interfaceuses Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls;type { TAddNewEntry } TAddNewEntry = class(TForm) Category: TComboBox; procedure OnCreate(Sender: TObject); private public end; var AddNewEntry: TAddNewEntry;implementation{$R *.lfm}procedure TAddNewEntry.OnCreate(Sender: TObject);begin Category.Items.Clear; Category.Items.Add('Item1'); Category.Items.Add('Item2');end; end.
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.
balazsszekely:
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 [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---unit AddEntry;{$mode objfpc}{$H+}interfaceuses Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls;type { TAddNewEntry } TAddNewEntry = class(TForm) Category: TComboBox; procedure FormCreate(Sender: TObject);// procedure OnCreate(Sender: TObject); private public end; var AddNewEntry: TAddNewEntry;implementation{$R *.lfm} {procedure TAddNewEntry.OnCreate(Sender: TObject);begin Category.Items.Clear; Category.Items.Add('Item1'); Category.Items.Add('Item2');end;} { TAddNewEntry } procedure TAddNewEntry.FormCreate(Sender: TObject);begin Category.Items.Clear; Category.Items.Add('Item1'); Category.Items.Add('Item2');end;
facido:
Thanks a lot.
Navigation
[0] Message Index