[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

DragDrop Problem on Winform

Posted on 2006-05-12
6
Medium Priority
?
497 Views
Last Modified: 2008-03-10
Hello experts...

I'm using VB2005
I have a listbox lstPool and a textbox txt1a on a winform.
The listbox is populated from a SQL db and has 39 text entries
The textbox is empty.

My goal is to populate the textbox by moving an item from the listbox, using the mouse. However, when I left-click on an item in the listbox and drag it onto the textbox. txt1a remains empty after the drag operation completes.  Here are the relevent sections of code:

   Private Sub frmConnectionList_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        '  load data into the 'DsPool.tblPool' table - to populate the listbox
        Me.TblPoolTableAdapter.Fill(Me.DsPool.tblPool)

        txt1a.AllowDrop = True
       
    End Sub


  Private Sub lstPool_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles lstPool.MouseDown
        '   Handles the mousedown event for the listbox lstPool
        If e.Button = Windows.Forms.MouseButtons.Left Then
              lstPool.DoDragDrop(lstPool.SelectedValue, DragDropEffects.Move)
        End If
    End Sub


    Private Sub txt1a_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles txt1a.DragEnter
   '    Handles the DragEnter event for textbox txt1a
        e.Effect = DragDropEffects.Move
    End Sub

    Private Sub txt1a_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles txt1a.DragDrop
   '   Handles the DragDrop event for the target. Fires when mouse is released, ending operation
        txt1a.Text = e.Data.GetData(GetType(System.String))
    End Sub

0
Comment
Question by:LarryZ
  • 4
  • 2
6 Comments
 
LVL 96

Accepted Solution

by:
Bob Learned earned 1800 total points
ID: 16667267
I set up a small test form, and came up with this working code:

1) List Box with 7 items.

2) Use the MouseMove instead of MouseDown.

3) Find the index, and use the item that you are hovering over, instead of SelectedValue/SelectedItem

  Private Sub lstPool_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles lstPool.MouseMove
    '   Handles the mousedown event for the listbox lstPool
    If e.Button = Windows.Forms.MouseButtons.Left Then
      Dim index As Integer = lstPool.IndexFromPoint(New Point(e.X, e.Y))
      lstPool.DoDragDrop(lstPool.Items(index), DragDropEffects.Move)
    End If
  End Sub


  Private Sub txt1a_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles txt1a.DragEnter
    '    Handles the DragEnter event for textbox txt1a
    e.Effect = DragDropEffects.Move
  End Sub

  Private Sub txt1a_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles txt1a.DragDrop
    '   Handles the DragDrop event for the target. Fires when mouse is released, ending operation
    txt1a.Text = e.Data.GetData(DataFormats.Text)
  End Sub

Bob
0
 

Author Comment

by:LarryZ
ID: 16669623
Thanks Bob

I tried your approach on the ListBox, but I'm still not getting the selected item dropped onto the target control. I did notice that if I first manually type some text into the textbox, it is overwritten with "" (empty string).  So I guess something is still wrong on the pickup side - must not be sending the selected item from the listbox as an argument.  

I was wondering if it had anything to do with VB2005's automated data-binding tools that I used to populate the ListBox, so I tried populating another ListBox using :
      With ListBox1.Items
            .Add("qwerq")
            .Add("vnbcvn")
            .Add("ioiuho")
        End With
but it didn't work with either.

While experimenting, I was able to move text from TextBox to TextBox, but not ListBox to TextBox. Actually, I really wanted to go one step farther - ListBox to DataGridView cells.

Any ideas?
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16669727
Did you put in any debug breakpoints, and check the value that you are setting for the DataObject in the MouseMove event?

Bob
0
Industry Leaders: 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!

 

Author Comment

by:LarryZ
ID: 16671350
I inserted a breakpoint at 'End Sub' in the MouseMove event, then perused the "e"  and "sender" entries in Locals window. Not sure which entry would represent the DataObject. It seems hard to do (timing issue?). As soon as the mouse enters the listbox, the breakpoint is triggered.  I'm thinking you're on the right track, probably the desired data is not being sent.
0
 

Author Comment

by:LarryZ
ID: 16671599
I discovered that this seems to work nicely for copying:

  If e.Button = Windows.Forms.MouseButtons.Left Then
            Dim index As Integer = lstPool.IndexFromPoint(New Point(e.X, e.Y))
            lstPool.DoDragDrop(lstPool.Text, DragDropEffects.Copy)
        End If

(instead of:    lstPool.DoDragDrop(lstPool.Items(index), DragDropEffects.Copy)

It places the item selected from the ListBox into the TextBox.

I still could use some guidance on how to put it in a DataGridViewCell instead of the TextBox. Hard to point it to the cell you are houvering over when you release the mouse....

Thanks for pointing me in the right direction.
0
 

Author Comment

by:LarryZ
ID: 16671976
I got lucky. This link shows you how to drag items to DataGrisView cells in VB.Net:

http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=352688&SiteID=1
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

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

872 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