CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 3 FirstFirst 123 LastLast
Results 16 to 30 of 35
  1. #16
    Join Date
    Sep 2009
    Posts
    126

    Re: Save game/Load game

    Save code:
    Dim namefile As String
    namefile = Text3
    If Text3.Text = "" Then
    MsgBox "Fill in a Username!"
    Else
    Open "D:\Projects\GR_Manager\Data\" + namefile + ".txt" For Output As #1
    Print #1, Label5 + ";" + Label1 + ";";
    Print #1, Label10 + ";" + Label11 + ";";
    Print #1, Label38 + ";" + Label39 + ";";
    Print #1, Label31 + ";" + Label33 + ";";
    Print #1, Label32 + ";" + Label34 + ";";
    Print #1, Label42 + ";" + Label43 + ";";
    Print #1, Label19 + ";" + Label24 + ";";
    Print #1, Label17 + ";" + Label25 + ";";
    Print #1, Label16 + ";" + Label26 + ";";
    Print #1, Label20 + ";" + Label27 + ";";
    Print #1, Label21 + ";" + Label28 + ";";
    Print #1, Label22 + ";" + Label29 + ";";
    Print #1, Label23 + ";" + Label30 + ";";
    Close #1
    End If

    Load code:

    Dim a$, b$()
    Dim namefile As String
    namefile = Text2
    Open App.Path + "\Data\" + namefile + ".txt" For Input As #1
    Line Input #1, a$
    b = Split(a$, ";")
    Label5.Caption = b(0)
    Label1.Caption = b(1)

    Line Input #1, a$
    b = Split(a$, ";")
    Label10.Caption = b(0)
    Label11.Caption = b(1)

    Line Input #1, a$
    b = Split(a$, ";")
    Label38.Caption = b(0)
    Label39.Caption = b(1)

    Line Input #1, a$
    b = Split(a$, ";")
    Label31.Caption = b(0)
    Label33.Caption = b(1)

    Line Input #1, a$
    b = Split(a$, ";")
    Label32.Caption = b(0)
    Label34.Caption = b(1)

    Line Input #1, a$
    b = Split(a$, ";")
    Label42.Caption = b(0)
    Label43.Caption = b(1)

    Line Input #1, a$
    b = Split(a$, ";")
    Label19.Caption = b(0)
    Label24.Caption = b(1)

    Line Input #1, a$
    b = Split(a$, ";")
    Label17.Caption = b(0)
    Label25.Caption = b(1)

    Line Input #1, a$
    b = Split(a$, ";")
    Label16.Caption = b(0)
    Label26.Caption = b(1)

    Line Input #1, a$
    b = Split(a$, ";")
    Label20.Caption = b(0)
    Label27.Caption = b(1)

    Line Input #1, a$
    b = Split(a$, ";")
    Label21.Caption = b(0)
    Label28.Caption = b(1)

    Line Input #1, a$
    b = Split(a$, ";")
    Label22.Caption = b(0)
    Label29.Caption = b(1)

    Line Input #1, a$
    b = Split(a$, ";")
    Label23.Caption = b(0)
    Label30.Caption = b(1)

    Line Input #1, a$
    b = Split(a$, ";")
    Label10.Caption = b(0)
    Label11.Caption = b(1)

  2. #17
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: Save game/Load game

    Quote Originally Posted by Equx View Post
    I'm noob on codes and VB atm, trying to get better and better.
    Seeing the fact that you are a noob, and trying to learn, I'll give this advice ( not really applicable to your situation, but still very important )

    Names like Label1, Label2, Label3 etc. is extremely difficult to remember. Why do I say that ¿

    Imagine how difficult it is to remember each label's caption, for example :
    On Label 62 you might have text about a certain topic, 2 months from now, how would you remember what Label62 represented ¿

    Give them decent names, each should have a name which should easily identify its purpose

    Another thing here, is that a Form can only hold 255 controls, and is only so big. All your controls on the forms should be nicely organised, you must try to find ways to save form space, and design proper professional forms

  3. #18
    Join Date
    Sep 2009
    Posts
    126

    Re: Save game/Load game

    Quote Originally Posted by HanneSThEGreaT View Post
    Seeing the fact that you are a noob, and trying to learn, I'll give this advice ( not really applicable to your situation, but still very important )

    Names like Label1, Label2, Label3 etc. is extremely difficult to remember. Why do I say that ¿

    Imagine how difficult it is to remember each label's caption, for example :
    On Label 62 you might have text about a certain topic, 2 months from now, how would you remember what Label62 represented ¿

    Give them decent names, each should have a name which should easily identify its purpose

    Another thing here, is that a Form can only hold 255 controls, and is only so big. All your controls on the forms should be nicely organised, you must try to find ways to save form space, and design proper professional forms
    OK thanx for you advice.
    This is my first real project so I need advices.
    The labels are in an special order. I know exactly what label1 is and so on...
    But good advice when I make better programs

  4. #19
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: Save game/Load game

    I doubt, that the aboce code works.
    You do Print #1, Label5 + ";" + Label1 + ";";
    where the trailing ; does not print a complete line.
    For the above reading code to work you must omit the trailing semicolon
    The proper print statement should look like that:
    Print #1, Label5 + ";" + Label1
    All print statements should look like that. With this you get one line printed for each pair of labels.
    Only then you can read them back in line by line with the Line Input statement.

  5. #20
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Save game/Load game

    WOF is correct, the sample where I showed the use of the semi colon would work with the way you were reading the file before but not the way you are reading it in the most recent post.

    Remember the ; at the end of the print statement tells it not to send the new line characters

  6. #21
    Join Date
    Sep 2009
    Posts
    126

    Re: Save game/Load game

    yes but I still get the error '62: Input past end of file

  7. #22
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: Save game/Load game

    That's the reason why.
    Write your Print statements like I have shown you and your reading routine would work.
    Also, examine your file. It should contain individual lines which look like:
    nameofvalue;10000
    It should be one pair in each line.
    Please show us the file if you are in doubt.

  8. #23
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Save game/Load game

    Quote Originally Posted by Equx View Post
    yes but I still get the error '62: Input past end of file
    In your most recent code posted you are writing 1 line but reading more than one line therefore you get input past end when you try to read line 2 as there is no line 2 generated by the code posted.

    To use the read routine in your last post yuou need to remove the trailing semicolons from each print statement. You also should count the print # vs line input statements to make sure you have the same number of Print as Input otherwise you will be reading past end.

  9. #24
    Join Date
    Sep 2009
    Posts
    126

    Re: Save game/Load game

    Quote Originally Posted by WoF View Post
    That's the reason why.
    Write your Print statements like I have shown you and your reading routine would work.
    Also, examine your file. It should contain individual lines which look like:
    nameofvalue;10000
    It should be one pair in each line.
    Please show us the file if you are in doubt.
    Ok I have done that now.
    nameofvalue;10000 ?
    Can you explain exactly how it shall be?

  10. #25
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Save game/Load game

    Delete the last + ";"; in each line, so you get one ; between the parms, and one line of two parms

    Code:
    Open "D:\Projects\GR_Manager\Data\" + namefile + ".txt" For Output As #1
    Print #1, Label5 + ";" + Label1 + ";";
    Print #1, Label10 + ";" + Label11 + ";";
    Print #1, Label38 + ";" + Label39 + ";";
    Print #1, Label31 + ";" + Label33 + ";";
    Print #1, Label32 + ";" + Label34 + ";";
    Print #1, Label42 + ";" + Label43 + ";";
    Print #1, Label19 + ";" + Label24 + ";";
    Print #1, Label17 + ";" + Label25 + ";";
    Print #1, Label16 + ";" + Label26 + ";";
    Print #1, Label20 + ";" + Label27 + ";";
    Print #1, Label21 + ";" + Label28 + ";";
    Print #1, Label22 + ";" + Label29 + ";";
    Print #1, Label23 + ";" + Label30 + ";";
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  11. #26
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: Save game/Load game

    Exactly.
    That's how a Print statement should look like:
    Print #1, Label5 + ";" + Label1
    Change all yhe print statements like that, then write the file and open it with a text editor.
    Look at the lines it contains.
    Then you will understand what I wanted to say with nameofvalue;10000 because the lines will all look similar.
    I presume that in the first label is always some description and in the second one the current value,
    like Label5 says "Points" and Label1 says "10000", reflecting the actual points count.
    So your line 1 in the file would be "Points;10000"
    Last edited by WoF; September 4th, 2009 at 06:27 AM.

  12. #27
    Join Date
    Sep 2009
    Posts
    126

    Re: Save game/Load game

    Quote Originally Posted by WoF View Post
    Exactly.
    That's how a Print statement should look like:
    Print #1, Label5 + ";" + Label1
    Change all yhe print statements like that, then write the file and open it with a text editor.
    Look at the lines it contains.
    Then you will understand what I wanted to say with nameofvalue;10000 because the lines will all look similar.
    I presume that in the first label is always some description and in the second one the current value,
    like Label5 says "Points" and Label1 says "10000", reflecting the actual points count.
    So your line 1 in the file would be "Points;10000"
    Ok.
    My print code looks like this:
    ....
    Print #1, Label5 + ";" + Label1
    ....

    And when I print and open the printed txt file it looks like this:
    Current Money:;535000
    Current Loan:;12000
    .....

  13. #28
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: Save game/Load game

    Thats what I expected.
    Now you try your Load code with the blocks:
    Code:
    Line Input #1, a$
    b = Split(a$, ";")
    Label5.Caption = b(0)
    Label1.Caption = b(1)
    Watch that there are only as many reads as there are lines in the file.
    Then it should work as expected without reading past end of file.

  14. #29
    Join Date
    Sep 2009
    Posts
    126

    Smile Re: Save game/Load game

    Quote Originally Posted by WoF View Post
    Thats what I expected.
    Now you try your Load code with the blocks:
    Code:
    Line Input #1, a$
    b = Split(a$, ";")
    Label5.Caption = b(0)
    Label1.Caption = b(1)
    Watch that there are only as many reads as there are lines in the file.
    Then it should work as expected without reading past end of file.
    Thank you guys!!!
    What would I have done without you?
    Once more, thank you all!

    But one question more:
    I want to open an file by pressing a button.
    The location of the file is for example: D:\Projects\GR_Manager\TrackEditor.exe
    And the code?
    Last edited by Equx; September 4th, 2009 at 11:47 AM.

  15. #30
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Save game/Load game

    You can Shell() a program to open it.
    Code:
    Shell("C:\temp\notepad.exe")
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

Page 2 of 3 FirstFirst 123 LastLast

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

Featured