Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
How do I programatically add objects to a PageFrame?
Message
From
27/07/2000 20:59:03
 
 
To
27/07/2000 19:53:26
General information
Forum:
Visual FoxPro
Category:
Forms & Form designer
Miscellaneous
Thread ID:
00397937
Message ID:
00397960
Views:
17
Sorry... accidentally hit send before I finished..
This is basic, it doesn't add horizontally, and the alignments are a little off, but it does work.
Create and switch to a directory for this, and copy this code into a PRG called "AddRep".
** AddRep.prg code
IF !FILE("reports.dbc")
  CREATE DATA REPORTS
  CREATE TABLE 'REPORT_MAIN.DBF' NAME 'report_main' ( ;
    CKEY                 C(10) NOT NULL, ;
    CFILE                C(20) NOT NULL ;
    )

  INDEX ON CKEY TAG CKEY

  CREATE TABLE 'REPORT_CONTROLS.DBF' NAME 'report_controls' ( ;
    CKEY                 C(10) NOT NULL, ;
    CLABEL               C(10) NOT NULL, ;
    CCAPTION             C(10) NOT NULL, ;
    CCLASS               C(10) NOT NULL, ;
    CCLASSLIB            C(10) NOT NULL, ;
    CNAME                C(10) NOT NULL ;
    )

  ***** Create each index for report_controls *****
  SET COLLATE TO 'MACHINE'
  INDEX ON CKEY TAG CKEY

  m.cKey = SYS(2015)
  m.cFile = "myreport.frx"
  INSERT INTO report_main FROM MEMVAR

  FOR i = 1 TO 5
    lcNum = ALLTRIM(STR(i))
    m.clabel = "Label" + lcNum
    m.ccaption = "Caption" + lcNum
    DO CASE
    CASE INLIST(i,1,3,5)
      m.cclass = "textbox"
      m.cname = "Text" + lcNum
    CASE i = 2
      m.cclass = "combobox"
      m.cname = "Combo" + lcNum
    CASE i = 4
      m.cclass = "checkbox"
      m.cname = "Check1"
      m.clabel = ""
    ENDCASE
    INSERT INTO report_controls FROM MEMVAR
  ENDFOR
  CLOSE DATA ALL
ENDIF

*---------------
FUNCTION AddRepTest()
  *---------------
  * create the form PUBLIC so it will persist
  PUBLIC goForm
  goForm = CREATEOBJECT("addrepform")
  goForm.Show()
  USE report_main IN 0
  USE report_controls IN 0
  AddReportControls(goForm, report_main.ckey)
  CLOSE DATA ALL
ENDFUNC

*----------------------
FUNCTION AddReportControls()
  *----------------------
  LPARAMETERS toObject, tcReportKey
  LOCAL lcCaption, lcClass, lcClassLib, lcLabelName, lcName, lcReportFile, lnLabelLeft, ;
    lnLabelTop, lnLeft, lnTop, loLastControl, loLastLabel
  STORE "" To lcCaption, lcClass, lcClassLib, lcLabelName, lcName, lcReportFile
  STORE 0 To lnLabelLeft, lnLabelTop, lnLeft, lnTop
  STORE .NULL. TO loLastLabel, loLastControl

  *-- these won't change in this code
  lnLabelLeft = 5
  lnLeft = 102 && label width will be 100

  * report_main table is open
  =SEEK(tcReportKey, "report_main", "ckey")
  lcReportFile = report_main.cfile
  SELECT report_controls
  SET ORDER TO ckey
  SET KEY TO tcReportKey
  SCAN
    lcLabelName = ALLTRIM(clabel)
    lcCaption = ALLTRIM(ccaption)

    lcClass = ALLTRIM(cclass)
    lcClassLib = ALLTRIM(cclasslib)
    lcName = ALLTRIM(cname)

    IF !EMPTY(lcLabelName)
      *-- add a label
      toObject.AddObject(lcLabelName, "label")
      ** helps with aligning objects to not have AutoSize = .T.
      toObject.&lcLabelName..Width = 100
      toObject.&lcLabelName..Caption = lcCaption
    ENDIF

    IF !EMPTY(lcClassLib)
      * add custom class
      * not used in this example...
      toObject.NewObject(lcName, lcClass, lcClassLib)
    ELSE
      * add base class
      toObject.AddObject(lcName, lcClass)
    ENDIF

    IF PEMSTATUS(toObject.&lcName, "Caption",5)
      *-- put caption in control
      toObject.&lcName..Caption = lcCaption
    ENDIF

    *-- position the new label and control relative to the last control added
    *	get position
    IF ISNULL(loLastControl)
      lnTop = 3
      lnLabelTop = 5
    ELSE
      lnTop = loLastControl.Top + loLastControl.Height + 5
      lnLabelTop = loLastControl.Top + loLastControl.Height + 3
    ENDIF
    *	set position
    IF !EMPTY(lcLabelName)
      toObject.&lcLabelName..Top = lnLabelTop
      toObject.&lcLabelName..Left = lnLabelLeft
      loLastLabel = toObject.&lcLabelName
    ENDIF

    toObject.&lcName..Top = lnTop
    toObject.&lcName..Left = lnLeft
    loLastControl = toObject.&lcName

  ENDSCAN

  *--	everything added will be hidden, so
  toObject.SetAll("Visible", .T.)
ENDFUNC

*--------------------
* test form class
*--------------------
DEFINE CLASS addrepform AS FORM
  AutoCenter = .T.

  ADD OBJECT cmdClose AS commandbutton ;
    WITH Caption = "Close", ;
    Top = 214, ;
    Left = 145, ;
    Height = 25

  PROCEDURE cmdClose.Click
    thisform.Release()
  ENDPROC
ENDDEFINE
In the command window enter the following DO commands
The first will create data and populate it.
The second will create a form and dynamically add controls to it.
DO addrep
DO addreptest IN addrep
HTH
Insanity: Doing the same thing over and over and expecting different results.
Previous
Reply
Map
View

Click here to load this message in the networking platform