6

Using Delphi 2010

SQLQuery1.First; // move to the first record
while(not SQLQuery1.EOF)do begin
   // do something with the current record
   // What's the code should i write in this part in order to create a TEdit
   // containing the user fullname the current item.
   ShowMessage(SQLQuery1['whom']);
   SQLQuery1.Next; // move to the next record
end;
menjaraz
  • 7,551
  • 4
  • 41
  • 81
Rafik Bari
  • 4,867
  • 18
  • 73
  • 123
  • Why do you want create a component by each record? are you trying using a dbgrid instead? – RRUZ Dec 16 '11 at 13:26
  • Do you want an edit control for each and every record? Are these going inside of a grid or elsewhere? If using a DBGrid, there's no need to do this at all, it already supports editing. If you need it for other reasons, then you will need to somehow maintain a list of these controls somewhere so you can properly free them when done. It highly depends on where you want to put these controls and how long you want these controls to be visible. I personally think (if not using a DBGrid) that you may want to use a string grid instead (shows an edit area for every record). – Jerry Dodge Dec 16 '11 at 14:11
  • PS - `ShowMessage(SQLQuery1['whom']);` is safer to be `ShowMessage(SQLQuery1.FieldByName('whom').AsString);` – Jerry Dodge Dec 16 '11 at 14:13
  • Safer from what, @Jerry? – Rob Kennedy Dec 16 '11 at 16:00
  • @RobKennedy If for any reason 'whom' is NULL it can fail, because this is a variant type and it treats NULL differently. Or otherwise just making sure that you're explicitly using the correct type can avoid many various conversion issues. – Jerry Dodge Dec 16 '11 at 16:32

4 Answers4

6

Well, to create a TEdit you need to do the following:

Create a variable to work with. Either a local variable or a class member.

Edit: TEdit;

Then you construct it.

Edit := TEdit.Create(Self);

The parameter to the constructor is the owner. This ensures that the control is destroyed when its owner is destroyed. My assumption is that Self is a form.

Now you need to give the control a parent.

Edit.Parent := Self;

Or perhaps it's on a panel.

Edit.Parent := StatusPanel;

Finally, you set the text.

Edit.Text := SQLQuery1['whom']);

With a label it's all very similar except that you use the Caption property rather than the Text property.

And you will surely want to set other properties but I guess you already know how to do that.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • The best way to find out which properties are important to set when dynamically creating a component is to place one on a form, switch to viewing it as text, and see which properties are streamed. Those ones you should consider setting when you create it dynamically too. (After all, streamed components are dynamically created :)) – David Dec 16 '11 at 16:15
4

You can also design the components visually, use the GExperts Components to Code expert on them and then delete them from the form designer again. For a label/edit pair this gives something like

var
  Edit1: TEdit;
  Label1: TLabel;

  Edit1 := TEdit.Create(Self);
  Label1 := TLabel.Create(Self);

  Edit1.Name := 'Edit1';
  Edit1.Parent := Self;
  Edit1.Left := 344;
  Edit1.Top := 172;
  Edit1.Width := 121;
  Edit1.Height := 21;
  Edit1.TabOrder := 0;
  Edit1.Text := 'Edit1';
  Label1.Name := 'Label1';
  Label1.Parent := Self;
  Label1.Left := 296;
  Label1.Top := 176;
  Label1.Width := 65;
  Label1.Height := 17;
  Label1.Caption := 'Label1';
  Label1.FocusControl := Edit1;

Most of the times it needs some reworking (remove the TabOrder lines, replace the Left/Top/... stuff by SetBounds, Align or your own logic, ...) and for some properties/components it doesn't work at all. But you can save a lot of time that way.

Uli Gerhardt
  • 13,748
  • 1
  • 45
  • 83
3
Var
  AnEdit : TEdit;
Begin
  AnEdit := TEdit.Create(self);
  AnEdit.Parent := self; // or some suitable container compoent e.g GroupBox, Panel
  AnEdit.Top := ?;
  AnEdit.Left := ?
  // any other properties you weant to set.
End;

The bit that catches people out is setting parent.

Tony Hopkinson
  • 20,172
  • 3
  • 31
  • 39
1
with TEdit.Create(self) do
begin
  Parent:= ... // The name of the panel or form, on which you would like to place TEdit
  Text:= 'your text'; 
  // And you could set its position by giving "Left" and/or "Width", so on..
end;
csobang
  • 11
  • 3