B
booboo_US
Guest
Hi Everyone:
I am using VB.net 2019. I have several pictureboxes, with arrows in them (up arrow, left arrow, etc.). Now, I want the user to click on a picturebox, and drag it to picturebox10 (the target). This is similar to a simple drag and drop, which I know how to do as there were many examples on the internet. However, I do not want to do an exact drag and drop. Here is what I want.
1- When the user drops (let go of the mouse), record the coordinates (x, y) of where it was dropped
2- Erase arrows 1 and 2 (if they exist), and not the underlying picture
3- At the coordinates (x, y), draw a new arrow
my biggest problem is I do not know how to erase the arrows without erasing the underlying picture, and also, I do not know in which event procedure I have to put the drawing of the new arrow. I tried it in the Paint event, but in this event, it always draws the arrow, no matter what.
I would appreciate the code for this.
Imports System.Drawing
Imports System.Drawing.Drawing2D
Public Class Form1
Private Sub Form1_Load(ByVal sender As Object,
ByVal e As System.EventArgs) Handles MyBase.Load
Me.AllowDrop = True
PictureBox2.AllowDrop = True
End Sub
Private Sub PictureBox2_DragDrop(ByVal sender As Object,
ByVal e As System.Windows.Forms.DragEventArgs) Handles PictureBox2.DragDrop
PictureBox2.Image = e.Data.GetData(DataFormats.Bitmap)
End Sub
Private Sub PictureBox2_DragEnter(ByVal sender As Object,
ByVal e As System.Windows.Forms.DragEventArgs) Handles PictureBox2.DragEnter
' See if the data includes a Bitmap.
If e.Data.GetDataPresent(GetType(Bitmap)) Then
' There is Bitmap data. Allow copy.
e.Effect = DragDropEffects.Copy
Else
' There is no Bitmap. Prohibit drop.
e.Effect = DragDropEffects.None
End If
End Sub
Private Sub PictureBox1_MouseDown(ByVal sender As Object,
ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
PictureBox1.DoDragDrop(PictureBox1.Image, DragDropEffects.Copy)
'PictureBox2.DoDragDrop(sender, DragDropEffects.Copy)
End Sub
Private Sub PictureBox3_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox3.Paint
Dim pen As New Pen(Color.FromArgb(255, 0, 0, 255), 8)
pen.StartCap = System.Drawing.Drawing2D.LineCap.ArrowAnchor
pen.EndCap = System.Drawing.Drawing2D.LineCap.RoundAnchor
e.Graphics.DrawLine(pen, 20, 175, 300, 175)
End Sub
End Class
Continue reading...
I am using VB.net 2019. I have several pictureboxes, with arrows in them (up arrow, left arrow, etc.). Now, I want the user to click on a picturebox, and drag it to picturebox10 (the target). This is similar to a simple drag and drop, which I know how to do as there were many examples on the internet. However, I do not want to do an exact drag and drop. Here is what I want.
1- When the user drops (let go of the mouse), record the coordinates (x, y) of where it was dropped
2- Erase arrows 1 and 2 (if they exist), and not the underlying picture
3- At the coordinates (x, y), draw a new arrow
my biggest problem is I do not know how to erase the arrows without erasing the underlying picture, and also, I do not know in which event procedure I have to put the drawing of the new arrow. I tried it in the Paint event, but in this event, it always draws the arrow, no matter what.
I would appreciate the code for this.
Imports System.Drawing
Imports System.Drawing.Drawing2D
Public Class Form1
Private Sub Form1_Load(ByVal sender As Object,
ByVal e As System.EventArgs) Handles MyBase.Load
Me.AllowDrop = True
PictureBox2.AllowDrop = True
End Sub
Private Sub PictureBox2_DragDrop(ByVal sender As Object,
ByVal e As System.Windows.Forms.DragEventArgs) Handles PictureBox2.DragDrop
PictureBox2.Image = e.Data.GetData(DataFormats.Bitmap)
End Sub
Private Sub PictureBox2_DragEnter(ByVal sender As Object,
ByVal e As System.Windows.Forms.DragEventArgs) Handles PictureBox2.DragEnter
' See if the data includes a Bitmap.
If e.Data.GetDataPresent(GetType(Bitmap)) Then
' There is Bitmap data. Allow copy.
e.Effect = DragDropEffects.Copy
Else
' There is no Bitmap. Prohibit drop.
e.Effect = DragDropEffects.None
End If
End Sub
Private Sub PictureBox1_MouseDown(ByVal sender As Object,
ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
PictureBox1.DoDragDrop(PictureBox1.Image, DragDropEffects.Copy)
'PictureBox2.DoDragDrop(sender, DragDropEffects.Copy)
End Sub
Private Sub PictureBox3_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox3.Paint
Dim pen As New Pen(Color.FromArgb(255, 0, 0, 255), 8)
pen.StartCap = System.Drawing.Drawing2D.LineCap.ArrowAnchor
pen.EndCap = System.Drawing.Drawing2D.LineCap.RoundAnchor
e.Graphics.DrawLine(pen, 20, 175, 300, 175)
End Sub
End Class
Continue reading...