Forum > General

How to put controls taken from mysql on form?

(1/3) > >>

krexon:
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:

--- Quote from: krexon on December 17, 2010, 11:58:03 am ---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?

--- End quote ---

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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---function CreateEdit(AForm: TForm; {the params you will need}): Boolean;var   NewEdit: TEdit;begin   Result := False   NewEdit := nil;   NewEdit := TEdit(AForm);   NewEdit.Parent := AForm;   { set NewEdit name, it's important to get it's value }   { set NewEdit Value }   { set NewEdit position, justification and everything else }   Result := True;end; { similar to the previous one }function CreateNewEdit(AForm: TForm; {the params you will need}): TEdit;begin   Result := nil;   Result := TEdit(AForm);   Result.Parent := AForm;   { set NewEdit name, it's important to get it's value }   { set NewEdit Value }   { set NewEdit position, justification and everything else }end; function CreateAnonymousEdit(AForm: TForm; {the params you will need}): Boolean;begin   Result := False   with TEdit(AForm) do begin   NewEdit.Parent := AForm;      { set NewEdit name, it's important to get it's value }      { set NewEdit Value }      { set NewEdit position, justification and everything else }   end;   Result := True;end;  

krexon:
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:
If I was you I would design the whole form at desing time and set Visible True or False to the controls.

krexon:
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 :)

Navigation

[0] Message Index

[#] Next page

Go to full version