Board index » delphi » dynamic alloc of edit boxes on a child form

dynamic alloc of edit boxes on a child form

Hi chaps

I'm pretty new to Delphi, but did all my varsity years using Pascal
(Borland V3 to 5.5)

I'm writing an application that takes as input a set of text files.
These files define various text displays, graphs, etc.
One of the definitions is for an input form.
like this
*INPUT
explanatory text   ##inputvariablename##
more text ##inputvarname2##  and some more text
....

The input variables are defined as to type etc in a previous section.

Since I don't know at design time how many input fields the form needs,
nor where they need to go (have to do runtime formatting too)
I need a way of adding various edit controls to the form at run time.
How many?   1 to 150 or so......the form can scroll, that's ok
The form is an MDIchild, since I need multiple forms open at once, one
for text instructions, one for input, a graph, etc

Would be nice if I could add buttons to the form at runtime too.....

Delphi3 pro

thanks

--
       David Forsyth                 webmeister A-T Iwr.Ru.Ac.Za
Keeper of the listserver for South African Railways fans       _|_ His
Part time gricer, kiter, photographer, father etc etc           |  Way
http://www.ru.ac.za/departments/iwr/staff/daf/welcome.html      |  Up
For reduction of advertising mail, my return address is invalid.
Read the address above and figure it out please.

 

Re:dynamic alloc of edit boxes on a child form


Quote
In article <34D18AC6.7...@ru.ac.za>, David Forsyth <fa...@ru.ac.za> wrote:

]-Hi chaps
]-
]-I'm pretty new to Delphi, but did all my varsity years using Pascal
]-(Borland V3 to 5.5)
]-
]-I'm writing an application that takes as input a set of text files.
]-These files define various text displays, graphs, etc.
]-One of the definitions is for an input form.
]-like this
]-*INPUT
]-explanatory text   ##inputvariablename##
]-more text ##inputvarname2##  and some more text
]-.....
]-
]-The input variables are defined as to type etc in a previous section.
]-
]-Since I don't know at design time how many input fields the form needs,
]-nor where they need to go (have to do runtime formatting too)
]-I need a way of adding various edit controls to the form at run time.
]-How many?   1 to 150 or so......the form can scroll, that's ok
]-The form is an MDIchild, since I need multiple forms open at once, one
]-for text instructions, one for input, a graph, etc

try this...

first declare some data structures to hold your dynamically
created controls (in real life you might want to use a couple
of TLists instead of the arrays I've used below...)

  Const
    MaxDynamicControls = 150;

  Type
    TLabelArray = array[1..MaxDynamicControls] of TLabel;
    TEditArray  = array[1..MaxDynamicControls] of TEdit;

  Var
    cLabel : TLabelArray;
    cEdit  : TEditArray;

add a couple of procedures to your main form's private section...
these will be the OnEnter and OnExit events for the dynamically
created edit boxes...

  procedure TMainForm.dEditEnter(Sender: TObject);
  {= doesn't do anything really useful... =}
    BEGIN
      if Sender IS TEdit then
        BEGIN
          TEdit(Sender).Font.Color := clRed;
          cLabel[ TEdit(Sender).Tag ].Font.Color := clRed;
        END;
    END;

  procedure TMainForm.dEditExit(Sender: TObject);
  {= doesn't do anything really useful... =}
    BEGIN
      if Sender IS TEdit then
        BEGIN
          TEdit(Sender).Font.Color := clBlue;
          cLabel[ TEdit(Sender).Tag ].Font.Color := clBlue;
        END;
    END;

now drop a TButton and a TSpinEdit somewhere on your form, and
add the following code to the OnClick handler of the TButton...

  procedure TMainForm.Button1Click(Sender: TObject);
    Var
      Child : TMDIChild;
      dNum  : integer;
    BEGIN
      if (SpinEdit1.Value = 0) then
        BEGIN
          ShowMessage('Nothing to do, boss');
          exit;
        END;
      Child := TMDIChild.Create(Application);
      with Child do
        BEGIN
          Caption := 'Dynamic Component Demo';
          for dNum := 1 to SpinEdit1.Value do
            BEGIN
              cLabel[dNum] := TLabel.Create(Child);
              with cLabel[dNum] do
                BEGIN
                  Parent := TWinControl(Owner);
                  left := 8;
                  top  := 16 + (dNum-1)*(height + 16);
                  tag  := dNum;
                  Caption := 'cLabel[' + IntToStr(dNum) + ']';
                END;
              cEdit[dNum]  := TEdit.Create(Child);
              with cEdit[dNum] do
                BEGIN
                  Parent := TWinControl(Owner);
                  left := 8 + cLabel[dNum].left + cLabel[dNum].width;
                  top  := cLabel[dNum].top;
                  tag  := dNum;
                  text := 'cEdit[' + IntToStr(dNum) + ']';
                  OnEnter := dEditEnter;
                  OnExit  := dEditExit;
                END;
            END;
        END;
    END;

]-
]-Would be nice if I could add buttons to the form at runtime too.....

same general procedure as above...

]-Delphi3 pro

I did the example above using D1, but it works the same way
in D3.

Mark Vaughan

]-
]-thanks
]-

Other Threads