?
Solved

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

Posted on 2010-09-01
14
Medium Priority
?
591 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

764 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