Results 1 to 9 of 9

Thread: [RESOLVED] Deleteing vb lines that have an array.

  1. #1

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

    Resolved [RESOLVED] Deleteing vb lines that have an array.

    Hello,

    I am using the code below to add vb.Lines to my form.

    My question is how do I delete the lines?

    I tried me.Controls.Remove q_lines but that doesn't delete anymore then one line.

    So if the user created 2 or even 10 lines how would I delete them?

    Thank you and have a great!

    Stilekid007

    VB Code:
    1. Private q_InDrawMode As Boolean
    2. Private q_ActivateDrawmode As Boolean
    3. Private q_Lines As Line
    4.  
    5.  
    6. Private Sub Command1_Click()
    7. q_ActivateDrawmode = True
    8. End Sub
    9.  
    10.  
    11. Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    12.      Static LineNumber As Long
    13.      LineNumber = LineNumber + 1
    14.    
    15.      If q_ActivateDrawmode = True Then
    16.         'create a new line and initialise it at the current mouse pointer
    17.      
    18.          Set q_Lines = Me.Controls.Add("VB.Line", "Line" & CStr(LineNumber), Form1)
    19.          q_Lines.X1 = X
    20.          q_Lines.X2 = X
    21.          q_Lines.Y1 = Y
    22.          q_Lines.Y2 = Y
    23.          q_Lines.Visible = True
    24.          q_InDrawMode = True
    25.     End If
    26.  
    27. End Sub
    28.  
    29. Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    30.         If q_InDrawMode Then
    31.         'move the end point of the line
    32.         q_Lines.X2 = X
    33.         q_Lines.Y2 = Y
    34.     End If
    35.  
    36. End Sub
    37.  
    38. Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
    39.     q_InDrawMode = False
    40.     q_ActivateDrawmode = False
    41. End Sub
    Quote Originally Posted by stilekid007
    I RATE ALL HELPFULL POSTS!

  2. #2
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177

    Re: Deleteing vb lines that have an array.

    You need to enumerate the Controls collection and find the instances of the Line control you want to remove, i.e.
    VB Code:
    1. Private Sub Command2_Click()
    2.   Dim oControl As Control
    3.   Dim colLines As New Collection
    4.  
    5.   ' Create a collection of the Line controls to delete
    6.   For Each oControl In Me.Controls
    7.     If TypeOf oControl Is Line And Left(oControl.Name, 4) = "Line" Then
    8.       colLines.Add oControl
    9.     End If
    10.   Next
    11.  
    12.   ' Enumerate the collection of lines and remove them
    13.   For Each oControl In colLines
    14.     Me.Controls.Remove oControl
    15.   Next
    16. End Sub
    Regards,

    - Aaron.

  3. #3
    Fanatic Member vbasicgirl's Avatar
    Join Date
    Jan 2004
    Location
    Manchester, UK
    Posts
    1,016

    Re: Deleteing vb lines that have an array.

    another way is to put the variable Linenumber and the top of your code instead of using a static so you could do this
    VB Code:
    1. Dim i As Integer
    2.  
    3.  For i = 1 To LineNumber
    4.   Me.Controls.Remove "Line" & i
    5.  Next i
    6.  
    7.  LineNumber = 0

    casey.

  4. #4

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

    Re: Deleteing vb lines that have an array.

    HMMMMMM! For some wierd reason niether of those suggestion codes worked! Did you both try your codes?

    I appreciate your time and help!
    Stilekid007
    Quote Originally Posted by stilekid007
    I RATE ALL HELPFULL POSTS!

  5. #5
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177

    Re: Deleteing vb lines that have an array.

    Yes, my code worked fine when I used it against the example code you posted.

    Regards,

    - Aaron.

  6. #6
    Fanatic Member vbasicgirl's Avatar
    Join Date
    Jan 2004
    Location
    Manchester, UK
    Posts
    1,016

    Re: Deleteing vb lines that have an array.

    i did test it but i forgot to say you would also need to move the line
    LineNumber = LineNumber + 1

    it needs to be within the if statement

    If q_ActivateDrawmode = True Then
    LineNumber = LineNumber + 1

    casey.

  7. #7

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

    Re: Deleteing vb lines that have an array.

    Hello,

    Thank you for your responses!

    Aaron - Your code worked great! But with your code, if I change the name of my Line from "Line" to "ParLine" then how would I delete it?

    Example:
    Instead of creating the Line with this code:
    VB Code:
    1. Set q_Lines = Me.Controls.Add("VB.Line", "Line" & CStr(LineNumber), Form1)

    It would be:
    VB Code:
    1. Set q_Lines = Me.Controls.Add("VB.Line", "ParLine" & CStr(ParLineNumber), Form1)

    How would this change the code you provded?

    Thank you so much and have a great day!
    Stilekid007
    Quote Originally Posted by stilekid007
    I RATE ALL HELPFULL POSTS!

  8. #8
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177

    Re: Deleteing vb lines that have an array.

    Just change the line that checks the name of the control to be whatever you want to use.

    You could even remove the check altogether if you don't intend to use Lines for anything else.

    VB Code:
    1. ...
    2. If TypeOf oControl Is Line And Left(oControl.Name, 7) = "ParLine" Then
    3. ...
    Regards,

    - Aaron.

  9. #9

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

    Re: Deleteing vb lines that have an array.

    Ahh Yes! Thank you sir! I appreciate all your help!

    Have a great day!
    Stilekid007
    Quote Originally Posted by stilekid007
    I RATE ALL HELPFULL POSTS!

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