Drag-drop in vb.net - vb.net

I am new in visual basic and I followed one article to write this code to drag and drop images.
But I want to add an if statement to control drag-drop, so if picture 1 goes to picture box 2, it will give a message that it's in the wrong place.
My code:
Public Class Form1
Private Source As PictureBox = Nothing
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For Each PB As PictureBox In Me.Controls.OfType(Of PictureBox)()
PB.AllowDrop = True
AddHandler PB.MouseMove, AddressOf PBs_MouseMove
AddHandler PB.DragEnter, AddressOf PBs_DragEnter
AddHandler PB.DragDrop, AddressOf PBs_DragDrop
AddHandler PB.DragOver, AddressOf PBs_DragOver
Next
End Sub
Private Sub PBs_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
Dim PB As PictureBox = DirectCast(sender, PictureBox)
If Not IsNothing(PB.Image) AndAlso e.Button = Windows.Forms.MouseButtons.Left Then
Source = PB
PB.DoDragDrop(PB.Image, DragDropEffects.Copy Or DragDropEffects.Move)
End If
End Sub
Private Sub PBs_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs)
If e.Data.GetDataPresent(DataFormats.Bitmap) Then
If My.Computer.Keyboard.CtrlKeyDown Then
e.Effect = DragDropEffects.Copy
Else
e.Effect = DragDropEffects.Move
End If
Else
e.Effect = DragDropEffects.None
End If
End Sub
Private Sub PBs_DragOver(ByVal sender As Object, ByVal e As DragEventArgs)
If e.Data.GetDataPresent(DataFormats.Bitmap) Then
If My.Computer.Keyboard.CtrlKeyDown Then
e.Effect = DragDropEffects.Copy
Else
e.Effect = DragDropEffects.Move
End If
Else
e.Effect = DragDropEffects.None
End If
End Sub
Private Sub PBs_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs)
Dim PB As PictureBox = DirectCast(sender, PictureBox)
Dim tmpImage As Image = PB.Image ' store the current image
PB.Image = e.Data.GetData(DataFormats.Bitmap) ' change it to the dropped image
If e.Effect = DragDropEffects.Move Then
If Not (PB Is Source) Then
Source.Image = tmpImage ' put the stored image in the source picturebox (swap)
End If
End If
End Sub
End Class

You could try altering your PBs_DragDrop code to check which PictureBox is being dropped to like this
Private Sub PBs_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs)
Dim PB As PictureBox = DirectCast(sender, PictureBox)
If PB Is PictureBox2 Then
'handle mistake here
Else
Dim tmpImage As Image = PB.Image ' store the current image
PB.Image = e.Data.GetData(DataFormats.Bitmap) ' change it to the dropped image
If e.Effect = DragDropEffects.Move Then
If Not (PB Is Source) Then
Source.Image = tmpImage ' put the stored image in the source picturebox (swap)
End If
End If
End If
End Sub

Related

Display text for drag and drop images VB.NET

I am developing a simple SQL generator for images. I am having issues getting texts to be displayed in a textbox when I drag pictures into a PictureBox. Am I doing anything wrong? I want a situation when I drag the image into the PictureBox, the textbox shown in blue should display: 'SELECT FROM EMPLOYEE;'. I need help to get this code working. My code is displayed below.
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'' picDropTarget.AllowDrop = True
picAccept.AllowDrop = True
End Sub
Private Sub picSELECT_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles picSELECT.MouseDown, picEMPLOYEE.MouseDown
' Start the drag if it's the left mouse button.
If (e.Button = MouseButtons.Left) Then
Dim source As PictureBox = DirectCast(sender, PictureBox)
picSELECT.DoDragDrop(source.Image, DragDropEffects.Copy)
'Added this line to show
'txtSQL.Text = "SELECT"
End If
End Sub
Private Sub picAccept_DragEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles picAccept.DragEnter
' See if this is a copy and the data includes an image.
If (e.Data.GetDataPresent(DataFormats.Bitmap) AndAlso
(e.AllowedEffect And DragDropEffects.Copy) <> 0) _
Then
' Allow this.
'e.Effect = DragDropEffects.All
e.Effect = DragDropEffects.All
Else
' Don't allow any other drop.
' e.Effect = DragDropEffects.None
e.Effect = DragDropEffects.Copy
End If
End Sub
Private Sub picAccept_DragDrop(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles picAccept.DragDrop, picSELECT.DragDrop, picEMPLOYEE.DragDrop
Dim bm As Bitmap = DirectCast(e.Data.GetData(DataFormats.Bitmap, True), Bitmap)
picAccept.Image = bm
End SubEnd Class
TextBox1.Text = "select from EMPLOYEES"
Here's a fully working minimal sample. You can copy/paste the below, drop a file into the PictureBox at the bottom of the form, and the TextBox at the top will get populated with text.
Public Class Form1
Dim txtbx As New TextBox
Dim pctbx As New PictureBox
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
txtbx.Dock = DockStyle.Top
pctbx.Dock = DockStyle.Bottom
pctbx.BackColor = Color.AntiqueWhite
pctbx.AllowDrop = True
Controls.Add(txtbx)
Controls.Add(pctbx)
AddHandler pctbx.DragEnter, AddressOf pctbx_DragEnter
AddHandler pctbx.DragDrop, AddressOf pctbx_DragDrop
End Sub
Private Sub pctbx_DragEnter(sender As Object, e As DragEventArgs)
e.Effect = DragDropEffects.All
End Sub
Private Sub pctbx_DragDrop(sender As Object, e As DragEventArgs)
txtbx.Text = "select from employees where id = "
End Sub
End Class

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

How to move picture boxes in a panel using vb.net

I'm trying to move picture boxes in a panel.
This is my Code:
Private dragging As Boolean
Private beginX, beginY As Integer
Private Sub Control_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
dragging = True
beginX = CType(sender, PictureBox).Location.X
beginY = CType(sender, PictureBox).Location.Y
End Sub
Private Sub Control_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
Dim cntrl As Control = CType(sender, Control)
If dragging = True Then
cntrl.Location = New Point(cntrl.Location.X + e.X - beginX, cntrl.Location.Y + e.Y - beginY)
'Me.Refresh()
End If
End Sub
Private Sub Control_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
dragging = False
End Sub
I can't figure out why this don't work.
The subroutines you have are missing their handlers (ie the handles statement) at the end.
ex:
Private Sub Control_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) HANDLES controlName.MouseUp
dragging = False
End Sub
Try this:
Dim cmd As Boolean = False
Dim sp As Point
Private Sub Form1_Load() Handles MyBase.Load
For Each Control As Picturebox In Me.Controls.OfType(Of Picturebox)
AddHandler Control.MouseDown, Sub(sender As Object, e As MouseEventArgs)
cmd = True
sp = e.Location
End Sub
AddHandler Control.MouseMove, Sub(sender As Object, e As MouseEventArgs)
If cmd Then
Control.Location = Control.Location - sp + e.Location
End If
End Sub
AddHandler Control.MouseUp, Sub(sender As Object, e As MouseEventArgs)
cmd = False
End Sub
Next
End Sub

image drag and drop - how to get image

I am trying to write a program that allows the user to drag and drop an image from any folder to the form's picture box. I got the drag and drop part, but I don't know to do is how to store the image on a variable to be able to be dropped on the picturebox. Here is what I have so far:
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
AddHandler picCategoryImage.DragDrop, AddressOf picCategoryImage_DragDrop
AddHandler picCategoryImage.DragEnter, AddressOf picCategoryImage_DragEnter
End Sub
Private Sub picCategoryImage_DragDrop(sender As Object, e As DragEventArgs) Handles picCategoryImage.DragDrop
Dim picbox As PictureBox = CType(sender, PictureBox)
Dim g As Graphics = picbox.CreateGraphics()
g.DrawImage(CType(e.Data.GetData(DataFormats.Bitmap), Image), New Point(0, 0))
End Sub
Private Sub picCategoryImage_DragEnter(sender As Object, e As DragEventArgs) Handles picCategoryImage.DragEnter
If e.Data.GetDataPresent(DataFormats.Bitmap) Then
e.Effect = DragDropEffects.Copy
Else
e.Effect = DragDropEffects.None
End If
End Sub
where am I going wrong here?
You need DataFormats.FileDrop instead of DataFormats.Bitmap in DragEnter event. And in DragDrop you open the file name you get from e.Data.GetData():
Private Sub picCategoryImage_DragDrop(sender As System.Object, e As System.Windows.Forms.DragEventArgs) Handles picCategoryImage.DragDrop
Dim picbox As PictureBox = CType(sender, PictureBox)
Dim files() As String = CType(e.Data.GetData(DataFormats.FileDrop), String())
If files.Length <> 0 Then
Try
picbox.Image = Image.FromFile(files(0))
Catch ex As Exception
MessageBox.Show("Problem opening file ")
End Try
End If
End Sub
Private Sub picCategoryImage_DragEnter(sender As System.Object, e As System.Windows.Forms.DragEventArgs) Handles picCategoryImage.DragEnter
If e.Data.GetDataPresent(DataFormats.FileDrop) Then
e.Effect = DragDropEffects.Copy
Else
e.Effect = DragDropEffects.None
End If
End Sub
You need also to set(in form load event):
picCategoryImage.AllowDrop = True

Is it possible to print or save the picturebox with other picture box within it

I am a vb.net beginner. I got a project that I need to do function that enabled user to add new picture(which is a new picturebox) and move it in a picture box. I have made these two happened but I don`t know how to make the picturebox(that allow new picturebox move inside) save as bitmap/jpg into database. Is that possible to do that.If yes, how?
Public Class Form1
Private btn As Button ' this is a reference object
Private pic As PictureBox
Private ptX, ptY As Integer
Private drag As Boolean
Private Sub nodepic_MouseDown(ByVal senderPic As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
If e.Button = MouseButtons.Left Then
drag = True
pic = CType(senderPic, PictureBox)
ptX = e.X : ptY = e.Y
End If
If pic.Focused Then
clearButton.Enabled = True
End If
End Sub
Private Sub nodepic_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
If drag Then
If pic.Location.X >= 1 AndAlso pic.Location.Y >= 1 AndAlso
(pic.Location.X + pic.Width) <= panelPictureBox.Width - 5 AndAlso
(pic.Location.Y + pic.Height) <= panelPictureBox.Height - 5 Then
pic.Location = New Point(pic.Location.X + e.X - ptX, pic.Location.Y + e.Y - ptY)
Me.Refresh()
Else
drag = False
pic.Location = New Point(pic.Location.X + e.X - ptX, pic.Location.Y + e.Y - ptY)
End If
End If
End Sub
Private Sub node_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
drag = False
End Sub
Private Sub deleteButton(senderPic As Object, e As EventArgs)
Dim delete As DialogResult
delete = MessageBox.Show("Are you sure to delete this icon?", "Delete Icon", MessageBoxButtons.YesNo)
If delete = Windows.Forms.DialogResult.Yes Then
senderPic.Dispose()
locationLabel.Text = String.Empty
End If
End Sub
Private Sub locationButton(senderPic As Object, e As System.Windows.Forms.MouseEventArgs)
pic.Location = New Point(pic.Location.X + e.X - ptX, pic.Location.Y + e.Y - ptY)
locationLabel.Text = pic.Location.ToString()
End Sub
Private Sub TreeView1_AfterSelect(sender As Object, e As TreeNodeMouseClickEventArgs) Handles TreeView1.NodeMouseDoubleClick
Dim picBox As New PictureBox
If e.Node.Name.Equals("red") Then
picBox.Image = ImageList1.Images(0)
End If
If e.Node.Name.Equals("orange") Then
picBox.Image = ImageList1.Images(1)
End If
picBox.Location = New Point(10, 10)
panelPictureBox.Controls.Add(picBox)
action(picBox)
End Sub
Private Sub action(sender As PictureBox)
AddHandler sender.MouseDown, AddressOf nodepic_MouseDown
AddHandler sender.MouseMove, AddressOf nodepic_MouseMove
AddHandler sender.MouseUp, AddressOf node_MouseUp
AddHandler sender.MouseDoubleClick, AddressOf deleteButton
AddHandler sender.MouseClick, AddressOf locationButton
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
panelPictureBox.Enabled = True
panelPictureBox.BackColor = Color.White
End Sub
Private Sub clearButton_Click(senderPic As Object, e As EventArgs) Handles clearButton.Click
pic.Dispose()
End Sub**strong text**
End Class
You can save the PictureBox, along with its "child" PictureBoxes, to a Bitmap using code like this:
Dim bmp As New Bitmap(panelPictureBox.Width, panelPictureBox.Height)
panelPictureBox.DrawToBitmap(bmp, panelPictureBox.ClientRectangle)
Next you save it to memory by writing it out to a MemoryStream. Note that you can specify the format in the second parameter:
Dim ms As New System.IO.MemoryStream()
bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg)
Finally, you can obtain a byte array from the resulting MemoryStream:
Dim bytes() As Byte = ms.ToArray
' ... save "bytes" to your database ...
if you need to print the image inside the picturebox then you should insert printdialog ,printdocument in side the design of the form then copy the code below
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
PrintDialog1 = New PrintDialog
PrintDialog1.Document = PrintDocument1 'pbxLogo.Image
Dim r As DialogResult = PrintDialog1.ShowDialog
If r = DialogResult.OK Then
PrintDocument1.Print()
End If
End Sub
Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
e.Graphics.DrawImage(PictureBox1.Image, 0, 0)
End Sub