For one of my applicaitons I am trying to display multiple listboxes onto a form (representing different database tables) and then allow a user to drag a listbox item from one listbox over to another (representing relationships). When the user drops the selected item I want to programmatically draw a line from the selected item from the first listbox over to the dropped item in the second listbox. (For those of you who use SQL Server Enterprise Manager, this functionality is very similiar to when joining multiple tables when creating a view).
I can get the line to appear from the selected item in the first listbox, but I can not seem to get the line to end on the selected item of the second listbox.
I was wondering if someone could review my code to see what I am doing wrong. Please understand that I did not come up with this code on my own; I received it from this forum and from the Microsoft website.
With this code the line starts at the right spot, but it draws a line way past the second listbox. Please help!
Also, at this time I would also like to personally thank EarthAndFire, JonBatts, and Astrodestino for helping me on previous posts this past week.
-lucyv
I can get the line to appear from the selected item in the first listbox, but I can not seem to get the line to end on the selected item of the second listbox.
I was wondering if someone could review my code to see what I am doing wrong. Please understand that I did not come up with this code on my own; I received it from this forum and from the Microsoft website.
Code:
' Private module variables used to hold the X/Y coordinates.
Private X1 As Integer
Private Y1 As Integer
Private X2 As Integer
Private Y2 As Integer
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' Add items into list boxes, for now.
With Me.ListBox1
.Items.Add("Here 01")
.Items.Add("Here 02")
End With
With Me.ListBox2
.Items.Add("Here 03")
.Items.Add("Here 04")
End With
End Sub
Private Sub listBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListBox1.MouseDown
' Determines which item was selected.
Dim lb As ListBox = CType(sender, ListBox)
Dim pt As New Point(e.X, e.Y)
'Retrieve the item at the specified location within the ListBox.
Dim index As Integer = lb.IndexFromPoint(pt)
' Places the X/Y locations of ListBox1 into the starting X1 and Y1 coordinates.
X1 = Me.ListBox1.Location.X + Me.ListBox1.Width - 5
Y1 = e.Y + 10
' Starts a drag-and-drop operation.
If index >= 0 Then
' Retrieve the selected item text to drag into the RichTextBox.
lb.DoDragDrop(lb.Items(index).ToString(), DragDropEffects.Copy)
End If
End Sub
Private Sub ListBox2_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles ListBox2.DragEnter
' If the data is text, copy the data to the RichTextBox control.
If e.Data.GetDataPresent("Text") Then
e.Effect = DragDropEffects.Copy
End If
End Sub
Private Sub ListBox2_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles ListBox2.DragDrop
' Determines which item was selected.
Dim lb As ListBox = CType(sender, ListBox)
Dim pt As New Point(e.X, e.Y)
'Retrieve the item at the specified location within the ListBox.
Dim index As Integer = lb.IndexFromPoint(pt)
' Calls the DrawLine function, passing the X/Y coordinates.
Call DrawLine(X1, Y1, pt.X, pt.Y)
End Sub
Private Sub DrawLine(ByVal iX1 As Integer, ByVal iY1 As Integer, ByVal iX2 As Integer, ByVal iY2 As Integer)
Dim g As Graphics
Dim oPen As New Pen(Color.Red, 2)
Try
g = Me.CreateGraphics
g.DrawLine(oPen, iX1, iY1, iX2, iY2)
Finally
oPen.Dispose()
oPen = Nothing
g.Dispose()
g = Nothing
End Try
End Sub
With this code the line starts at the right spot, but it draws a line way past the second listbox. Please help!
Also, at this time I would also like to personally thank EarthAndFire, JonBatts, and Astrodestino for helping me on previous posts this past week.
-lucyv