RekhaShah
asked on
drag and drop between two instances of the same form in vb.net in winforms?
how would you drag and drop between two instances of the same form in vb.net in winforms?
I want the user to be able to drag and drop from the same controls between two instances of the same form. Appreciate your help.
I want the user to be able to drag and drop from the same controls between two instances of the same form. Appreciate your help.
Here's a code;
Just put or assign on the events of every listbox dragdrop,dragenter,draglea ve, mousedown
Just put or assign on the events of every listbox dragdrop,dragenter,draglea
Public Class Form1
Private Sub listBox_DragDrop(ByVal sender As Object, ByVal e As DragEventArgs) Handles ListBox1.DragDrop, ListBox2.DragDrop
Dim lb As ListBox = TryCast(sender, ListBox)
For Each item As String In lb.Items
If e.Data.GetData(DataFormats.Text) = item Then
Return
End If
Next
lb.Items.Add(e.Data.GetData(DataFormats.Text))
End Sub
Private Sub listBox_DragEnter(ByVal sender As Object, ByVal e As DragEventArgs) Handles ListBox1.DragEnter, ListBox2.DragEnter
e.Effect = DragDropEffects.Copy
End Sub
Private Sub listBox_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles ListBox2.MouseDown, ListBox1.MouseDown
Dim lb As ListBox = TryCast(sender, ListBox)
If lb.SelectedItem Is Nothing Then
Return
End If
lb.DoDragDrop(lb.SelectedItem, DragDropEffects.Copy)
End Sub
Private Sub listBox_DragLeave(ByVal sender As Object, ByVal e As EventArgs) Handles ListBox2.DragLeave
Dim lb As ListBox = TryCast(sender, ListBox)
lb.Items.Remove(lb.SelectedItem)
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
ListBox1.AllowDrop = True
ListBox2.AllowDrop = True
End Sub
End Class
ASKER
Hi Systan, I trid the your code with few modifications:
1. It is for textbox.
2. I am using move instead of copy.
It partially works. It moves the content from textbox1 for Form instance 1 to Textbox1 of Form Instance 2.
But, it does not clear the buffer and as soon as i click on the textbox1 where I had just dropped the text, keeps on repeating. eg, If i move "AAA" to that textbox, after dragdrop, if I click on that textbox, it's content becomes "AAAA', If i click again, it becomes "AAAAAA" and so on. It looks like we need drag end(?) .I guess as soon as I click, Mousedown even for Textbox1 gets triggerred and it starts over again. So I want to prevent it if it is the same control.
1. It is for textbox.
2. I am using move instead of copy.
It partially works. It moves the content from textbox1 for Form instance 1 to Textbox1 of Form Instance 2.
But, it does not clear the buffer and as soon as i click on the textbox1 where I had just dropped the text, keeps on repeating. eg, If i move "AAA" to that textbox, after dragdrop, if I click on that textbox, it's content becomes "AAAA', If i click again, it becomes "AAAAAA" and so on. It looks like we need drag end(?) .I guess as soon as I click, Mousedown even for Textbox1 gets triggerred and it starts over again. So I want to prevent it if it is the same control.
ASKER
Hi Idle_Mind,
In this form that i am working on, it will accept data from the same form from a different instance. It can be treeview node, text box or listview items. The problem is I have to set a flag or something to recognize that it is not being dragged-dropped on the same control itself.
This is a very complex project, and I am not that well versed in vb.net. So will be gratful for any guidence or help.
In this form that i am working on, it will accept data from the same form from a different instance. It can be treeview node, text box or listview items. The problem is I have to set a flag or something to recognize that it is not being dragged-dropped on the same control itself.
This is a very complex project, and I am not that well versed in vb.net. So will be gratful for any guidence or help.
So you have three different types of controls...
Do you want only dragging/dropping between the SAME type of controls?
TextBox --> TextBox
TreeView --> TreeView
ListView --> ListView
Or is it more complex?...like dragging text from a TextBox to a TreeView might create a new node?...or add to an existing node?
What about the TreeView?
...can you only drag/drop leaf nodes?
...or can you drag/drop an entire structure and all child nodes get moved to?
...can you drop a node/structure only to the root?
Detail, details, details...
Do you want only dragging/dropping between the SAME type of controls?
TextBox --> TextBox
TreeView --> TreeView
ListView --> ListView
Or is it more complex?...like dragging text from a TextBox to a TreeView might create a new node?...or add to an existing node?
What about the TreeView?
...can you only drag/drop leaf nodes?
...or can you drag/drop an entire structure and all child nodes get moved to?
...can you drop a node/structure only to the root?
Detail, details, details...
ASKER
No It is not that complex :)
I am attaching a word doc that may explain things little better.
dragdropProblem.docx
I am attaching a word doc that may explain things little better.
dragdropProblem.docx
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Gives me an error
Option Strict On disallows implicit conversions from 'Object' to 'System.Windows.Forms.Drag
on line
e.Effect = IIf(tb.Text.Trim.Length = 0, DragDropEffects.All, DragDropEffects.None)
You can break it out and do:
If tb.Text.Trim.Length = 0 Then
e.Effect = DragDropEffects.All
Else
e.Effect = DragDropEffects.None
End If
If tb.Text.Trim.Length = 0 Then
e.Effect = DragDropEffects.All
Else
e.Effect = DragDropEffects.None
End If
ASKER
It runs fine now, i fixed it .i will try to create a second instance of the form and see if it runs ok. Thanks,
ASKER
It works for the textboxes on two instances of the form without any problem.
THANK YOU!!!!
Question is, will it work for the treeview? :)
THANK YOU!!!!
Question is, will it work for the treeview? :)
You would need to modify it for the TreeView.
In that example I was passing the TextBox itself...so you'd need to change it up and pass the TreeView instead. You might want to pass the TreeNode instead...
Note that I created an explicit DataObject to pass and gave it a custom name:
Dim data As New DataObject("MyTextBox", tb)
You could use a different name for each type of control so that only that specific custom name is accepted on a drop. This way you know that it was your application as the source and you can cast the dataobject within to your known datatype (TextBox, String, TreeNode, Array of String for ListView items, etc...)
In that example I was passing the TextBox itself...so you'd need to change it up and pass the TreeView instead. You might want to pass the TreeNode instead...
Note that I created an explicit DataObject to pass and gave it a custom name:
Dim data As New DataObject("MyTextBox", tb)
You could use a different name for each type of control so that only that specific custom name is accepted on a drop. This way you know that it was your application as the source and you can cast the dataobject within to your known datatype (TextBox, String, TreeNode, Array of String for ListView items, etc...)
ASKER
Thanks a lot.
Should the controls ONLY accept drops from that other form?
...or can they accept string data from anywhere?