|
-
May 19th, 2009, 04:44 PM
#1
Thread Starter
New Member
[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:
Dim flag As Integer
Dim path As String, i As Integer
Dim gsngx(1 To 3000) As Single
Dim gsngy(1 To 3000) As Single
Dim gnnumpoints As Long
Sub drawlines()
CurrentX = gsngx(1)
CurrentY = gsngy(1)
Circle (gsngx(1), gsngy(1)), 50
For i = 2 To gnnumpoints
Line -(gsngx(i), gsngy(i))
Circle (gsngx(i), gsngy(i)), 50
Next i
End Sub
Sub Drawcircle(x As Single, y As Single)
Circle (x, y), 50
If gnnumpoints < 3000 Then
gnnumpoints = gnnumpoints + 1
gsngx(gnnumpoints) = x
gsngy(gnnumpoints) = y
End If
End Sub
Public Sub Form_Load()
Dim i As Integer
flag = 0
r = 0
g = 0
b = 0
End Sub
Private Sub mnuopen_Click()
path = UCase$(Trim$(InputBox("Filename", "OpenFile")))
path = "H:\" & 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
End Sub
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
flag = 1
Drawcircle x, y
End Sub
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)
If flag = 1 Then
Form1.Line -(x, y), RGB(r, g, b)
Drawcircle x, y
End If
End Sub
Private Sub Form_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single)
flag = 0
End Sub
Private Sub mnusave_Click()
path = UCase$(Trim$(InputBox("Enter name:", "Save", "bob")))
path = "H:\" + path + ".dat"
Open path For Binary Access Write As #1
Put #1, , gnnumpoints
For i = 1 To gnnumpoints
Put #1, , gsngx(i)
Put #1, , gsngy(i)
Next i
Close #1
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!
-
May 19th, 2009, 05:10 PM
#2
Thread Starter
New Member
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.
-
May 20th, 2009, 12:05 AM
#3
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
-
May 20th, 2009, 07:15 AM
#4
Thread Starter
New Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|