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