Having Some Issues
Hi there. I'm having some issues with the ListView (and a couple of other things) that I can't seem to figure out. I've been playing with it :BangHead: but I just don't see why it isn't working.
I'm developing a little game for my wife, and I'm currently just trying to populate a high-score list from a text file. However, when I execute the code, my ListView constantly comes up blank.
Sub SetHighScores
Dim PlayerName As String
Dim PlayerScore As String
Dim I As Int
lstHighScores.Initialize("lstHighScores")
'lstHighScores.Color = Colors.Transparent
'lstHighScores.Clear
If File.Exists(File.DirInternal, "HighScores.txt") = False Then
Dim TW As TextWriter
PlayerScore = "75,000"
PlayerName = "G_HOSA_PHAT"
For I = 0 To 9
TW.Initialize(File.OpenOutput(File.DirInternal, "HighScores.txt", False))
TW.WriteLine(PlayerScore)
TW.WriteLine(PlayerName)
lstHighScores.AddTwoLines(PlayerScore, PlayerName)
Next
TW.Close
Else
Dim TR As TextReader
TR.Initialize(File.OpenInput(File.DirInternal, "HighScores.txt"))
For I = 0 To 9
PlayerScore = TR.ReadLine
PlayerName = TR.ReadLine
lstHighScores.AddTwoLines(PlayerScore, PlayerName)
Next
TR.Close
End If
End Sub
Am I just being an idiot (probably), or what?
EDIT: Yep, I'm an idiot (to some extent). I commented out the Initialize line (because I already have the ListView object defined in my Designer), and tried again. Now I'm showing one line, with only the first entry (maybe because they're all the same?), but at least I've got it populated with something now. I'm sure just a little more work and I can add them all in there.
EDIT 2: Okay, so I deleted the text file and started over. When I ran it this time, it recreated the file and populated the list with 10 copies of the same thing (as it should have). Unfortunately, when I ran it again, I come up with the same results as before. One line in the ListView with text, and 9 blank lines.
So, I added a Select Case on the file creation piece to add an ordinal rank to each entry (1ST PLACE, 2ND PLACE, etc.) and tried again, and it worked this time. So, there we go. I think I've got it worked out myself.
I still have to work through the actual replacement of entries when a new high score goes in there, but I got the ListView part of my issue worked out. Here's what I ended up with:
Sub SetHighScores
Dim PlayerName As String
Dim PlayerScore As String
Dim ScoreTemp As Long
Dim I As Int
If File.Exists(File.DirInternal, "HighScores.txt") = True Then File.Delete(File.DirInternal, "HighScores.txt")
If File.Exists(File.DirInternal, "HighScores.txt") = False Then
Dim PlayerRank As String
Dim TW As TextWriter
PlayerScore = "75,000"
PlayerName = "G_HOSA_PHAT"
TW.Initialize(File.OpenOutput(File.DirInternal, "HighScores.txt", False))
For I = 1 To 10
Select Case I
Case 1
PlayerRank = "1ST PLACE"
Case 2
PlayerRank = "2ND PLACE"
Case 3
PlayerRank = "3RD PLACE"
Case 4
PlayerRank = "4TH PLACE"
Case 5
PlayerRank = "5TH PLACE"
Case 6
PlayerRank = "6TH PLACE"
Case 7
PlayerRank = "7TH PLACE"
Case 8
PlayerRank = "8TH PLACE"
Case 9
PlayerRank = "9TH PLACE"
Case 10
PlayerRank = "10TH PLACE"
End Select
TW.WriteLine(PlayerRank & " - " & PlayerName)
TW.WriteLine(PlayerScore)
lstHighScores.AddTwoLines2(PlayerRank & " - " & PlayerName, PlayerScore, I)
Next
TW.Close
Else
Dim TR As TextReader
TR.Initialize(File.OpenInput(File.DirInternal, "HighScores.txt"))
For I = 1 To 10
PlayerScore = TR.ReadLine
PlayerName = TR.ReadLine
lstHighScores.AddTwoLines2(PlayerScore, PlayerName, I)
Next
TR.Close
End If
End Sub