Solved

VB.net Drag and drop from a ListBox to a TextBox

Posted on 2012-04-09
3
651 Views
Last Modified: 2012-04-09
Hi

In VB.net what code would I use to drag a List Box item into a TextBox below it.

Thanks
0
Comment
Question by:murbro
  • 2
3 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 37822915
Are you moving the item or simply copying it?
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 37822928
Here's a very simple example of copying the text into the TextBox.

Public Class Form1

  Private Sub ListBox1_MouseDown(sender As Object, e As MouseEventArgs) Handles ListBox1.MouseDown
    If e.Button = Windows.Forms.MouseButtons.Left AndAlso Me.ListBox1.SelectedItem IsNot Nothing Then
      Me.ListBox1.DoDragDrop(Me.ListBox1.SelectedItem.ToString(), DragDropEffects.Copy)
    End If
  End Sub

  Private Sub TextBox1_DragEnter(sender As Object, e As DragEventArgs) Handles TextBox1.DragEnter
    If e.Data.GetDataPresent(System.Windows.Forms.DataFormats.StringFormat) Then
      e.Effect = DragDropEffects.Copy
    End If
  End Sub

  Private Sub TextBox1_DragDrop(sender As Object, e As DragEventArgs) Handles TextBox1.DragDrop
    If e.Data.GetDataPresent(System.Windows.Forms.DataFormats.StringFormat) Then
      Me.TextBox1.Text = e.Data.GetData(System.Windows.Forms.DataFormats.StringFormat).ToString()
    End If
  End Sub
End Class

Open in new window


Removing the text from the source ListBox would require a bit more work, but not much.
0
 

Author Closing Comment

by:murbro
ID: 37822935
Thanks very much. I am copying it
0

Featured Post

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!

Question has a verified solution.

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

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

730 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