Issue with MouseUp Event Not Triggering in Xojo Project

I am currently working on a project where I need to implement the functionality of dragging an image within a canvas using the mouse. However, I have encountered an issue where the MouseUp event is not triggering when I release the mouse button after dragging.

Here is the relevant code snippet for handling the MouseUp event in my canvas:

// 定义变量来跟踪鼠标位置和图片位置
Dim mouseX As Integer
Dim mouseY As Integer
Dim imageX As Integer
Dim imageY As Integer
Dim dragging As Boolean

// 鼠标按下事件
Sub MouseDown(X As Integer, Y As Integer) Handles MouseDown
    // 检查鼠标是否在图片上
    If X >= imageX And X <= imageX + Image.Width And Y >= imageY And Y <= imageY + Image.Height Then
        // 设置拖动标志和偏移量
        dragging = True
        mouseX = X - imageX
        mouseY = Y - imageY
    End If
End Sub

// 鼠标移动事件
Sub MouseMove(X As Integer, Y As Integer) Handles MouseMove
    // 检查是否正在拖动图片
    If dragging Then
        // 更新图片位置
        imageX = X - mouseX
        imageY = Y - mouseY
        
        // 重新绘制Canvas
        Refresh
    End If
End Sub

// 鼠标拖动事件
Sub MouseDrag(X As Integer, Y As Integer) Handles MouseDrag
    // 将鼠标移动事件的逻辑复用在此处
    MouseMove(X, Y)
End Sub

// 鼠标释放事件
Sub MouseUp(X As Integer, Y As Integer) Handles MouseUp
    // 停止拖动
    dragging = False
    MsgBox("MouseUp!!!!!!")
End Sub

// 绘制事件
Sub Paint(g As Graphics) Handles Paint
    // 在Canvas上绘制图片
    g.DrawPicture(imgCanvas, imageX, imageY)
End Sub

I have already implemented the MouseDown and MouseDrag events, which are working correctly. The MouseDown event allows me to start dragging the image, and the MouseDrag event updates the image’s position as expected. However, when I release the mouse button, the MouseUp event does not respond.

I have reviewed my code and ensured that the MouseUp event is correctly assigned to the canvas control. I have also verified that there are no conflicting event handlers or code that could interfere with the MouseUp event.

I would greatly appreciate any insights or suggestions from the Xojo community on how to resolve this issue and make the MouseUp event respond properly when releasing the mouse button after dragging.

Thank you for your assistance!

have you used the boolean return in mouse down?
and canvas option “allow focus” should be on.

1 Like

Yes, you have to add:

Return true

in the Canvas‘ MLouseDown Event to activate the MouseUp Event…

1 Like

@MarkusR @Emile_Schwarz Thank you for your assistance. After adding the return value, the MouseUp event is now being triggered. However, I have encountered an issue where the image is no longer draggable. Could you please provide further assistance on this matter? I would greatly appreciate it. Thank you!

Did you set (in the DesktopCanvas OpenEvent):

Me.AcceptFileDrop() ?

More there (I think):
https://documentation.xojo.com/api/user_interface/desktop/desktopcanvas.html#desktopcanvas-acceptfiledrop

1 Like

I would like to drag an image within a canvas, and I have found a solution. Thank you for your assistance. Here is my code:

Sub MouseDrag(x As Integer, y As Integer)
  If X >= imageX And X <= imageX + imgWidth And Y >= imageY And Y <= imageY + imgHeight Then
    dragging = True
    imageX = X - mouseX
    imageY = Y - mouseY
    Refresh
  End If
End Sub

Thank you once again for your help.

What’s still weird is, if you didn’t put “Return true” with your initial problem, the MouseDrag shouldn’t have worked either, and you said it did. :man_shrugging:

I have already returned True in MouseDown, as mentioned in my previous response.