Search code examples
formslazarus

lazarus form creation


The following code is from http://wiki.freepascal.org/MySQLDatabases

procedure TFormTryMySQL.OpenQueryButtonClick(Sender: TObject);
begin
 ShowQueryForm := TShowQueryForm.Create(self);
 ShowQueryForm.Datasource1.DataSet := SQLQuery1;
 SQLQuery1.SQL.Text := CommandEdit.Text;
 SQLQuery1.Open;
 ShowQueryForm.ShowModal;
 ShowQueryForm.Free;
 SQLQuery1.Close;
end;

I'm new to Lazarus. Can someone please explain the line ShowQueryForm := TShowQueryForm.Create(self);? I'm particularly curious about:

  1. Why do we need to CREATE a form programmatically?
  2. What is the TShowQueryForm?
  3. My form is without a T. How come the SQLQuery1 control can access the data on the new form?

I'm sorry if this is not a well-phrased question but I AM confused here :(

Thanks!


Solution

  •  1.    Why do we need to CREATE a form programmatically?
    

    It's optional. You can let forms autocreate, as vfclists says, in which the designer adds the form creation to the LPR. (just like Delphi btw).

     2. What is the TShowQueryForm? My form is without a T. 
    

    The type of the form. So you have a variable name xxxsomeForm with Txxxsomeform as its specific type.

     3. How come the SQLQuery1 control can access the data on the new form?
    

    Other way around. The grids, or whatever db aware controls are in showqueryform get their data from a datasource object. In the 2nd line:

     ShowQueryForm.Datasource1.DataSet := SQLQuery1;
    

    the sqlquery component of the current form is assigned to the dataset on the newly created form.