Results 1 to 2 of 2

Thread: joing picture boxes together with the line control

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2001
    Posts
    1,331

    joing picture boxes together with the line control

    I need to be able to join picture boxes together using the line control. When the user clicks on one picture box, the line will start from there, and when he clicks the second picture box to join the line to. Both picture boxes will be joined together. So when they drag the boxes the line will stay attached. Can someone tell me how to join these picture boxes together using the line control.

    Many thanks in advance

    Steve
    steve

  2. #2
    PowerPoster beachbum's Avatar
    Join Date
    Jul 2001
    Location
    Wollongong, NSW, Australia
    Posts
    2,274
    hi
    here is a quick throw together that u can adjust to your heart's content. Maybe u want to change from drag drop to mousemove... is up to you.
    Regards
    Stuart

    Form has:
    1 big picture box - Picture1
    4 little picture boxes - Picture2(0),(1),(2),(3) control array placed inside Picture1.. i set backcolor to red on mine to distinguish
    1 Line control - Line1
    VB Code:
    1. Option Explicit
    2.  
    3. Private Type LineConnect
    4.     X As Single
    5.     Y As Single
    6. End Type
    7. Dim LineCon() As LineConnect 'keeps record of connection posns
    8.  
    9. Dim fsngDragOrigTop As Single 'drag original posns
    10. Dim fsngDragOrigLeft As Single
    11. Dim fsngDragOrigX As Single
    12. Dim fsngDragOrigY As Single
    13. Dim lConn(1) As Integer 'which pics are connected
    14. Dim lConnPosn As Integer 'which new pic becomes connected
    15.  
    16. Private Sub Form_Load()
    17.     ReDim LineCon(1)
    18.     lConn(0) = 0 'default to pic(0)
    19.     lConn(1) = 1 'default to pic(1)
    20.     lConnPosn = 1 'next click will move right hand pic
    21.     Edges 'find edge posn
    22.     DrawLine 'draw line
    23. End Sub
    24.  
    25. 'CONNECTING LINE
    26. '=========================================================
    27. Private Sub Edges()
    28.     'Works out how to connect lines
    29.     'So eg pic on left and pic on right
    30.     'connection will be from middle right of left pic x = 1 y = 0.5
    31.     'to middle left of right pic x = 0: y = 0.5
    32.     With Picture2(lConn(0))
    33.         If .Left + .Width < Picture2(lConn(1)).Left - 400 Then
    34.             With LineCon(0): .X = 1: .Y = 0.5: End With
    35.             With LineCon(1): .X = 0: .Y = 0.5: End With
    36.         ElseIf .Left > Picture2(lConn(1)).Left + Picture2(lConn(1)).Width + 400 Then
    37.             With LineCon(0): .X = 0: .Y = 0.5: End With
    38.             With LineCon(1): .X = 1: .Y = 0.5: End With
    39.         ElseIf .Top + .Height < Picture2(lConn(1)).Top Then
    40.             With LineCon(0): .X = 0.5: .Y = 1: End With
    41.             With LineCon(1): .X = 0.5: .Y = 0: End With
    42.         Else
    43.             With LineCon(0): .X = 0.5: .Y = 0: End With
    44.             With LineCon(1): .X = 0.5: .Y = 1: End With
    45.         End If
    46.     End With
    47. End Sub
    48.  
    49. Private Sub DrawLine()
    50.     'Draw the connecting line
    51.     With Line1
    52.         .X1 = Picture2(lConn(0)).Left + Picture2(lConn(0)).Width * LineCon(0).X
    53.         .Y1 = Picture2(lConn(0)).Top + Picture2(lConn(0)).Height * LineCon(0).Y
    54.         .X2 = Picture2(lConn(1)).Left + Picture2(lConn(1)).Width * LineCon(1).X
    55.         .Y2 = Picture2(lConn(1)).Top + Picture2(lConn(1)).Height * LineCon(1).Y
    56.     End With
    57. End Sub
    58.  
    59. 'MOVING PIC BOX
    60. '=========================================================
    61. Private Sub Picture2_MouseDown(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single)
    62. 'starting position for drag drop
    63.     With Picture2(Index)
    64.         fsngDragOrigTop = .Top
    65.         fsngDragOrigLeft = .Left
    66.         fsngDragOrigX = X
    67.         fsngDragOrigY = Y
    68.         .Drag vbBeginDrag
    69.     End With
    70. End Sub
    71.  
    72. Private Sub Picture2_DragDrop(Index As Integer, Source As Control, X As Single, Y As Single)
    73. 'ending position if user drags only within source pic
    74.     On Error Resume Next
    75.     With Picture2(Index)
    76.         .Left = fsngDragOrigLeft + (X - fsngDragOrigX)
    77.         .Top = fsngDragOrigTop + (Y - fsngDragOrigY)
    78.         .Drag vbEndDrag
    79.     End With
    80.     Edges
    81.     DrawLine
    82. End Sub
    83.    
    84. Private Sub Picture1_DragDrop(Source As Control, X As Single, Y As Single)
    85.     On Error Resume Next
    86.     With Source
    87.         .Left = (X - fsngDragOrigX)
    88.         .Top = (Y - fsngDragOrigY)
    89.         .Drag vbEndDrag
    90.     End With
    91.     Edges
    92.     DrawLine
    93. End Sub
    94. '=========================================================
    95.  
    96. 'SELECTING NEW PIC BOX
    97. '=========================================================
    98. Private Sub Picture2_DblClick(Index As Integer)
    99.     'Move connection to another pic
    100.     lConn(lConnPosn) = Index
    101.     lConnPosn = 1 - lConnPosn
    102.     Edges
    103.     DrawLine
    104. End Sub
    Stuart Laidlaw
    Brightspark Financial Software
    http://www.gstsmartbook.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width