Thursday, July 19, 2012

Drag, Drop and Retain Positions

Here is another, moving objects around and retrieving positions on next form run.  This is just a simple demonstration of how we can do that:

While the dragging and dropping portion seems to be easy, plus preserving and retrieving those values later, what took me longer to figure out is how the rearranging of these objects will affect the tabindex properties of each object inside the container.  Anyway, this is maybe not perfect yet but at least you will have something you can start with and refine.

Copy-paste the codes to a .prg, run it, tick Allow Moving, then drag buttons around.  After that, close the form and then run it again.  See if it retains the last positions when you close the form and if the tabindex are proper.

Here is the code samples if you are interested:

Local oForm As Form
oForm = Createobject('TestForm')

Define Class TestForm As Form
      AutoCenter = .T.
      Width = 300
      Height = 440
      MinWidth = 200
      MinHeight = 100
      Caption = 'Drag, Drop & Retain Positions'

      Add Object chkMove As Checkbox With Caption = 'Allow Moving of Objects',;
            top = 400, Left = 5, AutoSize = .T., Value = .F., Anchor = 6
      Add Object Command1 As MyButton With Caption='One', Top = 5, Left = 5
      Add Object Command2 As MyButton With Caption='Two', Top = 70, Left = 5
      Add Object Command3 As MyButton With Caption='Three', Top =135, Left = 5
      Add Object Command4 As MyButton With Caption='Four', Top = 200, Left = 5
      Add Object Command5 As MyButton With Caption='Five', Top = 265, Left = 5
      Add Object Command6 As MyButton With Caption='Six', Top = 330, Left = 5

      Procedure Load
            Close Databases All
            * Check if table is there for preserving/restoring values
            If !File('DragDrops.dbf')
                  Create Table dragdrops Free (ObjectName c(40),xTop I,xLeft I,xTabIndex I)
                  Index On xTop+xLeft Tag xTabIndex
                  Use dragdrops Order xTabIndex

      Procedure Init
            * Check if first run or not, if first run, make an entry in the table
            If Reccount() == 0
                  For Each loCtrl In Thisform.Controls FoxObject
                        Insert Into dragdrops Values (loCtrl.Name,loCtrl.Top,loCtrl.Left,0)

      Procedure _TabIndex
            * recreate tab indexes
            Local lnTab
            lnTab = 1
                  Replace xTabIndex With m.lnTab In dragdrops
                  lnTab = m.lnTab + 1

      Procedure _reorder
            * Reorder Tab Index
            For Each loCtrl In Thisform.Controls FoxObject
                  Select dragdrops
                  Locate For Upper(loCtrl.Name) = Upper(ObjectName)
                  loCtrl.TabIndex = dragdrops.xTabIndex


Define Class MyButton As CommandButton
      Height = 60
      Width = 100

      Procedure Init
            * Get previous positions
            Select dragdrops
            Locate For Upper(This.Name) = Upper(ObjectName)
            This.Top = dragdrops.xTop
            This.Left = dragdrops.xLeft

      Procedure MouseMove
            Lparameters nButton, nShift, nXCoord, nYCoord
            If m.nButton = 1 And Thisform.chkMove.Value = .T.
                  This.Move(m.nXCoord, m.nYCoord)
                  * update new coordinates
                  Replace xTop With m.nYCoord, xLeft With m.nXCoord For;
                        Upper(This.Name) = Upper(ObjectName) In dragdrops

      Procedure Click
            Messagebox("You've clicked "+This.Name+Chr(13)+;
                  "Tab Index: "+Transform(This.TabIndex))


No comments:

Post a Comment