Results 1 to 10 of 10

Thread: Opening a saved file to then put values into specific labels

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Nov 2016
    Posts
    95

    Opening a saved file to then put values into specific labels

    Hello, I am creating a game for my final project and my teacher requires us to have a save and open dialog in the program. Well, I have the save done. Here is the code to probably help with the issue:

    Code:
            File.WriteAllText(SaveFileDialog.FileName, PlayerName & vbNewLine & PlayerHealth & vbNewLine & PlayerCash & vbNewLine & PlayerInfantry & vbNewLine & PlayerTanks & vbNewLine & PlayerJet & vbNewLine & PlayerMissile & vbNewLine & PlayerNuke & vbNewLine & PlayerRepairsLeft & vbNewLine & AiName & vbNewLine & AiCash & vbNewLine & AiInfantry & vbNewLine & AiTanks & vbNewLine & AiJet & vbNewLine & AiMissile & vbNewLine & AiNuke & vbNewLine & AiRepairsLeft)
    That is the order. Each one represents a value. Here is an image of where those values need to go:
    Name:  fdss.jpg
Views: 418
Size:  44.1 KB

    I do not know how to do this as I have never done it like this before. Hopefully I explained everything well. Thanks!

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Opening a saved file to then put values into specific labels

    It would be more appropriate to put all those values into an array and then call File.WriteAllLines. Similarly, you can then call File.ReadAllLines to read the data back into an array. You can then do whatever you like with each array element.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Nov 2016
    Posts
    95

    Re: Opening a saved file to then put values into specific labels

    Quote Originally Posted by jmcilhinney View Post
    It would be more appropriate to put all those values into an array and then call File.WriteAllLines. Similarly, you can then call File.ReadAllLines to read the data back into an array. You can then do whatever you like with each array element.
    Then I could put after the readalllines this:
    lblInfantry.Text = values(1)

    And it would change the current value of that label into the new one it is reading?

  4. #4
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,598

    Re: Opening a saved file to then put values into specific labels

    Why not try it and let us all know.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Nov 2016
    Posts
    95

    Re: Opening a saved file to then put values into specific labels

    Excuse me, sorry for asking but it says "Can not convert from String to Encoding"
    Code:
            File.WriteAllLines(SaveFileDialog.FileName, ValuesString(0) & vbNewLine & values(0) & vbNewLine & values(1) & vbNewLine & values(2) & vbNewLine & values(3) & vbNewLine & values(4) & vbNewLine & values(5) & vbNewLine & values(6) & vbNewLine & values(7) & vbNewLine & ValuesString(1) & vbNewLine & values(8) & vbNewLine & values(9) & vbNewLine & values(10) & vbNewLine & values(11) & vbNewLine & values(12) & vbNewLine & values(13) & vbNewLine & values(14) & vbNewLine & values(15))

  6. #6
    Frenzied Member
    Join Date
    Dec 2014
    Location
    VB6 dinosaur land
    Posts
    1,190

    Re: Opening a saved file to then put values into specific labels

    It should be more like
    Code:
    File.WriteAllLines(SaveFileDialog.FileName, values)
    You are trying to create a big long string from your array rather than just passing the array.

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Nov 2016
    Posts
    95

    Re: Opening a saved file to then put values into specific labels

    Alright you have helped me figure out that issue. I saved it instead of .xml to .csv and will do ReadallLines for opening. BUT I new issue has occurred for only this line of code. Now, this is a label storing an Integer value in it but being converted to String like the other labels with integers. Everything else gets converted to String but this 1 line:
    Code:
    values(0) = frmMainGame.lblPlayerCash.Text
    Now that is just another integer but here is the result I get: Name:  Capture.PNG
Views: 212
Size:  12.4 KB I have tried methods like .ToString(), Integer.Parse, Convert.ToString(code here) but none of those methods work. I do not understand why. I have researched it but nothing useful came up. This should not be happening. I am not very experienced which is why I come to the forums. I am trying to learn these issues and fix them myself but I sometimes just have no idea.

  8. #8
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,598

    Re: Opening a saved file to then put values into specific labels

    Those values exceed 2 billion I believe, and technically don't fit in an Integer.
    You probably want to use a Long, aka Int64 to hold those values.

    Perhaps use a Long for everything so you have a common type and handle your two worse (maximum value) cases.

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Nov 2016
    Posts
    95

    Re: Opening a saved file to then put values into specific labels

    So I try that method and it does not save any of the information below it. Reason if you know because I have tried fixing it multiple times.
    Code:
    values(0) = Int64.Parse(frmMainGame.lblPlayerCash.Text)

  10. #10
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,598

    Re: Opening a saved file to then put values into specific labels

    I guess I'm confused.
    What is the values() array's type?
    It seems if you are trying to save strings (writing to a file) from strings (.Text properties), then you shouldn't need any conversions at all.
    The values() array should be an array of strings.

    Perhaps your excel sheet capture you posted is showing Scientific (or engineering) notation for the number because your column is too narrow, or because of the format that you have specified for the cells formats large numbers that way.

    It may just be a display issue, not a value type issue, i.e. it is not an error, just a different representation of the number.
    Is the values array an array of String?

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