Results 1 to 6 of 6

Thread: [RESOLVED] Adding VB.Line (This code worked but now it does'nt)

  1. #1

    Thread Starter
    Hyperactive Member stilekid007's Avatar
    Join Date
    Apr 2005
    Location
    DUDE! I'm your neighbor! HELLO!!!
    Posts
    388

    Resolved [RESOLVED] Adding VB.Line (This code worked but now it does'nt)

    Hello,

    I am using the code below to draw a vb.line in my program. I have been using this code for a long time now but now that I added a some more features to my program the code does'nt work, and I get the error "Runtime Error '727': There is already a control named 'Line1'". Can someone tell me why this would happen?

    Here is the code I am using to draw the vb line.

    VB Code:
    1. Option Explicit
    2.  
    3. Private m_InDrawMode        As Boolean
    4. Private m_ActivateDrawmode  As Boolean
    5. Private mblnFirstLineDone   As Boolean
    6. Public avatar As String
    7. Private m_Lines()           As Line
    8.  
    9.  
    10. Private Sub Command1_Click()
    11.     If (Not m_InDrawMode) Then _
    12.         m_ActivateDrawmode = Not m_ActivateDrawmode
    13. End Sub
    14.  
    15. Private Sub Picture1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    16.  
    17.     If (m_ActivateDrawmode And Not m_InDrawMode) Then
    18.         'create a new line and initialise it at the current mouse pointer
    19.        
    20.         If (mblnFirstLineDone) Then
    21.             ReDim Preserve m_Lines(UBound(m_Lines) + 1)
    22.           Else
    23.             mblnFirstLineDone = True
    24.         End If
    25.        
    26.         Set m_Lines(UBound(m_Lines)) = Me.Controls.Add("VB.Line", "Line" & CStr(UBound(m_Lines)), Picture1)
    27.  
    28.         With m_Lines(UBound(m_Lines))
    29.             .X1 = X
    30.             .X2 = X
    31.             .Y1 = Y
    32.             If (UBound(m_Lines)) Then
    33.                 .Y2 = X + m_Lines(UBound(m_Lines) - 1).Y2 / m_Lines(UBound(m_Lines) - 1).X2
    34.               Else
    35.                 .Y2 = Y
    36.             End If
    37.             .Visible = True
    38.         .BorderColor = &H80000002
    39.         End With
    40.        
    41.         m_InDrawMode = True
    42.     End If
    43. End If
    44. End Sub
    45.  
    46. Private Sub Picture1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    47.   If m_InDrawMode Then    'move the end point of the line
    48.         With m_Lines(UBound(m_Lines))
    49.        
    50.             ' If it is the first line, allow user to change angle
    51.             If (UBound(m_Lines) = 0) Then
    52.                 .Y2 = Y
    53.                
    54.               Else
    55.                 ' If it is a subsequent line, force the angle
    56.                 ' to be the same
    57.                
    58.                 With m_Lines(0)                         ' Slope is of 1st line
    59.                
    60.                     ' y = mx + c
    61.                     ' m = (y2-y1)/(x2-x1) + c
    62.                     ' ... y = ((y2-y1)/(x2-x1))x + c
    63.                    
    64.                     Dim m As Double, c As Double
    65.                    
    66.                     m = ((.Y2 - .Y1) / (.X2 - .X1))
    67.                     c = m_Lines(UBound(m_Lines)).Y1
    68.                    
    69.                     ' x1 = (X - x0)
    70.                    
    71.                     m_Lines(UBound(m_Lines)).Y2 = _
    72.                         (m * (X - m_Lines(UBound(m_Lines)).X1)) + c
    73.                 End With
    74.                
    75.             End If
    76.            
    77.             .X2 = X
    78.         End With
    79.     End If
    80. End Sub
    81.  
    82. Private Sub Picture1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
    83.     m_InDrawMode = False
    84.     m_ActivateDrawmode = False
    85.  
    86. End Sub
    87.  
    88. Private Sub Form_Load()
    89.     ReDim m_Lines(0)
    90.     Me.ScaleMode = vbPixels
    91. End Sub
    Quote Originally Posted by stilekid007
    I RATE ALL HELPFULL POSTS!

  2. #2
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Adding VB.Line (This code worked but now it does'nt)

    As far as I can see you're trying to create new instance of an existing control.
    Here is your problem: m_Lines(UBound(m_Lines)) indicates control array. New member of a control array MUST be LOADed using Load statement and NOT ADD method of a controls collection - VB doesn't allow that.
    So, having said that the following is what you might need:
    VB Code:
    1. Load m_Lines(UBound(m_Lines) + 1) 'instead of Set m_Lines(UBound(m_Lines)) ...

  3. #3

    Thread Starter
    Hyperactive Member stilekid007's Avatar
    Join Date
    Apr 2005
    Location
    DUDE! I'm your neighbor! HELLO!!!
    Posts
    388

    Re: Adding VB.Line (This code worked but now it does'nt)

    Hello Rhino,

    Hmm I got excited there for a moment lol but then it didn't work. I get the error "Subscrit out of range."

    Do you happen to know why?

    I am awfull when it comes to error messages. But if I am the original programer of the code it is a lot easier because I would run into the error and know what it is.

    Thank you so much for your reply!
    Stilekid007
    Quote Originally Posted by stilekid007
    I RATE ALL HELPFULL POSTS!

  4. #4
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Adding VB.Line (This code worked but now it does'nt)

    Well, looking at your code again I noticed that m_Lines is ordinary array so what I suggest is paste one Line control onto your form (or directly onto the picturebox) and set its Index = 0 in design (use Properties window) so first member of control array is created. Then you can do something similar to what i've posted previously.

  5. #5

    Thread Starter
    Hyperactive Member stilekid007's Avatar
    Join Date
    Apr 2005
    Location
    DUDE! I'm your neighbor! HELLO!!!
    Posts
    388

    Re: Adding VB.Line (This code worked but now it does'nt)

    Ok - this is all my bad! I was messing with the index and I noticed when I would add a new line it would name it Line2 meaning there was a Line1 already on board! So I looked and there was a line behind a label! So I removed that line and now it all works. Because my code was adding the Line named Line1 and there was already a Line1 created!

    Thank you for your help Rhino I really appreciate your code and help!

    Stilekid007
    Quote Originally Posted by stilekid007
    I RATE ALL HELPFULL POSTS!

  6. #6
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: [RESOLVED] Adding VB.Line (This code worked but now it does'nt)

    No problem.

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