Results 1 to 5 of 5

Thread: connecting lines to picture boxes at runtime

  1. #1

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

    connecting lines to picture boxes at runtime

    l have some picture boxes loaded on to a form at run-time. and l connect these picture boxes with lines at run-time. But the problem is l need a simple way to drag the picture boxes round the form, so all the lines stay attached to the picture boxes. picture boxes can have more then one line attached to it.

    If you have time, can you answer me this question. if a user clicks on a picture box, how can l find out what line is connected to it.

    Many thanks in advance
    steve

  2. #2
    PowerPoster beachbum's Avatar
    Join Date
    Jul 2001
    Location
    Wollongong, NSW, Australia
    Posts
    2,274
    Hi again Steve
    I thought that i already answered something very similar to this before. http://www.vbforums.com/showthread.p...highlight=line ... what sort of problems have u got, changes to make etc? You say that a picture box can have multiple lines. I would suggest that you would need to make a Type variable that records the picture box number, the number of lines (u may have a fixed number), the connection position and the picture and connection position of the next picture.

    Can u post a picture of the layout that you are looking for. Maybe throw it together in Paint or Excel or something
    Regards
    Stuart
    Stuart Laidlaw
    Brightspark Financial Software
    http://www.gstsmartbook.com

  3. #3
    PowerPoster beachbum's Avatar
    Join Date
    Jul 2001
    Location
    Wollongong, NSW, Australia
    Posts
    2,274
    Hi again steve
    In a one hour throw together I have made a competitor to Visio so this better be what u want !! All u need is a picture box on a form (picture1) a picture box inside picture 1 (picture2) with its index set to 0 and a line inside picture 1 (line1) with its index set to 0. Also, put on 2 command buttons (command1 and command2)
    Regards
    Stuart

    PS Have included as frm file and shown code over two posts cos is too long for one.

    VB Code:
    1. Option Explicit
    2.  
    3. Private Type LineConnType
    4.     StartPic As Integer
    5.     EndPic As Integer
    6. End Type
    7. Dim LineConn() As LineConnType
    8. Dim NextLoadLine As Integer
    9. Dim SpareLine As Integer
    10.  
    11. Private Type PicOffsetType
    12.     CurX As Single
    13.     CurY As Single
    14. End Type
    15. Dim PicOffset As PicOffsetType
    16. Dim PictureConn() As Integer
    17. Dim NextLoadPicture As Integer
    18. Dim SparePicture As Integer
    19.  
    20. Dim CurLine As Integer
    21. Dim CurPicture As Integer
    22.  
    23. Dim ConnectStatus As Integer
    24. Dim DragStatus As Integer
    25.  
    26. Dim lCounter As Integer
    27.  
    28. Private Sub Form_Load()
    29.     'Set up main back picture
    30.     With Picture1
    31.         .Appearance = 0
    32.         .BorderStyle = vbFixedSingle
    33.         .BackColor = vbWhite
    34.         .Move 500, 500, 5000, 5000
    35.     End With
    36.    
    37.     'Set up first control array picture
    38.     With Picture2(0)
    39.         .Appearance = 0
    40.         .BorderStyle = vbFixedSingle
    41.         .BackColor = vbRed
    42.         .Visible = False
    43.         .Move 0, 0, 400, 400
    44.     End With
    45.    
    46.     'Set up first control array line
    47.     Line1(0).Visible = False
    48.    
    49.     'Set up command buttons
    50.     With Command1
    51.         .Caption = "Load New"
    52.         .Move 500, 100, 1200, 300
    53.     End With
    54.     With Command2
    55.         .Caption = "Delete"
    56.         .Move 2000, 100, 1200, 300
    57.     End With
    58.    
    59.     NextLoadLine = 1 'The index number of the next line to load
    60.     SpareLine = 0 'How many lines are loaded but not visible
    61.     NextLoadPicture = 1 'Index number of next picture to load
    62.     SpareLine = 0 'Number of pictures loaded but not visible
    63.     CurLine = 0 'Default to no current line
    64.     CurPicture = 1 'Default to first picture being current
    65.    
    66.     LoadNewPic 'Load the first picture
    67. End Sub
    68.  
    69. Private Sub Command1_Click()
    70.     LoadNewPic 'Load new pictures
    71. End Sub
    72.  
    73. Private Sub LoadNewPic()
    74.     On Error Resume Next
    75.     Picture2(CurPicture).BackColor = vbRed 'Set current picture to not current colour
    76.     If SparePicture > 0 Then 'Have we got any spare picture boxes, loaded but not visible
    77.         For lCounter = 1 To Picture2.UBound 'If so, lets loop thru and find one
    78.             With Picture2(lCounter)
    79.                 If Not .Visible Then 'Find first non visible picture
    80.                     .Move 0, 0 'Move it to top left
    81.                     .Visible = True 'Show it
    82.                     .ZOrder 'Bring it to front
    83.                     .BackColor = vbGreen 'Set colour to current picture
    84.                     PictureConn(lCounter) = 0 'Start with no line connections
    85.                     SparePicture = SparePicture - 1 'Reduce number of spare pictures
    86.                     CurPicture = lCounter 'Set this as the current picture
    87.                     Exit For
    88.                 End If
    89.             End With
    90.         Next
    91.     Else
    92.         Load Picture2(NextLoadPicture) 'Load a new picture
    93.         With Picture2(NextLoadPicture)
    94.             .Move 0, 0 'Move to top left
    95.             .Visible = True 'Set to visible
    96.             .ZOrder 'Bring to front
    97.             .BackColor = vbGreen 'Set colour to current picture
    98.         End With
    99.         CurPicture = NextLoadPicture 'Set this as the current picture
    100.         ReDim Preserve PictureConn(1 To NextLoadPicture) 'Increase array for connections
    101.         NextLoadPicture = NextLoadPicture + 1 'Number of next picture to load
    102.     End If
    103. End Sub
    104.  
    105. Private Sub Command2_Click()
    106.     DeletePicture CurPicture 'Delete green or current picture
    107.     CurPicture = 0 'Set to no current picture
    108. End Sub
    109.  
    110. Private Sub DeletePicture(ByVal PicToDelete As Integer)
    111.     If PicToDelete > 0 Then 'Check valid picture
    112.         Picture2(PicToDelete).Visible = False 'Hide it
    113.         SparePicture = SparePicture + 1 'Add to count of spare pictures ie hidden but still loaded
    114.         If PictureConn(PicToDelete) > 0 Then 'Did that picture have any connecting lines?
    115.             RemoveLinesFromDelPic PicToDelete 'Remove all lines connected to this picture
    116.             PictureConn(PicToDelete) = 0 'Set to having no connecting lines
    117.         End If
    118.     End If
    119. End Sub
    120.  
    121. Private Sub RemoveLinesFromDelPic(ByVal PicToDelete As Integer)
    122.     For lCounter = 1 To UBound(LineConn) 'Loop through all lines
    123.         With LineConn(lCounter)
    124.             If .StartPic = PicToDelete Then 'Is this line connected to deleted picture?
    125.                 Line1(lCounter).Visible = False 'Hide it
    126.                 PictureConn(.EndPic) = PictureConn(.EndPic) - 1 'Reduce the count of connections for the other picture
    127.                 .StartPic = 0: .EndPic = 0 'Set to no connect
    128.             ElseIf .EndPic = PicToDelete Then 'Same again but with opposite end of line
    129.                 Line1(lCounter).Visible = False
    130.                 PictureConn(.StartPic) = PictureConn(.StartPic) - 1
    131.                 .StartPic = 0: .EndPic = 0
    132.             End If
    133.         End With
    134.     Next
    135. End Sub
    Attached Files Attached Files
    Stuart Laidlaw
    Brightspark Financial Software
    http://www.gstsmartbook.com

  4. #4
    PowerPoster beachbum's Avatar
    Join Date
    Jul 2001
    Location
    Wollongong, NSW, Australia
    Posts
    2,274
    VB Code:
    1. 'This routine determines if picture is being dragged or having connecting lines drawn or hidden
    2. Private Sub Picture2_MouseDown(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single)
    3.     If CurPicture > 0 Then Picture2(CurPicture).BackColor = vbRed 'Make old current pic red
    4.     CurPicture = Index 'Make this picture the current pic
    5.     Picture2(CurPicture).BackColor = vbGreen 'and colour it green
    6.     If Button = vbLeftButton Then 'User is pressing left mouse button?
    7.         'Check for holding shift key down. This indicates that user wants to connect
    8.         'boxes. But also check that there is a box to connect to
    9.         If (Shift And vbShiftMask) = vbShiftMask And NextLoadPicture - SparePicture > 1 Then
    10.             If ConnectStatus = 0 Or ConnectStatus = Index Then 'No connection so far
    11.                 ConnectStatus = Index 'Record the first picture box number for the connection
    12.             Else
    13.                 'The first box has been set above and so this must be the second box
    14.                 ConnectPictures ConnectStatus, Index 'Connect both boxes
    15.                 ConnectStatus = Index 'Set second box as next connection so that user
    16.                 'can click on other boxes and keep connecting lines
    17.             End If
    18.         Else
    19.             ConnectStatus = Index 'Reset connections
    20.             DragStatus = Index 'Record the picture box that is being dragged
    21.             PicOffset.CurX = X 'Remember the mouse location within the box
    22.             PicOffset.CurY = Y
    23.            
    24.             For lCounter = 1 To Picture2.UBound 'Disable all boxes to allow smooth dragging
    25.                 Picture2(lCounter).Enabled = False
    26.             Next
    27.         End If
    28.     End If
    29. End Sub
    30.  
    31. Private Sub ConnectPictures(ByVal Pic1 As Integer, ByVal Pic2 As Integer)
    32.     Dim lFound As Boolean 'Flag to see if these boxes already connected
    33.     If CurLine > 0 Then
    34.         For lCounter = 1 To UBound(LineConn) 'Loop thru lines
    35.             With LineConn(lCounter)
    36.                 If (.StartPic = Pic1 And .EndPic = Pic2) Or (.StartPic = Pic2 And .EndPic = Pic1) Then
    37.                     'Already connected so remove connection
    38.                     PictureConn(.StartPic) = PictureConn(.StartPic) - 1 'Reduce connection count
    39.                     PictureConn(.EndPic) = PictureConn(.EndPic) - 1 'Reduce connection count
    40.                     .StartPic = 0: .EndPic = 0 'Reset connection values
    41.                     SpareLine = SpareLine + 1 'Add to count of lines hidden but loaded
    42.                     Line1(lCounter).Visible = False 'Hide the line
    43.                     lFound = True 'Record as being already connected so dont do again
    44.                     Exit For
    45.                 End If
    46.             End With
    47.         Next
    48.     End If
    49.            
    50.     If Not lFound Then 'Not found so connect
    51.         LoadNewLine 'Load a line
    52.         With LineConn(CurLine)
    53.             .StartPic = Pic1 'Record the pictures that this line connects
    54.             .EndPic = Pic2
    55.             PictureConn(.StartPic) = PictureConn(.StartPic) + 1 'Add to the number of connections
    56.             PictureConn(.EndPic) = PictureConn(.EndPic) + 1 'that each picture has
    57.         End With
    58.         DrawLine CurLine, Pic1, Pic2 'Draw the line
    59.         Line1(CurLine).Visible = True 'Make it visible
    60.     End If
    61. End Sub
    62.  
    63. Private Sub LoadNewLine()
    64. 'This sub is practically the same as the LoadNewPic sub
    65.     On Error Resume Next
    66.     If SpareLine > 0 Then 'If there is already a line then use it
    67.         For lCounter = 1 To Line1.UBound 'Loop thru lines
    68.             With Line1(lCounter)
    69.                 If Not .Visible Then 'Found one that is loaded but not visible
    70.                     .Visible = True 'Make it visible
    71.                     .ZOrder 1 'All lines to back
    72.                     LineConn(lCounter).StartPic = 0 'Default to no connections
    73.                     LineConn(lCounter).EndPic = 0
    74.                     CurLine = lCounter 'Set as current line
    75.                     SpareLine = SpareLine - 1 'Remove one from stack of loaded hidden lines
    76.                     Exit For
    77.                 End If
    78.             End With
    79.         Next
    80.     Else
    81.         Load Line1(NextLoadLine) 'Load a new line
    82.         With Line1(NextLoadLine)
    83.             .Visible = True 'Make visible
    84.             .ZOrder 1 'Put to back
    85.         End With
    86.         ReDim Preserve LineConn(1 To NextLoadLine) 'Resize array recording connections
    87.         LineConn(NextLoadLine).StartPic = 0 'Default to no connections
    88.         LineConn(NextLoadLine).EndPic = 0
    89.         CurLine = NextLoadLine 'Set as current line
    90.         NextLoadLine = NextLoadLine + 1 'update number of next line to be loaded
    91.     End If
    92. End Sub
    93.  
    94. Private Sub DrawLine(ByVal LineNum As Integer, ByVal Pic1 As Integer, ByVal Pic2 As Integer)
    95.     'This subroutine actually draws the lines from the centre of the first pic
    96.     'to the centre of the second pic. The use of width / height etc allows the
    97.     'addition of code to resize picture boxes without modifying this sub
    98.     On Error Resume Next
    99.     With Picture2(Pic1)
    100.         Line1(LineNum).X1 = .Left + .Width \ 2
    101.         Line1(LineNum).Y1 = .Top + .Height \ 2
    102.     End With
    103.    
    104.     With Picture2(Pic2)
    105.         Line1(LineNum).X2 = .Left + .Width \ 2
    106.         Line1(LineNum).Y2 = .Top + .Height \ 2
    107.     End With
    108. End Sub
    109.  
    110. Private Sub ReDrawConnections(ByVal PicThatMoved As Integer)
    111.     'Redraw the lines if a picture box moves
    112.     On Error Resume Next
    113.     For lCounter = 1 To UBound(LineConn)
    114.         With LineConn(lCounter)
    115.             'Find all lines that are connected to this picture box
    116.             If .StartPic = PicThatMoved Or .EndPic = PicThatMoved Then
    117.                 DrawLine lCounter, .StartPic, .EndPic 'Redraw them
    118.             End If
    119.         End With
    120.     Next
    121. End Sub
    122.  
    123. Private Sub Picture1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    124. 'User is moving / dragging a picture box
    125.     Dim LeftPosn As Single 'Where to redraw the picture box
    126.     Dim TopPosn As Single
    127.     Dim HitAnEdge As Boolean 'User has dragged too far.. this can prob be handled better
    128.     If Button = vbLeftButton And DragStatus > 0 Then 'Leftbutton down and dragging
    129.         With PicOffset
    130.             LeftPosn = X - .CurX 'Get left position and then check for boundaries
    131.             If LeftPosn < -40 Then LeftPosn = 0: HitAnEdge = True
    132.             If LeftPosn + Picture2(DragStatus).Width > Picture1.Width + 40 Then LeftPosn = Picture1.Width - Picture2(DragStatus).Width: HitAnEdge = True
    133.             TopPosn = Y - .CurY 'Get top position and then check for boundaries
    134.             If TopPosn < -40 Then TopPosn = 0: HitAnEdge = True
    135.             If TopPosn + Picture2(DragStatus).Height > Picture1.Height + 40 Then TopPosn = Picture1.Height - Picture2(DragStatus).Height: HitAnEdge = True
    136.             Picture2(DragStatus).Move LeftPosn, TopPosn
    137.         End With
    138.         If PictureConn(DragStatus) > 0 Then ReDrawConnections DragStatus 'Redraw lines
    139.         If HitAnEdge Then DragStatus = 0 'Reset to no dragging if hit an edge
    140.     End If
    141. End Sub
    142.  
    143. Private Sub Picture1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
    144.     On Error Resume Next
    145.     DragStatus = 0
    146.     For lCounter = 1 To Picture2.UBound
    147.         Picture2(lCounter).Enabled = True 'Reenable all pics when dragging finished
    148.     Next
    149. End Sub
    Stuart Laidlaw
    Brightspark Financial Software
    http://www.gstsmartbook.com

  5. #5
    PowerPoster beachbum's Avatar
    Join Date
    Jul 2001
    Location
    Wollongong, NSW, Australia
    Posts
    2,274
    This is one dang funky program ... load a few picture boxes (using the command button) and then move them around the big picture box. Connect each picture box to every other picture box and then start dragging them around for some cool elastic effects
    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