Wednesday, June 11, 2014

Dynamically Creating Components in a Page Controller to another Page controller (Delphi)

This function can duplicate a Page controller into another given Page controller. I am using this for to create a search Panel.

DBEdit into a Text Box,

wwDBComboBox into the same type

User should have to set the main page controller component tag field accordingly.


Ex:

DBEdit1.tag := 21; mean, Type is Integer and component type is Text Box

Tags --> first 1 to 9 Database Types

1 --> text
2 --> Numeric
3 --> DateTime

Second 1 to 9 Component Type
1 --> TextBox
2 --> Combo Box
3 --> List Box
4 --> DateTimePicker
5 --> CheckBox

6 --> Option Button

With this sample, as a param user sending  and the 

  Current form using, Parent Page controller, Child Page controller. 

Function will create all the Pagetabs in the Parent page controller in to the Child page controller. 

Searching all the controllers which tag is > 0 (Need to be in the search panel) and create under the child page controller

procedure CreateSearchPanel(form :Tform; sht, shp :TPageControl);
var
   txt : TEdit;
   mem : TMemo;
   cmb : TwwDBLookupCombo;
   dtm : TwwDBDateTimePicker;
   chk : TCheckBox;
   i, j, k, iComp : Integer;
   tab :TTabSheet;
   Noti : TNotifyEvent;
   // M: TMethod;
Begin

    i := sht.PageCount -1;
    for k := 0 to i do
    Begin
        iComp := sht.Pages[k].ControlCount - 1;
        tab := TTabSheet.Create(form);
        tab.Visible := True;
        tab.Name := '_' + sht.Pages[k].name;
        tab.Top := sht.Pages[k].Top;
        tab.Left := sht.Pages[k].Left;
        tab.Height := sht.Pages[k].Height;
        tab.Width := sht.Pages[k].Width;
        tab.Caption := sht.Pages[k].Caption;
        tab.tag :=  99;
        tab.PageControl := shp;
        tab.Font.Color := sht.pages[k].font.Color;//clBlue;

        for j := 0 to iComp do
        Begin
              if (strToInt((copy(intToStr(sht.Pages[k].controls[j].Tag),1,1))) >=1) and (strToInt((copy(intToStr(sht.Pages[k].controls[j].Tag),1,1))) <=9) then
              Begin
                 CASE strToInt((copy(intToStr(sht.Pages[k].controls[j].Tag),2,1))) of
                   1:
                   Begin
                 
                        txt := TEdit.Create(form);

//                        txt.Name :=  '_' + TDBEdit(sht.Pages[k].controls[j]).Name;
                        if Trim(copy(intToStr(sht.Pages[k].controls[j].Tag),3,1)) = '' then
                                txt.Name :=  '_' + TDBEdit(sht.Pages[k].controls[j]).DataField
                        else
                                txt.Name := '_' + GetComponentFieldName(sht.Pages[k].controls[j],StrToInt(copy(intToStr(sht.Pages[k].controls[j].Tag),3,1)),true);
                        txt.font :=  TDBEdit(sht.Pages[k].controls[j]).font;
                        txt.Top :=  TDBEdit(sht.Pages[k].controls[j]).Top;
                        txt.Left :=  TDBEdit(sht.Pages[k].controls[j]).Left;
                        txt.Width := TDBEdit(sht.Pages[k].controls[j]).Width;
                        txt.Height := TDBEdit(sht.Pages[k].controls[j]).Height;
                        txt.Visible := True;
                        txt.Tag := TDBEdit(sht.Pages[k].controls[j]).Tag;
                        tab.InsertControl(txt);
                        txt.Text := '';
//                        TDBEdit(sht.Pages[k].controls[j]).DataSource.DataSet.FieldByName(TDBEdit(sht.Pages[k].controls[j]).DataField).DisplayName;
                        txt.PopupMenu := TPopupMenu(form.FindComponent('PopupMenu1'));
                        {M.Data := nil;
                        M.Code := @EditBoxEnterHandler;
                        txt.OnExit := TNotifyEvent(M); }

                        if Trim(copy(intToStr(sht.Pages[k].controls[j].Tag),3,1)) = '' then
                           SetLabel(form, sht.Pages[k], tab, TDBEdit(sht.Pages[k].controls[j]).DataSource.DataSet.FieldByName(TDBEdit(sht.Pages[k].controls[j]).DataField).DisplayLabel )
                        else
                           SetLabel(form, sht.Pages[k], tab, GetComponentFieldName(sht.Pages[k].controls[j],StrToInt(copy(intToStr(sht.Pages[k].controls[j].Tag),3,1))));
                        //SetLabel(form, sht.Pages[k], tab, txt.Name );
                        //CreateSQLLabels(form, tab, txt.name, txt.Top, txt.Left, txt.width, txt.height);

                   end;
                   2:
                   Begin

                        cmb := TwwDBLookupCombo.Create(form);
                        tab.InsertControl(cmb);
                        if Trim(copy(intToStr(sht.Pages[k].controls[j].Tag),3,1)) = '' then
                                cmb.Name :=  '_' + TwwDBLookupCombo(sht.Pages[k].controls[j]).DataField
                        else
                                cmb.Name :=  '_' + '_' + GetComponentFieldName(sht.Pages[k].controls[j],StrToInt(copy(intToStr(sht.Pages[k].controls[j].Tag),3,1)),true);
                             
                        cmb.Font :=  TwwDBLookupCombo(sht.Pages[k].controls[j]).Font;
                        cmb.Top :=  TwwDBLookupCombo(sht.Pages[k].controls[j]).Top;
                        cmb.Left :=  TwwDBLookupCombo(sht.Pages[k].controls[j]).Left;
                        cmb.Width := TwwDBLookupCombo(sht.Pages[k].controls[j]).Width;
                        cmb.Height := TwwDBLookupCombo(sht.Pages[k].controls[j]).Height;
                        cmb.LookupTable   := TwwDBLookupCombo(sht.Pages[k].controls[j]).LookupTable;
                        cmb.LookupField    := TwwDBLookupCombo(sht.Pages[k].controls[j]).LookupField;
                        //cmb.KeyField    := TwwDBLookupCombo(sht.Pages[k].controls[j]).KeyField;
                        cmb.Selected := TwwDBLookupCombo(sht.Pages[k].controls[j]).Selected;
                        cmb.Height := TwwDBLookupCombo(sht.Pages[k].controls[j]).Height;
                        cmb.Options := TwwDBLookupCombo(sht.Pages[k].controls[j]).Options;
                        cmb.Visible := True;
                        cmb.PopupMenu := TPopupMenu(form.FindComponent('PopupMenu1'));
                        cmb.Tag := TwwDBLookupCombo(sht.Pages[k].controls[j]).Tag;
                        cmb.Text := '';
//                        SetLabel(form, sht.Pages[k], tab, cmb.name);
                        if Trim(copy(intToStr(sht.Pages[k].controls[j].Tag),3,1)) = '' then
                           SetLabel(form, sht.Pages[k], tab, TwwDBLookupCombo(sht.Pages[k].controls[j]).DataSource.DataSet.FieldByName(TwwDBLookupCombo(sht.Pages[k].controls[j]).DataField).DisplayLabel )
                        else
                           SetLabel(form, sht.Pages[k], tab, GetComponentFieldName(sht.Pages[k].controls[j],StrToInt(copy(intToStr(sht.Pages[k].controls[j].Tag),3,1))));

//                        CreateSQLLabels(form, tab, cmb.name, cmb.Top, cmb.Left, cmb.width, cmb.height);

                   end;
                   3:
                   Begin

                        dtm := TwwDBDateTimePicker.Create(form);
                        tab.InsertControl(dtm);
                        dtm.Name :=  '_' + TwwDBDateTimePicker(sht.Pages[k].controls[j]).DataField;;
                        dtm.Font :=  TwwDBDateTimePicker(sht.Pages[k].controls[j]).Font;
                        dtm.Top :=  TwwDBDateTimePicker(sht.Pages[k].controls[j]).Top;
                        dtm.Left :=  TwwDBDateTimePicker(sht.Pages[k].controls[j]).Left;
                        dtm.Width := TwwDBDateTimePicker(sht.Pages[k].controls[j]).Width;
                        dtm.Height := TwwDBDateTimePicker(sht.Pages[k].controls[j]).Height;
                        dtm.Height := TwwDBDateTimePicker(sht.Pages[k].controls[j]).Height;
                        dtm.Visible := True;
                        dtm.Tag := TwwDBDateTimePicker(sht.Pages[k].controls[j]).Tag;
                        dtm.Text := '';

                        if Trim(copy(intToStr(sht.Pages[k].controls[j].Tag),3,1)) = '' then
                           SetLabel(form, sht.Pages[k], tab, TwwDBDateTimePicker(sht.Pages[k].controls[j]).DataSource.DataSet.FieldByName(TwwDBDateTimePicker(sht.Pages[k].controls[j]).DataField).DisplayLabel )
                        else
                           SetLabel(form, sht.Pages[k], tab, GetComponentFieldName(sht.Pages[k].controls[j],StrToInt(copy(intToStr(sht.Pages[k].controls[j].Tag),3,1))));

                       // SetLabel(form, sht.Pages[k], tab, dtm.Name);
                     
                       // CreateSQLLabels(form, tab, dtm.name, dtm.Top, dtm.Left, dtm.width, dtm.height);
                     
                   end;
                   4:
                   Begin
                 
                        mem := TMemo.Create(form);
                        mem.Name :=  '_' + TDBMemo(sht.Pages[k].controls[j]).DataField;;
                        mem.Font :=  TDBMemo(sht.Pages[k].controls[j]).Font;
                        mem.Top :=  TDBMemo(sht.Pages[k].controls[j]).Top;
                        mem.Left :=  TDBMemo(sht.Pages[k].controls[j]).Left;
                        mem.Width := TDBMemo(sht.Pages[k].controls[j]).Width;
                        mem.Height := TDBMemo(sht.Pages[k].controls[j]).Height;
                        mem.Visible := True;
                        mem.Tag := TDBMemo(sht.Pages[k].controls[j]).Tag;
                        tab.InsertControl(mem);
                        mem.Text := '';
//                        TDBEdit(sht.Pages[k].controls[j]).DataSource.DataSet.FieldByName(TDBEdit(sht.Pages[k].controls[j]).DataField).DisplayName;
                        mem.PopupMenu := TPopupMenu(form.FindComponent('PopupMenu1'));
                        SetLabel(form, sht.Pages[k], tab, TDBMemo(sht.Pages[k].controls[j]).DataSource.DataSet.FieldByName(TDBMemo(sht.Pages[k].controls[j]).DataField).DisplayLabel );
                        //SetLabel(form, sht.Pages[k], tab, txt.Name );
                       // CreateSQLLabels(form, tab, txt.name, txt.Top, txt.Left, txt.width, txt.height);

                   end;
                  5:
                   Begin
                        chk := TCheckbox.Create(form);
                        chk.Name :=  '_' + TDBCheckbox(sht.Pages[k].controls[j]).DataField;
                        chk.Font :=  TDBCheckbox(sht.Pages[k].controls[j]).Font;
                        chk.Top :=  TDBCheckbox(sht.Pages[k].controls[j]).Top;
                        chk.Left :=  TDBCheckbox(sht.Pages[k].controls[j]).Left;
                        chk.Width := TDBCheckbox(sht.Pages[k].controls[j]).Width;
                        chk.Height := TDBCheckbox(sht.Pages[k].controls[j]).Height;
                        chk.Visible := True;
                        chk.Tag := TDBCheckbox(sht.Pages[k].controls[j]).Tag;
                        tab.InsertControl(chk);
                        chk.Enabled := True;
                        chk.Checked := False;
                        chk.Alignment := TDBCheckbox(sht.Pages[k].controls[j]).Alignment;
//                        TDBEdit(sht.Pages[k].controls[j]).DataSource.DataSet.FieldByName(TDBEdit(sht.Pages[k].controls[j]).DataField).DisplayName;
                        chk.PopupMenu := TPopupMenu(form.FindComponent('PopupMenu1'));
                        if TDBCheckbox(sht.Pages[k].controls[j]).Caption <> '' then
                        Begin
                             chk.Caption := TDBCheckbox(sht.Pages[k].controls[j]).caption;
                        end else
                        Begin
                                chk.Caption := '';

                                if Trim(copy(intToStr(sht.Pages[k].controls[j].Tag),3,1)) = '' then
                                   SetLabel(form, sht.Pages[k], tab, TDBCheckBox(sht.Pages[k].controls[j]).DataSource.DataSet.FieldByName(TDBCheckBox(sht.Pages[k].controls[j]).DataField).DisplayLabel )
                                else
                                   SetLabel(form, sht.Pages[k], tab, GetComponentFieldName(sht.Pages[k].controls[j],StrToInt(copy(intToStr(sht.Pages[k].controls[j].Tag),3,1))));

                        end;

                       // SetLabel(form, tab, chk.Name);
                       // CreateSQLLabels(form, tab, chk.name, chk.Top, chk.Left, chk.width, chk.height);
                     
                   end;  
                 end;
              end;
        end;
     end;

end;

No comments:

Post a Comment

Postgress - Read a XML file from a postgress table XML column

SELECT xmltable.* FROM xmldata, XMLTABLE('//ROWS/ROW' PASSING data COLUMNS id int PATH ...