Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Opening a form from a grid value
Message
De
24/11/2005 08:00:08
Cetin Basoz
Engineerica Inc.
Izmir, Turquie
 
 
À
24/11/2005 02:41:06
Keith Emerson
Emerson Diversified Technologies Inc.
Maryland Heights, Missouri, États-Unis
Information générale
Forum:
Visual FoxPro
Catégorie:
Codage, syntaxe et commandes
Versions des environnements
Visual FoxPro:
VFP 9
OS:
Windows XP SP2
Database:
Visual FoxPro
Divers
Thread ID:
01071795
Message ID:
01071899
Vues:
22
>Thanks but that had no effect on the operation. If anyone knows of an example please point me to it.

Keith,
Strange it didn't work out for you. I could guarantee 99.9% of members know an example:)
OK lets go step by step:

-Create a test form

modify form testForm

-Go to init and add
* Remember lparameters should be on the first executable line.
lparameters p1,p2,p3
messagebox(m.p1)
messagebox(m.p2)
messagebox(m.p3)

-Save the form and close
-From command window call the form with 0-3 parameters (we have 3 max)
do form testForm
do form testForm with "Hello"
do form testForm with "Hello",1
do form testForm with "Hello",1,date()
-All work, right? OK it shows that form's init gets the parameters
-Next you should remember though init gets the parameters, those parameters are local to init code only (BTW forget Parameters command ever existed. Use Lparameters - not mandatory of course just a recommendation). If any other place needs to use it then you should make it available through some property of form. That is why docs say assign to a form property. To test this part modify your test form again and add a command button with click code:

messagebox(m.p1)

-Save, close and from command window:
do form testForm with "Hello"
-Click the button and you'd get error (assuming there is no public p1 defined). It shows button's click code doesn't have an idea what m.p1 is (it is called "variable not in scope").
-OK go and fix it. Modify the form again and make parameters available to all form elements:
* testForm init code edited
lparameters p1,p2,p3
this.AddProperty('PassedParameter1', m.p1)
this.AddProperty('PassedParameter2', m.p2)
this.AddProperty('PassedParameter3', m.p3)
* button click code edited
* note: assuming you pass simple parameters like string, number, date etc
* otherwise this code causes datatype mismatch (ie: an object)
text to m.lcPassedParameters textmerge noshow
Passed Parameter #1 is: <<thisform.PassedParameter1>>
Passed Parameter #2 is: <<thisform.PassedParameter2>>
Passed Parameter #3 is: <<thisform.PassedParameter3>>
endtext
MessageBox(m.lcPassedParameters)
-Save and test again. Clicking the button now show your passed parameters. right? Great.

Now you learned the basics of passing parameters to a form. Below there is a much more complex version of parameters exchanging between forms. You might not grasp it for the moment but at least just run it to try, save it in a safe place for future reference. It not only shows passing simple parameter passing but passing complex parameters as well plus how you could return (OK return is not the correct term - docs show how to return a value from a form but probably you'd never use that way. Works only for simple and single return parameters) multiple parameters, interact with called and caller form, pass complete cursor contents between data sessions etc etc.
Public oForm
oForm = Createobject('ContactsForm')
oForm.Show

Define Class ContactsForm As Form
  Width = 700
  Height = 400
  DataSession = 2
  Add Object myGrid As Grid With ;
    width=700,Height = 300, DeleteMark=.F.,RecordMark=.F.,SplitBar=.F.
  Add Object someTextBox As TextBox With Top = 310, Width = 200

  Procedure Init
    With This.myGrid
      .RecordSourceType = 4
      .RecordSource = "select * from customer into cursor myCursor"
      For ix=1 To .ColumnCount
        With .Columns(m.ix)
          .AddObject('myTextBox','myTextBox')
          .CurrentControl = 'myTextBox'
          .Sparse = .F.
          .myTextBox.Visible = .T.
        Endwith
      Endfor
      .AddProperty('nCurrec',0)
      .AddProperty('InGrid')
      .SetAll('DynamicBackColor','(Iif(Recno()=this.nCurrec,0x00FFFF,0xFFFFFF))')
    Endwith
  Endproc
  Procedure myGrid.BeforeRowColChange
    Lparameters nColIndex
    Thisform.LockScreen = This.InGrid
  Endproc

  Procedure myGrid.AfterRowColChange
    Lparameters nColIndex
    This.nCurrec = Recno()
    Thisform.LockScreen = .F.
  Endproc

  Procedure myGrid.DblClick
    This.InGrid = .F.
    Thisform.LockScreen = .F.
    Thisform.CallChangeForm()
  Endproc

  Procedure myGrid.When
    This.InGrid = .T.
  Endproc

  Procedure CallChangeForm
    Thisform.LockScreen = .F.
    Local loForm,ix,jx,lcReturnMessage,luSomeCustomProperty,loParamCargo
    * Another parameter exchange tip
    loParamCargo = Createobject('custom') && 'Empty' class in VFP9
    With loParamCargo
      .AddProperty('StartDate',{^1993/1/1})
      .AddProperty('EndDate',{^1993/12/31})
      .AddProperty('ProductList[1]')
      .AddProperty('Products')
      Select product_id ;
        from products ;
        where prod_name = 'C' ;
        into Array .ProductList
    Endwith
    * Another parameter exchange tip

    *DO form ChangeForm name loForm with Customer.Cust_ID, thisform, loParamCargo
    loForm = Createobject('ChangeForm',myCursor.Cust_id,Thisform,loParamCargo)
    loForm.Show
    *

    * Called form is a modal form
    * This part of code 'waits' called form to hide,release
    * to continue. Note that its exit button hide()s the form
    * so it's still there and we can interact
    This.someTextBox.Value = 'Returned from called form'
    lcReturnMessage = loForm.Tag
    luSomeCustomProperty = loForm.SomeCustomProperty
    loParamCargo = loForm.oParamCargo && Get processed one back
    loForm.Release() && Done with it-release
    Release loForm
    Messagebox(m.lcReturnMessage,0,'Called form Tag')
    Messagebox(m.luSomeCustomProperty,0,'Called form SomeCustomProperty')

    * loParamCargo return value
    Local lcText
    With loParamCargo
      Set Textmerge To Memvar m.lcText Noshow
      Set Textmerge On
    \\Product sales to customer between <<.StartDate>> and <<.EndDate>>:
      If .products = 0
    \None.
      Else
        For ix=1 To .products
          For jx=1 To Amembers(aMemberProps,.ProductList[m.ix])
    \<<aMemberProps[m.jx]>>: <<Evaluate('.ProductList[m.ix].'+aMemberProps[m.jx])>>
          Endfor
        Endfor
      Endif
      Set Textmerge To
      Set Textmerge Off
      Messagebox(m.lcText,0,'Called form ParamCargo')
    Endwith
  Endproc
Enddefine

Define Class myTextBox As TextBox
  Margin = 0
  SpecialEffect = 1
  BorderStyle = 0

  Procedure DblClick
    This.Parent.Parent.DblClick()
  Endproc
Enddefine

Define Class ChangeForm As Form
  Top = 200
  Left = 200
  DataSession = 2
  WindowType = 1
  ControlBox=.F.
  Caption = 'Changes will not be saved'
  SomeCustomProperty = Datetime()
  oCaller = Null && reference to caller form

  Add Object txtCompany As TextBox With ;
    top=0, ControlSource='customer.Company'
  Add Object txtContact As TextBox With ;
    top=30, ControlSource='customer.Contact'
  Add Object txtCountry As TextBox With ;
    top=60, ControlSource='customer.Country'
  Add Object exitButton As CommandButton With ;
    top = 100, Caption='Done'
  Add Object toggleButton As CommandButton With ;
    top = 100, Left = 100, Caption='Hide Caller'

  Procedure Load
    Set Multilocks On
    Use Customer

    CursorSetProp("Buffering",5,'Customer')
  Endproc

  Procedure Init
    Lparameters tcCustomerID, toCallerForm, toParamCargo
    This.oCaller = toCallerForm
    Select Customer
    Locate For Upper(Cust_id) = m.tcCustomerID
    This.oCaller.someTextBox.Value = "Got parameters sir."
    If Type('toParamCargo') = 'O'
      This.AddProperty('oParamCargo',toParamCargo)
      This.ProcessParamCargo()
    Endif
  Endproc

  Procedure exitButton.Click
TEXT to thisform.Tag textmerge noshow
Edited customer values:
Customer Id: <<customer.cust_id>>
Company: <<customer.company>>
Contact: <<customer.contact>>
Country: <<customer.country>>
ENDTEXT
    Thisform.oCaller.Visible = .T. && In case toggled
    Thisform.Hide()
  Endproc

  Procedure toggleButton.Click
    With Thisform
      If Type('.oCaller')='O' And ;
          !Isnull(.oCaller) And ;
          Lower(.oCaller.BaseClass) == 'form'
        If This.Caption = 'Hide Caller'
          .oCaller.Visible = .F.
          This.Caption = 'Show Caller'
        Else
          .oCaller.Visible = .T.
          This.Caption = 'Hide Caller'
        Endif
      Endif
    Endwith
  Endproc

  Procedure ProcessParamCargo
    Create Cursor ProductList (productID i)
    Append From Array Thisform.oParamCargo.ProductList
    With Thisform.oParamCargo
      Select ;
        o.Order_id, o.Shipped_on,;
        p.product_id, p.prod_name, oi.Quantity ;
        from orders o ;
        inner Join orditems oi On o.Order_id = oi.Order_id ;
        inner Join products p On oi.product_id = p.product_id ;
        inner Join ProductList pl On Val(p.product_id) = pl.productID ;
        where o.Cust_id == Customer.Cust_id And ;
        o.Shipped_on Between .StartDate And .EndDate ;
        into Cursor crsSold ;
        nofilter
      .products = _Tally
      If .products > 0
        Dimension .ProductList[.Products]
        Scan
          Scatter Name .ProductList[Recno()]
        Endscan
      Endif
    Endwith
  Endproc
Enddefine
Cetin
Çetin Basöz

The way to Go
Flutter - For mobile, web and desktop.
World's most advanced open source relational database.
.Net for foxheads - Blog (main)
FoxSharp - Blog (mirror)
Welcome to FoxyClasses

LinqPad - C#,VB,F#,SQL,eSQL ... scratchpad
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform