Move a panel with the sylus... But there're some problems...

sitajony

Active Member
Licensed User
Hi, I know it's a newbie question, in JS it's easy but apparently there're some problem here:
I added 3 events: MouseDown, MouseMove and MouseUp.
When we MouseDown 2 variables are modified: posx and controlx.
When we move the panel the Left propriete is = to Controlx+x-posx.
It work but apparently the MouseMove is run each time we move the panel so +1=+1+1+1...
The MouseMove doesn't run when POSX=-1 and I tried to set -1 each time we move the panel and after I reset this variable to X but it's the same thing...
Someone understand and could help me?
Thanks for your help!
PS: I can tell you how do this in JS, I did Windows Seven all in JS (Nearly like Win7)
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
This code move a panel named Panel1:
B4X:
Sub Globals
    mouseDown = False
    Dim mouseX, mouseY As Number
End Sub

Sub App_Start
    Form1.Show
End Sub


Sub Panel1_MouseDown (x,y)
    mouseDown = True
    mouseX = x
    mouseY = y
End Sub

Sub Panel1_MouseMove (x,y)
    If Not(mouseDown) Then Return
    panel1.Left = panel1.Left + x - mouseX
    Panel1.Top = panel1.Top + y - mouseY
End Sub

Sub Panel1_MouseUp (x,y)
    mouseDown = False
End Sub
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
This code adds a "snap to grid" effect:
B4X:
Sub Globals
    mouseDown = False
    Dim mouseX, mouseY, gridSize As Number
    gridSize = 15
End Sub

Sub App_Start
    Form1.Show
End Sub


Sub Panel1_MouseDown (x,y)
    mouseDown = True
    mouseX = x
    mouseY = y
End Sub

Sub Panel1_MouseMove (x,y)
    If Not(mouseDown) Then Return
    Dim dx, dy As Number
    dx = x - mouseX - ((x - mouseX) Mod gridSize)
    dy = y - mouseY - ((y - mouseY) Mod gridSize)
    panel1.Left = panel1.Left + dx
    Panel1.Top = panel1.Top + dy
End Sub

Sub Panel1_MouseUp (x,y)
    mouseDown = False
End Sub
 

sitajony

Active Member
Licensed User
Thanks for your code, I use this and it doesn't work:
B4X:
Sub Globals
    startx=-1
    starty=-1
End Sub
Sub AppStart
    Form1.Show
End Sub
Sub Panel1_MouseDown (x,y)
    startx=x
    starty=y
End Sub
Sub Panel1_MouseMove (x,y)
    if startx=-1 Then Return
    Panel1.Left = Panel1.Left + x - startx
    Panel1.Top = Panel1.Top + y - starty
End Sub
Sub Panel1_MouseUp (x,y)
    startx=-1
    starty=-1
End Sub
And apparently it's nearly the same code so I don't know if it work... I try...
 

sitajony

Active Member
Licensed User
It works better but there's again a problem, sometime the panel go to right, left, right left etc...
It's weird... Maybe something is wrong on my code elsewhere...
But it's better than before Thanks
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…