Sunteți pe pagina 1din 9

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 Form.Create 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 SQLite3.dll 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 0.9.28.2 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.

http://www.alevel-computing.x10.mx/TutorialDynamicComponents.php

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 SQlite3.dll. It is an easy download. See Appendix C.

Pgina 2

Top

Key ideas
We will use the event handler 'Form.OnCreate' 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:=TButton.create(self)> define what it looks like by setting its properties such as 'MyButton.width:=30' make it visible with a line such as 'MyButton.Parent:=self'

Top

Form.Create
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 TForm1.FormCreate(Sender: TObject); begin NewLabel:=TLabel.create(self); NewLabel.caption:='dynamic components'; NewLabel.Top:=200; NewLabel.left:=75; NewLabel.Font.name:='Comic Sans MS'; NewLabel.Parent:=self;

Top

Create an edit box

http://www.alevel-computing.x10.mx/TutorialDynamicComponents.php

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:=TEdit.create(self); NewEdit.width:=100; NewEdit.Top:=100; NewEdit.left:=75; NewEdit.color:=$00D3F4FF; NewEdit.Parent:=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:=TButton.create(self); NewButton.Left:=75; NewButton.Width:=84; NewButton.Height:=47; NewButton.caption:='Click me'; NewButton.parent:=self; //NewButton.OnClick:=@NewButtonClick; Add the following code above the FormCreate procedure: procedure TForm1.NewButtonClick(Sender: TObject); begin NewEdit.Text:='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 NewButton.OnClick:=@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

http://www.alevel-computing.x10.mx/TutorialDynamicComponents.php

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:=TStringGrid.create(self); NewStringGrid.colcount:=10; NewStringGrid.width:=100; NewStringGrid.Top:=300; NewStringGrid.left:=75; NewStringGrid.Parent:=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 Form.Create procedure - do what is said in the comments! //add sqlite3conn to uses at the top SQLConnection1:=TSQLite3Connection.create(self); SQLConnection1.DatabaseName:='A.db'; SQLConnection1.Connected:=true; //add sqldb to uses SQLTransaction1:=TSQLTransaction.create(self); SQLTransaction1.Database:=SQLConnection1; SQLConnection1.Transaction:=SQLTransaction1; SQLTransaction1.Active:=true; SQLQuery1:=TSQLQuery.create(self); SQLQuery1.Database:=SQLConnection1; SQLQuery1.Transaction:=SQLTransaction1; SQLQuery1.SQL.Text:='SELECT * FROM Student;'; SQLDatasource1:=TDatasource.create(self); SQLDatasource1.DataSet:=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

http://www.alevel-computing.x10.mx/TutorialDynamicComponents.php

04/04/2013 12:52:17

Creating Components dynamically

Pgina 5

// Add DBGrids to uses section SQLDBGrid1:=TDBGrid.create(self); SQLDBGrid1.Datasource:=SQLDatasource1; SQLDBGrid1.Enabled:=true; SQLDBGrid1.Height:=200; SQLDBGrid1.Width:=200; SQLDBGrid1.Top:=20; SQLDBGrid1.Left:=250; SQLDBGrid1.Parent:=Self; SQLQuery1.open;

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 SQLite3.exe 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 A.db You need to create an SQLite database. My database is called A.db, 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 A.txt 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

http://www.alevel-computing.x10.mx/TutorialDynamicComponents.php

04/04/2013 12:52:17

Creating Components dynamically Then Save the file into the same folder as SQLite3.exe Double-click SQLite3.exe Type .read A.txt (just copy and paste this text) Type SELECT * FROM Student (at the SQLite prompt) You should see the three records. Type .backup A.db 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 SQLite3.dll 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 SQLite3.dll and this contains the SQLite3.dll 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 }

http://www.alevel-computing.x10.mx/TutorialDynamicComponents.php

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 TForm1.NewButtonClick(Sender: TObject); begin NewEdit.Text:='button clicked'; end; procedure TForm1.FormCreate(Sender: TObject); begin //add StdCtrls to uses at the top NewButton:=TButton.create(self); NewButton.Left:=75; NewButton.Width:=84; NewButton.Height:=47; NewButton.caption:='Click me'; NewButton.parent:=self; NewButton.OnClick:=@NewButtonClick; NewEdit:=TEdit.create(self); NewEdit.width:=100; NewEdit.Top:=100; NewEdit.left:=75; NewEdit.color:=$00D3F4FF; NewEdit.Parent:=self; NewLabel:=TLabel.create(self); NewLabel.caption:='dynamic compone nts'; NewLabel.Top:=200; NewLabel.left:=75; NewLabel.Font.name:='Comic Sans MS '; NewLabel.Parent:=self; // add grids to uses at the top NewStringgrid:=TStringGrid.create( self); NewStringGrid.colcount:=10; NewStringGrid.width:=100; NewStringGrid.Top:=300; NewStringGrid.left:=75; NewStringGrid.Parent:=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 SQLite3.dll in the project folder //Note that the non-visible components do not need the .Parent=self line

http://www.alevel-computing.x10.mx/TutorialDynamicComponents.php

04/04/2013 12:52:17

Creating Components dynamically // because this makes a component visible. //Add sqlite3conn to uses at the top SQLConnection1:=TSQLite3Connection.create(self); SQLConnection1.DatabaseName:='A.db'; SQLConnection1.Connected:=true; //add sqldb to uses SQLTransaction1:=TSQLTransaction.create(self); SQLTransaction1.Database:=SQLConnection1; SQLConnection1.Transaction:=SQLTransaction1; SQLTransaction1.Active:=true; SQLQuery1:=TSQLQuery.create(self); SQLQuery1.Database:=SQLConnection1; SQLQuery1.Transaction:=SQLTransaction1; SQLQuery1.SQL.Text:='SELECT * FROM Student;'; SQLDatasource1:=TDatasource.create(self); SQLDatasource1.DataSet:=SQLQuery1; // Add DBGrids to uses section SQLDBGrid1:=TDBGrid.create(self); SQLDBGrid1.Datasource:=SQLDatasource1; SQLDBGrid1.Enabled:=true; SQLDBGrid1.Height:=200; SQLDBGrid1.Width:=200; SQLDBGrid1.Top:=20; SQLDBGrid1.Left:=250; SQLDBGrid1.Parent:=Self; SQLQuery1.open; end; initialization {$I unit1.lrs} end.

Pgina 8

http://www.alevel-computing.x10.mx/TutorialDynamicComponents.php

04/04/2013 12:52:17

Creating Components dynamically

Pgina 9

http://www.alevel-computing.x10.mx/TutorialDynamicComponents.php

04/04/2013 12:52:17

S-ar putea să vă placă și