Solved

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

Posted on 2010-09-01
14
568 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 70

Expert Comment

by:Éric Moreau
ID: 33586046
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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
 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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 …
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

766 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