Recent

Author Topic: How to put controls taken from mysql on form?  (Read 9205 times)

krexon

  • Jr. Member
  • **
  • Posts: 80
How to put controls taken from mysql on form?
« on: December 17, 2010, 11:58:03 am »
I need help :)

On main form i have combobox with customers. When I select a customer and click button, new form appears (ie. form2). I want to put controls on form2 taken from mysql.
My customer's table in mysql looks like:
customer_id; ... ; controls_ids; ...
My control's table looks like:
control_id (autoincrement); label; type; alignment

label - label's caption
type - 0, 1 or 2 (0 - TEdit, 1 - TMemo, 2 - TdateTime)
alignment - text alignment in control (0 - taLeftJustify, 1 - taCenter, 2 - taRightJustify)

Customer's table data:
customerA; 1,2,3,4
customerB; 2,3,4
customerC; 1,3,4

Control's table data:
1; aaaaaa; 0; 0
2; bbbbbb; 1; 0
3; cccccc; 2; 1
4; dddddd; 0, 2

So, when I select customerA and click button, form2 opens with 4 controls - TEdit with leftjustify, TMemo with leftjustify, TDateTime with center alignment, TEdit with leftjustify.
When I select customerB and click button, form2 opens with 3 controls - TMemo with leftjustify, TDateTime with center alignment, TEdit with leftjustify and so on.

Before I put TEdit1.Create I have to declare TEdt1:TEdit in var section, but I don't know what and how many controls I will put on form. Additionally I can add controls to mysql.

Can someone give an idea how to solve my problem?

garlar27

  • Hero Member
  • *****
  • Posts: 652
Re: How to put controls taken from mysql on form?
« Reply #1 on: December 17, 2010, 01:17:43 pm »
Before I put TEdit1.Create I have to declare TEdt1:TEdit in var section, but I don't know what and how many controls I will put on form. Additionally I can add controls to mysql.

Can someone give an idea how to solve my problem?

You don't need to declare all the TEdit you will need because it's not necessary. At this point you can do it two different ways (that I can think of) both of them are specialized functions:

1st: You declare a TEdit variable, then create it, set it's owner and parent, all it's properties and forget about it.

2nd: Do an "anonymous" object creation, set it's properties and thats it...

The code would be something like this:

Code: Pascal  [Select][+][-]
  1. function CreateEdit(AForm: TForm; {the params you will need}): Boolean;
  2. var
  3.    NewEdit: TEdit;
  4. begin
  5.    Result := False
  6.    NewEdit := nil;
  7.    NewEdit := TEdit(AForm);
  8.    NewEdit.Parent := AForm;
  9.    { set NewEdit name, it's important to get it's value }
  10.    { set NewEdit Value }
  11.    { set NewEdit position, justification and everything else }
  12.    Result := True;
  13. end;
  14.  
  15. { similar to the previous one }
  16. function CreateNewEdit(AForm: TForm; {the params you will need}): TEdit;
  17. begin
  18.    Result := nil;
  19.    Result := TEdit(AForm);
  20.    Result.Parent := AForm;
  21.    { set NewEdit name, it's important to get it's value }
  22.    { set NewEdit Value }
  23.    { set NewEdit position, justification and everything else }
  24. end;
  25.  
  26. function CreateAnonymousEdit(AForm: TForm; {the params you will need}): Boolean;
  27. begin
  28.    Result := False
  29.    with TEdit(AForm) do begin
  30.    NewEdit.Parent := AForm;
  31.       { set NewEdit name, it's important to get it's value }
  32.       { set NewEdit Value }
  33.       { set NewEdit position, justification and everything else }
  34.    end;
  35.    Result := True;
  36. end;
  37.  
  38.  

krexon

  • Jr. Member
  • **
  • Posts: 80
Re: How to put controls taken from mysql on form?
« Reply #2 on: December 17, 2010, 01:40:05 pm »
Sorry for stupid question, but should I use only one of these 3 functions?
How to free controls? Or are they freed after closing form?
I understand that creating TMemo and TDateTime is similar. How to create ie. 5 TEdits?

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: How to put controls taken from mysql on form?
« Reply #3 on: December 17, 2010, 02:50:19 pm »
If I was you I would design the whole form at desing time and set Visible True or False to the controls.

krexon

  • Jr. Member
  • **
  • Posts: 80
Re: How to put controls taken from mysql on form?
« Reply #4 on: December 17, 2010, 03:02:30 pm »
At beginning I thought about it, but now I have 10 TEdits, 4 TDateTime and 1 TMemo (15 controls all together) and only 6-7 of them is required for customer.
It would have generate a lot of holes in form :) So I thought of dynamically creating controls.
In future it can be more TEdits and TDateTime controls, so second solution is better :)
« Last Edit: December 17, 2010, 03:04:47 pm by krexon »

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: How to put controls taken from mysql on form?
« Reply #5 on: December 17, 2010, 03:19:32 pm »
To create them dinamically you need to use the constructor Create and set the Owner properly as parameter of the constructor. This ensures them to be freed once the Owner is freed.

garlar27

  • Hero Member
  • *****
  • Posts: 652
Re: How to put controls taken from mysql on form?
« Reply #6 on: December 17, 2010, 10:23:41 pm »
What typo said here is the safer option:
If I was you I would design the whole form at desing time and set Visible True or False to the controls.

He also explained how it works here:
To create them dinamically you need to use the constructor Create and set the Owner properly as parameter of the constructor. This ensures them to be freed once the Owner is freed.

the three functions are 3 ways to do the same thing. Personally I like the second one you can use it as it where a procedure or catch the result to do and special config.

Let me give you some advice about creating "disposable" components in a form:

If you are going to use disposable components in a form it is better that the form which you use to create and show the components, should also be disposable.

Maybe an example would help to explain my point:
Code: Pascal  [Select][+][-]
  1. procedure RunDisposableForm;
  2. var
  3.    Form: TForm;
  4. begin
  5.    Form := TForm.Create(nil{ this is important so your form won't remain in memmory until you close the main program });
  6.    try
  7.       { create your components }
  8.       Form.ShowModal;
  9.  
  10.       { process the data obtained from the form }
  11.    finally
  12.       Form.Free; { YOU HAVE TO RELEASE THE MEMMORY THAT YOU DONT NEED ANY MORE!!!}
  13.    end;
  14. end;
  15.  

remember to pay attention to what are you doing because you're walking on thin ice  :o

krexon

  • Jr. Member
  • **
  • Posts: 80
Re: How to put controls taken from mysql on form?
« Reply #7 on: December 18, 2010, 10:39:45 pm »
Thanks for all your ideas :)
I think I will try with putting controls at design time and hiding not used with selected customer. To avoid a lot of empty spaces between controls, I thought of using variable t:=t+20 which represents top position of TEdit, TDateTime, t:=t+110 for TMemo.

garlar27

  • Hero Member
  • *****
  • Posts: 652
Re: How to put controls taken from mysql on form?
« Reply #8 on: December 20, 2010, 12:45:39 pm »
You can use anchors to help you fit and organize your controls inside the form. It's a little tricky to set anchors from code, but not impossible.

The following code creates a Form and 2 Labels and anchor a Label1 to the top left of a form and Label2 is anchored to the Top Right of Label1:
Code: [Select]
MyForm := TFotm.Create(nil);
try
   Label1 := TLabel.Create(MyForm);
   Label2 := TLabel.Create(MyForm);

   Label1 := MyForm;
   Label2 := MyForm;

   Label1.AutoSize    := TRUE;
   Label1.ParentFont  := TRUE;
   Label1.Transparent := TRUE;
   Label1.Top  := 0;
   Label1.Left := 0;

   Label1.AnchorSide[akLeft].Side := asrLeft;
   Label1.AnchorSide[akLeft].Control := MyForm;
   Label1.BorderSpacing.Left := 0;

   Label1.AnchorSide[akTop].Side := asrTop;
   Label1.AnchorSide[akTop].Control := MyForm;
   Label1.BorderSpacing.Top := 0;

   Label1.AnchorSide[akBottom].Side := asrBottom;
   Label1.AnchorSide[akBottom].Control := Self;
   Label1.BorderSpacing.Bottom := 0;


   Label2.AutoSize    := TRUE;
   Label2.ParentFont  := FALSE;
   Label2.Transparent := TRUE;

   Label2.AnchorSide[akLeft].Side := asrRight;
   Label2.AnchorSide[akLeft].Control := Label1;
   Label2.BorderSpacing.Left := 0;

   Label2.AnchorSide[akTop].Side := asrTop;
   Label2.AnchorSide[akTop].Control := Label1;
   Label2.BorderSpacing.Top := 0;

   MyForm.ShowModal;
finally
   MyForm.Free;
end;

I hope you can take some ideas from that (if this is what you need) ...

krexon

  • Jr. Member
  • **
  • Posts: 80
Re: How to put controls taken from mysql on form?
« Reply #9 on: December 20, 2010, 03:50:37 pm »
Thanks, I will try :)

garlar27

  • Hero Member
  • *****
  • Posts: 652
Re: How to put controls taken from mysql on form?
« Reply #10 on: December 21, 2010, 12:38:53 pm »
OOPS!!! I MISSED SOMETHING!!! :-[ :-[ :-[

I didn't assigned the Label's parent!!! Also I forgot to set labels values so you can see them... :-[

The code should be like this:
Code: [Select]
MyForm := TFotm.Create(nil);
try
   Label1 := TLabel.Create(MyForm);
   Label2 := TLabel.Create(MyForm);

   Label1.Parent := MyForm; // This is what you need to do to make controls visible in the form
   Label2.Parent := MyForm; // This is what you need to do to make controls visible in the form

   Label1.AutoSize    := TRUE;
   Label1.ParentFont  := TRUE;
   Label1.Transparent := TRUE;
   Label1.Top  := 0;
   Label1.Left := 0;

   Label1.AnchorSide[akLeft].Side := asrLeft;
   Label1.AnchorSide[akLeft].Control := MyForm;
   Label1.BorderSpacing.Left := 0;

   Label1.AnchorSide[akTop].Side := asrTop;
   Label1.AnchorSide[akTop].Control := MyForm;
   Label1.BorderSpacing.Top := 0;

   Label1.AnchorSide[akBottom].Side := asrBottom;
   Label1.AnchorSide[akBottom].Control := Self;
   Label1.BorderSpacing.Bottom := 0;


   Label2.AutoSize    := TRUE;
   Label2.ParentFont  := FALSE;
   Label2.Transparent := TRUE;

   Label2.AnchorSide[akLeft].Side := asrRight;
   Label2.AnchorSide[akLeft].Control := Label1;
   Label2.BorderSpacing.Left := 20; // this gives a 20 pixels margin from anchor.

   Label2.AnchorSide[akTop].Side := asrTop;
   Label2.AnchorSide[akTop].Control := Label1;
   Label2.BorderSpacing.Top := 0;

   Label1.Caption := 'Hello world!!';
   Label2.Caption := 'This Label2';

   MyForm.ShowModal;
finally
   MyForm.Free;
end;

 

TinyPortal © 2005-2018