?
Solved

drag and drop between two  instances of the same form in vb.net in winforms?

Posted on 2010-09-01
14
Medium Priority
?
613 Views
Last Modified: 2012-05-10
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.
0
Comment
Question by:RekhaShah
14 Comments
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 33581849
What types of controls will the drag/drops be initiated from?

Should the controls ONLY accept drops from that other form?
...or can they accept string data from anywhere?
0
 
LVL 14

Expert Comment

by:systan
ID: 33584708
Here's a code;
Just put or assign on the events of every listbox dragdrop,dragenter,dragleave, mousedown
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

Open in new window

0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 33586046
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:RekhaShah
ID: 33587228
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.
0
 

Author Comment

by:RekhaShah
ID: 33587473
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.
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 33587529
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...
0
 

Author Comment

by:RekhaShah
ID: 33588154
No It is not that complex :)
I am attaching a word doc that may explain things little better.

dragdropProblem.docx
0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 1000 total points
ID: 33588570
Here's another example to play with that demonstrates moving text between textboxes:
http://www.experts-exchange.com/Programming/Languages/.NET/Visual_Basic.NET/Q_26441747.html#33568927

I would start with just a bare bones new project and forms with textboxes and use the example above or one of the other linked examples by the other experts and play with it until you understand it.
0
 

Author Comment

by:RekhaShah
ID: 33588800

Gives me an error
 Option Strict On disallows implicit conversions from 'Object' to 'System.Windows.Forms.DragDropEffects'. C:\Documents and Settings\rshah\Local Settings\Application Data\Temporary Projects\WindowsApplication1\Form1.vb 35 24 WindowsApplication1
 
on line

e.Effect = IIf(tb.Text.Trim.Length = 0, DragDropEffects.All, DragDropEffects.None)



0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 33588829
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
0
 

Author Comment

by:RekhaShah
ID: 33588895
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,
0
 

Author Comment

by:RekhaShah
ID: 33588959
It works for the textboxes on two instances of the form without any problem.
THANK YOU!!!!
Question is, will it work for the treeview? :)
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 33589027
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...)
0
 

Author Closing Comment

by:RekhaShah
ID: 33589717
Thanks a lot.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question