I have a problem with my project. (attached file).
When I click on the "computer", I get a computer on the screen. Suppose on the screen, i have 2 computers. (they can be moved)
Now I want that:
1. When I click on the "Connection" button and choose the 2 computers, the connection is drawing. If still want to draw a connection, I have to click on the "Connection", choose the 2 computers again.
2. When I move the computer's location, "connection" will not be lost.
'========== DI CHUYEN Group(i)========== MOT KIEU HAM
Private dragging As Boolean
Private beginX, beginY As Integer
Private Sub Control_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
dragging = True
beginX = e.X
beginY = e.Y
End Sub
Private Sub Control_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
Dim DraggedControl As Control = DirectCast(sender, Control)
If dragging = True Then
DraggedControl.Location = New Point(DraggedControl.Location.X +
e.X - beginX, DraggedControl.Location.Y +
e.Y - beginY)
Me.Refresh()
End If
End Sub
Private Sub Control_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
dragging = False
End Sub
Private Sub Control_layvitri(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
Dim vtriControl As New Point
vtriControl = e.Location
End Sub
#End Region
'=========HET phan di chuyen group(i) ==============
#Region " TAO GROUP"
' "GROUP COMPUTER"
Private Sub computer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles computer.Click
butcom(icom) = New Button
With butcom(icom)
.Name = "computer " & icom
.Location = New Point(376, 178)
.Width = 55
.Height = 55
.BackColor = Color.Green
.Image = Image.FromFile(Application.StartupPath & "\data\PC.png")
End With
Me.Controls.Add(butcom(icom))
butcom(icom).BringToFront()
ToolTip1.SetToolTip(butcom(icom), "Computer " & icom)
AddHandler butcom(icom).MouseDown, AddressOf Me.Control_MouseDown
AddHandler butcom(icom).MouseDown, AddressOf Me.Control_layvitri
AddHandler butcom(icom).MouseMove, AddressOf Me.Control_MouseMove
AddHandler butcom(icom).MouseUp, AddressOf Me.Control_MouseUp
icom = icom + 1
End Sub
'================= HET GROUP COMPUTER====================
#End Region
End Class
I am also curious if anyone has an elegant solution. I have started a similar project but opted for the GDI+ approach; I created a user control that keeps a collection of objects (computers in your case). The user control intercepts its parent's paint event and paints the objects stored in the collection. I am looking in to implementing an A* (pronounced A-Star) path finding algorithm to enable "connections" to be redrawn when the objects are moved by the user after a connection has been made.
One thing to note is that if you want to continue using dynamically created controls as you are, you should probably make the picturebox a panel and add the "computer" objects to the panel's control collection instead of the form's.
Bookmarks