[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 781
  • Last Modified:

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

Hi

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

Thanks
0
Murray Brown
Asked:
Murray Brown
  • 2
1 Solution
 
käµfm³d 👽Commented:
Are you moving the item or simply copying it?
0
 
käµfm³d 👽Commented:
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
 
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
Thanks very much. I am copying it
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now