Solved

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

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

Expert Comment

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

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

708 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

17 Experts available now in Live!

Get 1:1 Help Now