Creating Components dynamically
Pgina 1
Home - - A2 Databases - - A2 Scripts - - A2 Problems - - AS Hardware - Database Tutorials - - Lazarus Tutorials - - Lazarus - - Linux - -
Creating components dynamically in Lazarus
Version 1 Contents Introduction Preparations Key Ideas [Link] Create a label Create an edit box Create a button and is handler Create a StringGrid Create a Database Connection Create a DBGrid to see the data Copying code Appendix A : Getting SQLite3 Appendix B : Creating the SQLite3 database Appendix C : Getting the file [Link] Screenshot Source Code
Top
Introduction
This tutorial shows you how to create objects using source code. They are created as the program runs. The tutorial is a step-by-step guide. There are three advantages to this. If components are only needed in certain circumstances then you can create them when they are needed. The code creates everything including the buttons etc., so it can be inserted into other programs without the need to explain what components are needed The method gets credit in the COMP 4 exam I am working with Lazarus [Link] beta in MS Windows.
Top
Preparations
Create a folder called Dynamic Components. To save a Lazarus application into the folder Click File/New/Application and the familiar Form1 should appear Click File/Save All and navigate to your Dynamic Components folder Click Save (for the unit) and Save (for the project). Click the green run icon and check that a blank form appears Close the blank form. Lazarus is ready.
[Link]
04/04/2013 12:52:17
Creating Components dynamically Optional: If you want to link to the database then you need the items below as well. You need SQLite3 to create the database. See Appendix A. You can to create an SQLite database called A beforehand. See Appendix B. Paste A into the 'Dynamic Components' folder with your Lazarus project. Add the file [Link]. It is an easy download. See Appendix C.
Pgina 2
Top
Key ideas
We will use the event handler '[Link]' as a trigger, so that everything happens as the form is created. Then all the components appear immediately with the form. For each component you create the component with a line such as 'MyButton:=[Link](self)> define what it looks like by setting its properties such as '[Link]:=30' make it visible with a line such as '[Link]:=self'
Top
[Link]
Click on Form1. Go to the Object Inspector. click the Events tab. Find 'OnCreate'. Double-click in the edit box to create the handler. Most of the code below goes into this procedure.
Top
Create a label
Add the highlighted code below into the Implementation Var section implementation { TForm1 } var NewLabel:TLabel; Add the highlighted code below into the OnCreate procedure, compile and run. You should see the label on the form. procedure [Link](Sender: TObject); begin NewLabel:=[Link](self); [Link]:='dynamic components'; [Link]:=200; [Link]:=75; [Link]:='Comic Sans MS'; [Link]:=self;
Top
Create an edit box
[Link]
04/04/2013 12:52:17
Creating Components dynamically Add the highlighted code below into the Implementation Var section NewEdit:TEdit; Add the highlighted code below into the OnCreate procedure, compile and run. You should see the label on the form. NewEdit:=[Link](self); [Link]:=100; [Link]:=100; [Link]:=75; [Link]:=$00D3F4FF; [Link]:=self;
Pgina 3
Top
Create a button and an OnClick handler
Add the highlighted code below into the Implementation Var section NewButton:TButton; Add StdCtrls to the uses list. Add the highlighted code below into the OnCreate procedure compile and run. You should see the button on the form. NewButton:=[Link](self); [Link]:=75; [Link]:=84; [Link]:=47; [Link]:='Click me'; [Link]:=self; //[Link]:=@NewButtonClick; Add the following code above the FormCreate procedure: procedure [Link](Sender: TObject); begin [Link]:='button clicked'; end; Add a line into the Form definition: TForm1 = class(TForm) procedure FormCreate(Sender: TObject); private { private declarations } procedure NewButtonClick(Sender: TObject); NOW UNCOMMENT THE LINE [Link]:=@NewButtonClick; Compile and run. The button should change the text in the edit box
Top
Create a StringGrid
Add the highlighted code below into the Implementation Var section
[Link]
04/04/2013 12:52:17
Creating Components dynamically
Pgina 4
NewStringgrid:TStringGrid; Add Grids to the uses section. Add the highlighted code below into the OnCreate procedure Compile and run. You should see the StringGrid on the form. NewStringgrid:=[Link](self); [Link]:=10; [Link]:=100; [Link]:=300; [Link]:=75; [Link]:=self;
Top
Create a Database Connection
Add the highlighted code below into the Implementation Var section SQLConnection1:TSQLite3Connection; SQLTransaction1:TSQLTransaction; SQLQuery1:TSQLQuery; SQLDatasource1:TDatasource; Add the code below to the [Link] procedure - do what is said in the comments! //add sqlite3conn to uses at the top SQLConnection1:=[Link](self); [Link]:='[Link]'; [Link]:=true; //add sqldb to uses SQLTransaction1:=[Link](self); [Link]:=SQLConnection1; [Link]:=SQLTransaction1; [Link]:=true; SQLQuery1:=[Link](self); [Link]:=SQLConnection1; [Link]:=SQLTransaction1; [Link]:='SELECT * FROM Student;'; SQLDatasource1:=[Link](self); [Link]:=SQLQuery1; Complie and run and there should be no errors
Top
Create a DBGrid to show the data
Add the highlighted code below into the Implementation Var section SQLDBGrid1:TDBGrid; Add DBGrids to the uses section. Add the highlighted code below into the OnCreate procedure Compile and run. You should see the Student table data in the DBGrid
[Link]
04/04/2013 12:52:17
Creating Components dynamically
Pgina 5
// Add DBGrids to uses section SQLDBGrid1:=[Link](self); [Link]:=SQLDatasource1; [Link]:=true; [Link]:=200; [Link]:=200; [Link]:=20; [Link]:=250; [Link]:=Self; [Link];
Top
Copying code
You can copy and paste code that creates components dynamically directly into a project of your own. Copy the whole of the unit Open a new Lazarus application Paste the code into your unit Click on Form1 and open the Object Inspector Click on the events tab and find 'OnCreate' Make sure that your procedure is selected or the form will be blank Compile and run The Form and components should be visible
Top
Appendix A
Getting [Link] The program can be obtained from the SQLite website . It can be installed and run on a memory stick (flash drive).
Top
Appendix B
Creating a simple SQLite database called [Link] You need to create an SQLite database. My database is called [Link], and it has one table called Student, with three columns - StudentID(number), First(text) and Second(text). There are three rows in the table. To produce the database Open a text editor like Notepad Save a text file with filename [Link] Copy and paste the SQL below into it. -- SQL for the Student table CREATE TABLE Student( StudentID INTEGER PRIMARY KEY NOT NULL, First VARCHAR(20), Second VARHAR(20)); -- Some Student Values INSERT INTO Student VALUES (1, David, Beckham); INSERT INTO Student VALUES (2, William, Shakespeare); INSERT INTO Student VALUES (3, Reginald, Dwight); -- End of SQL
[Link]
04/04/2013 12:52:17
Creating Components dynamically Then Save the file into the same folder as [Link] Double-click [Link] Type .read [Link] (just copy and paste this text) Type SELECT * FROM Student (at the SQLite prompt) You should see the three records. Type .backup [Link] The Student table should have been imported along with the three records. Check they are there by browsing them.
Pgina 6
Top
Appendix C
Getting [Link] This file can be obtained from the SQLite website. You download an installer for the file. The installer should be executed. It will create a folder called [Link] and this contains the [Link] file. The file needs to go into your SQLite3Connect folder with your project
Top
Screenshot of the form
Top
Source Code
unit Unit1; {$mode objfpc}{$H+} interface uses Classes, sqlite3conn, sqldb, db, SysUtils, FileUtil, LResources, Forms, Controls, StdCtrls, Grids, Graphics, Dialogs, DbCtrls, DBGrids; type { TForm1 }
[Link]
04/04/2013 12:52:17
Creating Components dynamically
Pgina 7
TForm1 = class(TForm) procedure FormCreate(Sender: TObject); private { private declarations } procedure NewButtonClick(Sender: TObject); public { public declarations } end; var Form1: TForm1; implementation { TForm1 } var NewButton:TButton; NewEdit:TEdit; NewLabel:TLabel; NewStringgrid:TStringGrid; SQLConnection1:TSQLite3Connection; SQLTransaction1:TSQLTransaction; SQLQuery1:TSQLQuery; SQLDatasource1:TDatasource; SQLDBGrid1:TDBGrid; procedure [Link](Sender: TObject); begin [Link]:='button clicked'; end; procedure [Link](Sender: TObject); begin //add StdCtrls to uses at the top NewButton:=[Link](self); [Link]:=75; [Link]:=84; [Link]:=47; [Link]:='Click me'; [Link]:=self; [Link]:=@NewButtonClick; NewEdit:=[Link](self); [Link]:=100; [Link]:=100; [Link]:=75; [Link]:=$00D3F4FF; [Link]:=self; NewLabel:=[Link](self); [Link]:='dynamic compone nts'; [Link]:=200; [Link]:=75; [Link]:='Comic Sans MS '; [Link]:=self; // add grids to uses at the top NewStringgrid:=[Link]( self); [Link]:=10; [Link]:=100; [Link]:=300; [Link]:=75; [Link]:=self; //We will now connect to a database //You will need to have a database called 'A' with a table called 'Student' //in the project folder. //You will need to have the file [Link] in the project folder //Note that the non-visible components do not need the .Parent=self line
[Link]
04/04/2013 12:52:17
Creating Components dynamically // because this makes a component visible. //Add sqlite3conn to uses at the top SQLConnection1:=[Link](self); [Link]:='[Link]'; [Link]:=true; //add sqldb to uses SQLTransaction1:=[Link](self); [Link]:=SQLConnection1; [Link]:=SQLTransaction1; [Link]:=true; SQLQuery1:=[Link](self); [Link]:=SQLConnection1; [Link]:=SQLTransaction1; [Link]:='SELECT * FROM Student;'; SQLDatasource1:=[Link](self); [Link]:=SQLQuery1; // Add DBGrids to uses section SQLDBGrid1:=[Link](self); [Link]:=SQLDatasource1; [Link]:=true; [Link]:=200; [Link]:=200; [Link]:=20; [Link]:=250; [Link]:=Self; [Link]; end; initialization {$I [Link]} end.
Pgina 8
[Link]
04/04/2013 12:52:17
Creating Components dynamically
Pgina 9
[Link]
04/04/2013 12:52:17