Results 1 to 4 of 4

Thread: [RESOLVED] Saving and Loading arrays? in VB6

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2009
    Posts
    4

    [RESOLVED] Saving and Loading arrays? in VB6

    Hello all!
    I'm new here, in case you couldn't tell. I'm also relatively new to Visual Basic 6.0. So, I apologize if this should have gone in the "Games and Graphic Programming" section or something!

    Anyway, let's get down to brass tacks.

    I'm working on a MS Paint type program (for a class) and I'm having some trouble saving and loading. This is done using arrays-the program tracks your mouse movements and saves them in an array, then re-draws them when you load. I've done everything I think I'm supposed to--but I end up with a line going from the top left corner to the last place where I mouse-up'd.

    Paint Project Code:
    1. Dim flag As Integer
    2. Dim path As String, i As Integer
    3. Dim gsngx(1 To 3000) As Single
    4. Dim gsngy(1 To 3000) As Single
    5. Dim gnnumpoints As Long
    6.  
    7. Sub drawlines()
    8. CurrentX = gsngx(1)
    9. CurrentY = gsngy(1)
    10. Circle (gsngx(1), gsngy(1)), 50
    11. For i = 2 To gnnumpoints
    12.     Line -(gsngx(i), gsngy(i))
    13.     Circle (gsngx(i), gsngy(i)), 50
    14. Next i
    15. End Sub
    16.  
    17. Sub Drawcircle(x As Single, y As Single)
    18. Circle (x, y), 50
    19. If gnnumpoints < 3000 Then
    20.     gnnumpoints = gnnumpoints + 1
    21.     gsngx(gnnumpoints) = x
    22.     gsngy(gnnumpoints) = y
    23. End If
    24. End Sub
    25.  
    26.  
    27. Public Sub Form_Load()
    28. Dim i As Integer
    29. flag = 0
    30. r = 0
    31. g = 0
    32. b = 0
    33. End Sub
    34.  
    35. Private Sub mnuopen_Click()
    36. path = UCase$(Trim$(InputBox("Filename", "OpenFile")))
    37. path = "H:\" & path & ".dat"
    38. Open path For Binary Access Read As #1
    39.     Get #1, , gnnumpoints
    40.     For i = 1 To gnnumpoints
    41.         Get #1, , gsngx(i)
    42.         Get #1, , gsngy(i)
    43.         drawlines
    44.     Next i
    45. Close #1
    46. End Sub
    47.  
    48. Private Sub Form_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
    49. flag = 1
    50. Drawcircle x, y
    51. End Sub
    52.  
    53.  
    54. Private Sub Form_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)
    55. If flag = 1 Then
    56.    Form1.Line -(x, y), RGB(r, g, b)
    57.    Drawcircle x, y
    58. End If
    59. End Sub
    60.  
    61. Private Sub Form_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single)
    62. flag = 0
    63. End Sub
    64. Private Sub mnusave_Click()
    65. path = UCase$(Trim$(InputBox("Enter name:", "Save", "bob")))
    66. path = "H:\" + path + ".dat"
    67. Open path For Binary Access Write As #1
    68. Put #1, , gnnumpoints
    69. For i = 1 To gnnumpoints
    70.     Put #1, , gsngx(i)
    71.     Put #1, , gsngy(i)
    72. Next i
    73. Close #1
    74. End Sub

    I've cleared out some non-important stuff. Code was UNGODLY long.

    I have no doubt there's an easier way to go about doing this-maybe a picturebox or something?-but this is how i'm supposed to do it. Like I said, it's for a class.

    Can anyone help?
    Last edited by XelaisPWN; May 20th, 2009 at 07:21 AM. Reason: RESOLVED!

  2. #2

    Thread Starter
    New Member
    Join Date
    May 2009
    Posts
    4

    Re: Saving and Loading arrays? in VB6

    One last thing--ignore the color stuff (ex. RGB(r, g, b)). I have a module and a second form and stuff for that... and once I get this saving and loading stuff down, i'll work on getting that to save.

  3. #3
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: Saving and Loading arrays? in VB6

    You are using the same variable (i) as the counter in two For Loops. And since one Loop calls the other loop, the first loop will never iterate through all the elements. This happens in the mnuOpen code. Even though the following sample is impossible in a single procedure it demonstrates the problem

    For i = 1 to 2
    For i = 1 to 10
    Next
    Next

    with the above i will equal 11 after the inner loop exits. The outer loop will only execute once, missing an iteration.

    Anyways the problem can be solved by moving the call to the DrawLines function in mnuOpen_Click outside the For Loop. Basically, load the entire array from the file and then call DrawLines.

    Code:
    Private Sub mnuopen_Click()
        path = UCase$(Trim$(InputBox("Filename", "OpenFile")))
        path = "C:\" & path & ".dat"
        Open path For Binary Access Read As #1
            Get #1, , gnnumpoints
            For i = 1 To gnnumpoints
                Get #1, , gsngx(i)
                Get #1, , gsngy(i)
                'drawlines
            Next i
        Close #1
        Drawlines
    
    End Sub

  4. #4

    Thread Starter
    New Member
    Join Date
    May 2009
    Posts
    4

    Re: Saving and Loading arrays? in VB6

    Oh my god--thank you so much! You're a friggin' livesaver!

Tags for this Thread

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