Saturday, June 6, 2020

Drag, Drop and Restrict

I have just read a problem inside foxite where when a user accidentally moves the mouse beyond the form boundaries, then drag and drop fails because the objects disappear on the areas beyond the form. 

The solution to that is to restrict the drag and drop movements within your form, or dimension within objects in your form.  Here are two samples showing how to achieve that:

Sample 1:

* Restricting drag and drop within the form

Local oForm As Form
oForm = Createobject('TestForm')
oForm.Show(1)
Return

Define Class TestForm As Form
      AutoCenter = .T.
      Width = 900
      Height = 440
      Caption = 'Drag, Drop & Restrict Inside Form'
      Add Object container1 As Mycontainer With Top = 30, Left = 50
Enddefine

Define Class Mycontainer As Container
      Height = 100
      Width = 100
      Procedure MouseMove
      Lparameters nButton, nShift, nXCoord, nYCoord
      If m.nButton = 1 And Between(m.nYCoord,0,Thisform.Height-This.Height) And ;
                  BETWEEN(m.nXCoord,0,Thisform.Width-This.Width)
            This.Move(m.nXCoord, m.nYCoord)
      Endif
      Endproc
Enddefine

Sample 2:

* Restricing within objects on form, in this case above or below the lines
Local oForm As Form
oForm = Createobject('TestForm')
oForm.Show(1)
Return

Define Class TestForm As Form
      AutoCenter = .T.
      Width = 900
      Height = 440
      Caption = 'Drag, Drop & Restrict'
      Add Object Shape1 As shape With Top = 30, Left = 0, Width = 900, height = 1
      Add Object Shape2 As shape With Top = 200, Left = 0, Width = 900, height = 1
      Add Object Command1 As MyButton With Caption='Move Me outside of the lines', Top = 35, Left = 5, width = 200, height = 30
Enddefine

Define Class MyButton As CommandButton
      Procedure MouseMove
            Lparameters nButton, nShift, nXCoord, nYCoord
            If m.nButton = 1 AND BETWEEN(m.nYCoord,30,171)
                  This.Move(m.nXCoord, m.nYCoord)
                  WAIT WINDOW m.nYCoord nowait
            Endif
      Endproc
Enddefine

In case you need it.  Cheers!

No comments:

Post a Comment