如何移动在运行时创建的 PictureBox?

How to move a PictureBox created at runtime?

提问人:Isaac Tenorio Londoño 提问时间:11/3/2023 最后编辑:LarsTechIsaac Tenorio Londoño 更新时间:11/3/2023 访问量:51

问:

我正在开发一个应用程序,VB.NET 在该应用程序中创建 PictureBox 的副本并在运行时将其添加到面板中。我希望能够在将 PictureBox 拖放到面板上后用鼠标移动此副本。我尝试处理 PictureBox 副本的 MouseDown、MouseMove 和 MouseUp 事件,但它似乎不起作用。这是我正在使用的代码(当我拖动 picturebox1 并创建新代码时):

`Public Sub NewPictureBoxWhenIsDraggedOriginal()
pictureBoxCopy = New PictureBox()
pictureBoxCopy.Image = PictureBox1.Image
pictureBoxCopy.SizeMode = PictureBoxSizeMode.StretchImage
pictureBoxCopy.Size = PictureBox1.Size
pictureBoxCopy.Location = PictureBox1.Location
Panel2.Controls.Add(pictureBoxCopy)
AddHandler pictureBoxCopy.MouseDown, AddressOf Me.PictureBoxCopy_MouseDown
AddHandler pictureBoxCopy.MouseMove, AddressOf Me.PictureBoxCopy_MouseMove
AddHandler pictureBoxCopy.MouseUp, AddressOf Me.PictureBoxCopy_MouseUp
Private Sub PictureBoxCopy_MouseDown(sender As Object, e As MouseEventArgs)
' Set isDragging to True to indicate that the PictureBox is being dragged
isDragging = True


dragOffset = New Point(e.X, e.Y)
End Sub

Private Sub PictureBoxCopy_MouseMove(sender As Object, e As MouseEventArgs)
' Check if the PictureBox is being dragged
If isDragging Then
    ' Update the PictureBox location to follow the mouse cursor
    Dim newLocation As Point = pictureBoxCopy.Location
    newLocation.X += e.X - dragOffset.X
    newLocation.Y += e.Y - dragOffset.Y
    pictureBoxCopy.Location = newLocation
End If
End Sub

Private Sub PictureBoxCopy_MouseUp(sender As Object, e As MouseEventArgs)
' Set isDragging to False to indicate that the PictureBox has been dropped
isDragging = False
End Sub`    

谁能帮助我理解为什么我无法移动 PictureBox 副本以及如何修复它?任何帮助将不胜感激。谢谢。

伙计们,我尝试在运行时添加处理程序,但它不起作用,好吧,首先有一个原始的图片框,(PictureBox1),当我尝试拖动它时,副本是我可以拖放(和拖放)面板(Panel2),但是当我放下时,我无法再移动副本,我尝试添加处理程序,但我不起作用,因为, (我认为处理程序在潜艇上运行,那是因为不起作用),那么我不知道如何使对象(未声明)可以在面板区域移动,因为我不能在代码上说当鼠标移动时未在 VS Designer 上声明的对象......

vb.net winforms

评论

0赞 jmcilhinney 11/3/2023
请阅读本文并相应地更新您的问题。提供我们需要的一切,没有我们没有的。如果您需要创建一个仅具有相关功能的新项目,请执行此操作。

答:

0赞 Nick Abbot 11/3/2023 #1

您将 PictureBoxCopy 位置设置为与源位置相同,它位于原始 Picturebox 的顶部。无论如何,您都要将其放入面板中,不要设置位置。这是我有效的代码。

Dim isDragging As Boolean
Dim dragOffset As Point

Dim pictureBoxCopy As New PictureBox()

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    NewPictureBoxWhenIsDraggedOriginal()
End Sub

Public Sub NewPictureBoxWhenIsDraggedOriginal()

    pictureBoxCopy.Image = PictureBox1.Image
    pictureBoxCopy.SizeMode = PictureBoxSizeMode.StretchImage
    pictureBoxCopy.Size = PictureBox1.Size

    'pictureBoxCopy.Location = PictureBox1.Location

    Panel2.Controls.Add(pictureBoxCopy)

    AddHandler pictureBoxCopy.MouseDown, AddressOf Me.pictureBoxCopy_MouseDown
    AddHandler pictureBoxCopy.MouseMove, AddressOf Me.pictureBoxCopy_MouseMove
    AddHandler pictureBoxCopy.MouseUp, AddressOf Me.pictureBoxCopy_MouseUp

End Sub

Private Sub pictureBoxCopy_MouseDown(sender As Object, e As MouseEventArgs)
    ' Set isDragging to True to indicate that the PictureBox is being dragged
    isDragging = True


    dragOffset = New Point(e.X, e.Y)
End Sub


Private Sub pictureBoxCopy_MouseMove(sender As Object, e As MouseEventArgs)
    ' Check if the PictureBox is being dragged
    If isDragging Then
        ' Update the PictureBox location to follow the mouse cursor
        Dim newLocation As Point = pictureBoxCopy.Location
        newLocation.X += e.X - dragOffset.X
        newLocation.Y += e.Y - dragOffset.Y
        pictureBoxCopy.Location = newLocation
    End If
End Sub

Private Sub pictureBoxCopy_MouseUp(sender As Object, e As MouseEventArgs)
    ' Set isDragging to False to indicate that the PictureBox has been dropped
    isDragging = False
End Sub