Drag and Drop Issue Between Panel and GroupBox - vb.net

So I have a Panel and a GroupBox. I coded it so I can drag a picture from a Panel to a GroupBox and lay it there. The problem is that I can only add 1 picture to the GroupBox, I want to add as many as I want and also resize the picture when I lay it in the GroupBox so it be smaller than it is in the Panel.
Private Sub Panel1_MouseDown(sender As Object, e As MouseEventArgs) Handles Panel1.MouseDown
Panel1.DoDragDrop(Panel1.BackgroundImage, DragDropEffects.Copy)
End Sub
Private Sub view1_DragEnter(sender As Object, e As DragEventArgs) Handles view1.DragEnter
If (e.Data.GetDataPresent(DataFormats.Bitmap)) Then
e.Effect = DragDropEffects.Copy
End If
End Sub
Private Sub view1_DragDrop(sender As Object, e As DragEventArgs) Handles view1.DragDrop
Dim bm As Bitmap = DirectCast(e.Data.GetData(DataFormats.Bitmap), Bitmap)
view1.BackgroundImage = bm
End Sub

Related

How to make Drag and Drop Objects move in real time, with border etc in VS 2012

I've created a simple drag and drop experiment which in principal works fine, moving Label 1 over Label 2 and changing the text
However, you dont see the actual label move, you get an arrow with a plus sign when the mouse arrives over Label 2. Is there anyway to physically pick up the object and see it actually moving.
Public Class Form1
Private Sub Label2_DragDrop(sender As Object, e As DragEventArgs) Handles Label2.DragDrop
Label2.Text = e.Data.GetData(DataFormats.Text).ToString
End Sub
Private Sub Label2_DragEnter(sender As Object, e As DragEventArgs) Handles Label2.DragEnter
e.Effect = DragDropEffects.Copy
End Sub
Private Sub Label1_MouseDown(sender As Object, e As MouseEventArgs) Handles Label1.MouseDown
If e.Button = Windows.Forms.MouseButtons.Left Then
Label1.DoDragDrop(Label1.Text, DragDropEffects.Copy Or DragDropEffects.Move)
End If
End Sub
End Class

How to move a picturebox within a panel

I can move a picturebox from a Form to a panel as follows (both PictureBox1 and Panel1 are on the same Form control). But once it is in panel1, PictureBox1 doesn't move with MouseMove event. My intention is, once the picturebox entered the panel area, I want to move it freely (MouseMove) only within the Panel area. Can anyone help me to achieve this?
The code is this:
Dim locate As Point
Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles PictureBox1.MouseDown
locate = New Point(-e.X, -e.Y)
End Sub
Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) Handles PictureBox1.MouseMove
If e.Button = Windows.Forms.MouseButtons.Left Then
Dim MoveTo As Point = Me.PointToClient(MousePosition)
MoveTo.locate(locate.X, locate.Y)
If PictureBox1.Location.X < Panel1.Location.X Then
PictureBox1.Location = MoveTo
End If
End If
End Sub

Loading pictures with code

Private Sub frmPegSolitaire_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For Each pictire As PictureBox In Me.Controls
If Not pictire.Tag.Equals("n") Then
pictire.Image = Image.FromFile("peg.png")
End If
Next
End Sub
Here is my code that does not work. What am I doing wrong?
You're looping over all the Controls contained in Me (probably the Form)
In that collection there is more than just the PictureBoxes so you need to filter to only get those :
(see OfType)
Private Sub frmPegSolitaire_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For Each pictire As PictureBox In Me.Controls.OfType(Of PictureBox)
If Not pictire.Tag.Equals("n") Then
pictire.Image = Image.FromFile("peg.png")
End If
Next
End Sub

Drag and drop TableLayoutPanel

somebody can help me to realise a drag and drop in a TableLayoutPanel please. For a drag and drop normal it's easy but here it isn't cause there is controls which are PictureBox.
I have to slide an image from the TableLayoutPanle_main to a PictureBox in TableLayoutPanle_plateau
Here's what i have try :
Private Sub TableLayoutPanel_main_MouseMove(sender As Object, e As MouseEventArgs) Handles TableLayoutPanel_main.MouseMove
Dim rep As DragDropEffects
If e.Button = Windows.Forms.MouseButtons.Left Then
rep = sender.DoDragDrop(sender.Controls, DragDropEffects.Move)
End If
End Sub
Private Sub TableLayoutPanel_plateau_DragEnter(sender As Object, e As DragEventArgs) Handles TableLayoutPanel_plateau.DragEnter
If e.Data.GetDataPresent(DataFormats.Bitmap) Then
e.Effect = DragDropEffects.Move
Else
e.Effect = DragDropEffects.None
End If
End Sub
Private Sub TableLayoutPanel_plateau_DragDrop(sender As Object, e As DragEventArgs) Handles TableLayoutPanel_plateau.DragDrop
If TypeOf sender.Controls Is PictureBox Then
Dim pb As PictureBox = CType(sender.Controls, PictureBox)
pb.Image = e.Data.GetData(DataFormats.Bitmap)
End If
End Sub

VB.Net make draggable controls not go outside the form

I have a custom control that is draggable using the code below, but the problem is that i can move the control outside the form.
Private Sub ObjCan_MouseMove(sender As Object, e As MouseEventArgs) Handles Me.MouseMove
If e.Button = Windows.Forms.MouseButtons.Left Then
Me.Location = New Point(FormGame.PointToClient(MousePosition).X - 16, FormGame.PointToClient(MousePosition).Y - 16)
End If
End Sub
Set Cursor.Clip to limit mouse movement:
Gets or sets the bounds that represent the clipping rectangle for the
cursor. A clipped cursor is allowed to move only within its clipping
rectangle.
Here's a quick example with a Label. Note that if you moved the Label into a different container, like a Panel, the code would still work and the Label would be confined to the Panel bounds instead:
Public Class Form1
Private PrevClip As Rectangle
Private Sub Label1_MouseDown(sender As Object, e As MouseEventArgs) Handles Label1.MouseDown
PrevClip = Cursor.Clip
Dim ctl As Control = DirectCast(sender, Control)
Dim ctlContainer As Control = ctl.Parent
Cursor.Clip = ctlContainer.RectangleToScreen(New Rectangle(0, 0, ctlContainer.ClientSize.Width - ctl.Width, ctlContainer.ClientSize.Height - ctl.Height))
End Sub
Private Sub Label1_MouseMove(sender As Object, e As MouseEventArgs) Handles Label1.MouseMove
If e.Button = MouseButtons.Left Then
Dim ctl As Control = DirectCast(sender, Control)
Dim ctlContainer As Control = ctl.Parent
ctl.Location = ctlContainer.PointToClient(Cursor.Position)
End If
End Sub
Private Sub Label1_MouseUp(sender As Object, e As MouseEventArgs) Handles Label1.MouseUp
Cursor.Clip = PrevClip
End Sub
End Class