Solved

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

Posted on 2012-04-09
3
628 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

813 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

10 Experts available now in Live!

Get 1:1 Help Now