Solved

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

Posted on 2010-09-01
14
565 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 85

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 69

Expert Comment

by:Éric Moreau
ID: 33586046
0
 

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 85

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 250 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 85

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 85

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…

914 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now