[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:
Option Explicit
Private m_InDrawMode As Boolean
Private m_ActivateDrawmode As Boolean
Private mblnFirstLineDone As Boolean
Public avatar As String
Private m_Lines() As Line
Private Sub Command1_Click()
If (Not m_InDrawMode) Then _
m_ActivateDrawmode = Not m_ActivateDrawmode
End Sub
Private Sub Picture1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If (m_ActivateDrawmode And Not m_InDrawMode) Then
'create a new line and initialise it at the current mouse pointer
If (mblnFirstLineDone) Then
ReDim Preserve m_Lines(UBound(m_Lines) + 1)
Else
mblnFirstLineDone = True
End If
Set m_Lines(UBound(m_Lines)) = Me.Controls.Add("VB.Line", "Line" & CStr(UBound(m_Lines)), Picture1)
With m_Lines(UBound(m_Lines))
.X1 = X
.X2 = X
.Y1 = Y
If (UBound(m_Lines)) Then
.Y2 = X + m_Lines(UBound(m_Lines) - 1).Y2 / m_Lines(UBound(m_Lines) - 1).X2
Else
.Y2 = Y
End If
.Visible = True
.BorderColor = &H80000002
End With
m_InDrawMode = True
End If
End If
End Sub
Private Sub Picture1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If m_InDrawMode Then 'move the end point of the line
With m_Lines(UBound(m_Lines))
' If it is the first line, allow user to change angle
If (UBound(m_Lines) = 0) Then
.Y2 = Y
Else
' If it is a subsequent line, force the angle
' to be the same
With m_Lines(0) ' Slope is of 1st line
' y = mx + c
' m = (y2-y1)/(x2-x1) + c
' ... y = ((y2-y1)/(x2-x1))x + c
Dim m As Double, c As Double
m = ((.Y2 - .Y1) / (.X2 - .X1))
c = m_Lines(UBound(m_Lines)).Y1
' x1 = (X - x0)
m_Lines(UBound(m_Lines)).Y2 = _
(m * (X - m_Lines(UBound(m_Lines)).X1)) + c
End With
End If
.X2 = X
End With
End If
End Sub
Private Sub Picture1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
m_InDrawMode = False
m_ActivateDrawmode = False
End Sub
Private Sub Form_Load()
ReDim m_Lines(0)
Me.ScaleMode = vbPixels
End Sub
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:
Load m_Lines(UBound(m_Lines) + 1) 'instead of Set m_Lines(UBound(m_Lines)) ...
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
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.
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
Re: [RESOLVED] Adding VB.Line (This code worked but now it does'nt)