VB.NET optimizing drag & drop code - vb.net

I'm currently struggling with drag & drop code. I have 3 images that are at the top, and I want to add them in a random order in a flow-layout-panel.
I have this code for adding the square image into the flow-layout-panel, but I have got the feeling that this is not 100% correct. Is it possible to add these with 1 sub instead of 3?
And how do you write a sub that detects what object in being dragged? Now my sub just adds a square with each dragdrop event. But I need it to drop a Square only when a Square is being dragged and drop a Trapezium or round when it's being dragged.
Public Class Form2
Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
' Initiate dragging.
PictureBox1.DoDragDrop(PictureBox1, DragDropEffects.Copy)
End Sub
Private Sub PictureBox2_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox2.MouseMove
' Initiate dragging.
PictureBox2.DoDragDrop(PictureBox2, DragDropEffects.Copy)
End Sub
Private Sub PictureBox3_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox3.MouseMove
' Initiate dragging.
PictureBox3.DoDragDrop(PictureBox2, DragDropEffects.Copy)
End Sub
Private Sub FlowLayoutPanel1_DragEnter(ByVal sender As Object, ByVal e As _
System.Windows.Forms.DragEventArgs) Handles FlowLayoutPanel1.DragEnter
' Check the format of the data being dropped.
If (e.Data.GetDataPresent(GetType(PictureBox))) Then
' Display the copy cursor.
e.Effect = DragDropEffects.Copy
Else
' Display the no-drop cursor.
e.Effect = DragDropEffects.None
End If
End Sub
Private Sub FlowLayoutPanel1_DragDrop(ByVal sender As Object, ByVal e As _
System.Windows.Forms.DragEventArgs) Handles FlowLayoutPanel1.DragDrop
Dim oPB As New PictureBox()
oPB.Image = Image.FromFile("C:\Users\Jef\Desktop\square.jpg")
oPB.Visible = True
oPB.Width = 100
oPB.Height = 100
oPB.SizeMode = PictureBoxSizeMode.CenterImage
FlowLayoutPanel1.Controls.Add(oPB)
End Sub

You already got the answer in your previous question:
Private Sub FlowLayoutPanel1_DragDrop(ByVal sender As Object, ByVal e As _
System.Windows.Forms.DragEventArgs) Handles FlowLayoutPanel1.DragDrop
Dim oPB As New PictureBox()
Dim pb = CType(e.Data.GetData(GetType(PictureBox)))
oPB.Image = pb.Image
pb.Image = Nothing '' Optional
'' etc...
End Sub
You do have a bug, the probable reason why you did not use the answer:
Private Sub PictureBox3_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox3.MouseMove
' Initiate dragging.
PictureBox3.DoDragDrop(PictureBox2, DragDropEffects.Copy)
End Sub
Note how it drags the wrong control, PictureBox2 instead of PictureBox3. You avoid bugs like this by writing DRY code, Do not Repeat Yourself. The sender argument of a MouseMove event already gives you a reference to the control. So you just need one event handler for all three controls:
Private Sub PictureBox_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove, PictureBox2.MouseMove, PictureBox3.MouseMove
' Initiate dragging.
Me.DoDragDrop(sender, DragDropEffects.Copy)
End Sub
With the detail that we now let the Form support DoDragDrop(). Which only matters if you implement the GiveFeedback or QueryContinueDrag events.

Related

form increases size when resizing using button

so I removed the borders of my program to have my own custom ui into it, so of course I have to make a way to resize it. i do this by using a button, but when it is on the bottom-right sides of the screen the program increases its size by a lot but it can still be resized, and when it's on the top left side it still does it but only increases the size by a little.
also I know there are other ways, but for me this one's the easiest, it's just that the button increases it's own size for some reason.
here's the code:
Private Sub tim_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tim.Tick
Me.Size = MousePosition
End Sub
Private Sub res_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles res.MouseDown
tim.Enabled = True
End Sub
Private Sub res_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles res.MouseUp
tim.Enabled = False
End Sub
If I understand your intent correctly, what you are missing is the Control.PointToClient method. It converts MousePosition from coordinates on your screen to coordinates on your form.
Here's a modified version of your code.
Private Sub tim_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tim.Tick
Dim curpos As Point = PointToClient(MousePosition)
Size = New Size(curpos.X + (Size.Width - ClientSize.Width), curpos.Y + (Size.Height - ClientSize.Height))
'if your form has no border you can skip the above and just use:
' Size = PointToClient(MousePosition)
End Sub
Private Sub res_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles res.MouseDown
Cursor.Position = PointToScreen(ClientRectangle.Size)
Cursor = Cursors.SizeNWSE
tim.Enabled = True
End Sub
Private Sub res_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles res.MouseUp
Cursor = Cursors.Default
tim.Enabled = False
End Sub

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 Text Box to List Box

I'm working on a VB.net(2015) code that would allow me to drag and drop from a text box to a list box.
I've set up the code below, and set the allow drop in the list box to true.
I’m able to drag the text to the list box, but it doesn’t appear in the list box.
Looking at the code below I’m not sure what piece I’ve left out, are there any recommendations or suggestions on what can be done?
Public Class PotluckParty
Private Sub enterFoodTextBox_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles enterFoodTextBox.MouseDown
enterFoodTextBox.DoDragDrop(enterFoodTextBox.Text, DragDropEffects.Move)
End Sub
Private Sub SaladListBox_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles SaladListBox.DragDrop
SaladListBox.Text &= e.Data.GetData(DataFormats.Text).ToString
enterFoodTextBox.Text = ""
End Sub
Private Sub SaladListBox_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles SaladListBox.DragEnter
If (e.Data.GetDataPresent(DataFormats.Text)) Then
e.Effect = DragDropEffects.Move
End If
End Sub

DragLeave doesn't get called

in my VB.Net application I am try to drag and drop a custom control. It should be dropable onto another instance of the same class. So here is the code I use:
Private Sub HandleMouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles Me.MouseDown
Me.DoDragDrop(Me, DragDropEffects.Copy)
End Sub
Private Sub HandleDragEnter(ByVal sender As Object, ByVal e As DragEventArgs) Handles Me.DragEnter
e.Effect = If(checkDropData(e), DragDropEffects.Copy, DragDropEffects.None)
End Sub
Private Sub HandleDragDrop(ByVal sender As Object, ByVal e As DragEventArgs) Handles Me.DragDrop
addControl(e.Data.GetData(GetType(MyControl)))
End Sub
Private Sub HandleDragLeave(ByVal sender As Object, ByVal e As DragEventArgs) Handles Me.DragLeave
Console.WriteLine("HandleDragLeave: " & sender.ToString)
End Sub
I am able to drop the control but while dragging the DragLeave event never gets called. Did I miss something?
That's very bad... I used the wrong signature but the was nothing which told me I was wrong.
I found the solution here. I used DragEventArgs instead of EventArgs as type of e. After changing the type it works like expected.

Drag drop a usercontrol in vb.net

I have, what should be , a very simple problem, but now I have used 5 hours without results.
I have a usercontrol, UserControl1, which I want to drag and drop on my form, Form1.
That’s it. It should be simple, but I have googled for hours without results. Does anybody have a sample code to fix this?
I dont know what a user control is (I'm still learning) but I found something that might help.
In this code, add two TextBox controls to a form and set the AllowDrop property of the second TextBox control to True.
Then use this code to enable drag and drop
Private MouseIsDown As Boolean = False
Private Sub TextBox1_MouseDown(ByVal sender As Object, ByVal e As _
System.Windows.Forms.MouseEventArgs) Handles TextBox1.MouseDown
' Set a flag to show that the mouse is down.
MouseIsDown = True
End Sub
Private Sub TextBox1_MouseMove(ByVal sender As Object, ByVal e As _
System.Windows.Forms.MouseEventArgs) Handles TextBox1.MouseMove
If MouseIsDown Then
' Initiate dragging.
TextBox1.DoDragDrop(TextBox1.Text, DragDropEffects.Copy)
End If
MouseIsDown = False
End Sub
Private Sub TextBox2_DragEnter(ByVal sender As Object, ByVal e As _
System.Windows.Forms.DragEventArgs) Handles TextBox2.DragEnter
' Check the format of the data being dropped.
If (e.Data.GetDataPresent(DataFormats.Text)) Then
' Display the copy cursor.
e.Effect = DragDropEffects.Copy
Else
' Display the no-drop cursor.
e.Effect = DragDropEffects.None
End If
End Sub
Private Sub TextBox2_DragDrop(ByVal sender As Object, ByVal e As _
System.Windows.Forms.DragEventArgs) Handles TextBox2.DragDrop
' Paste the text.
TextBox2.Text = e.Data.GetData(DataFormats.Text)
End Sub
I hope that you can use this for a usercontrol. Good luck and comment!
Here is the code, I have used, to get it work.
Now I have a form, Form1, and a usercontrol, Usercontrol1. To drag the usercontrol, I inserted a panel in the top of the usercontrol, and only if the user pressed the panel (panel1), the control should to move - like normal windows forms.
Public Class UserControl1
Shared mypositionX As Integer
Shared mypositionY As Integer
Shared mBlnFormDragging As Boolean
Shared drawBeginX As Integer
Shared drawBeginY As Integer
Shared drawing As Boolean
Private Sub Panel1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseMove
If mBlnFormDragging = True Then
Dim position As Point = Form1.PointToClient(MousePosition)
Me.Location = New Point(position)
End If
End Sub
Private Sub Panel1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseUp
' Dim dd1 As DragDropEffects = DoDragDrop(ParentForm, DragDropEffects.Move)
mBlnFormDragging = False
Dim position As Point = Form1.PointToClient(MousePosition)
Location = New Point(position)
End Sub
Public Sub Panel1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseDown
'Dim dd1 As DragDropEffects = DoDragDrop(ParentForm, DragDropEffects.Move)
mBlnFormDragging = True
End Sub