0

I am making a game in Visual Basic that has to contain a high score table. I am using a ListView control named lstscores to store rows of scores in 2 columns called Name and Score. The user has to guess words that are entered into a ListBox called lstwords when correct. I have set up 2 things in Application.Settings: settingname and settingscore both with type: System.Collections.Specialized.StringCollectino, Scope: user and Value: Nothing.

When user clicks on a save button, I basically want to take the username and score and store it in the settings respectively. So when I open the form up next time I can load the settings back into the same ListView control.

I have been researching for a few hours, and I haven't been able to find anything exactly suited to my needs.

I cannot provide my code, as i have tried everything possible to my knowledge, and nothing has worked.

If someone could please give me advice or a direction on how to go about doing this, I would be very grateful.

Thanks

Dim arr(1) As String
    Dim score As Integer

    inputname = txtname.Text
    score = lstwords.Items.Count

    arr(0) = inputname
    arr(1) = score
    lstscores.Items.Add(New ListViewItem(arr))
Paul B
  • 199
  • 1
  • 4
  • 13

1 Answers1

0

You can use combination of Save() and Reload() methods to store your data. When user clicks on Save button, you can iterate your ListView adding content ListView.Items[n] to collection of names and content of sub-item (ListViewItem.SubItems[0]) to collection of scores. Then, use My.Settings.Save() to save your data. To have your data back in ListView after start, use My.Settings.Reload() (in for example Form.Load event) and iterate it back. Here's an example of saving iteration (not tested):

For Each it0 in lstscores.Items
    My.Settings.settingname.Add(it0.Text)
    My.Settings.settingscore.Add(it0.SubItems[0].Text)
Next
My.Settings.Save()
user35443
  • 6,309
  • 12
  • 52
  • 75
  • ok so i tried your code with only the settingname line, and it doesn't even save that to my.settings – Paul B Jun 01 '13 at 14:16
  • Didi you use `My.Settings.Save()` ? – user35443 Jun 01 '13 at 14:19
  • `For Each it0 In lstscores.Items` `My.Settings.settingname.Add(it0.Text)` `Next` `My.Settings.Save()` – Paul B Jun 01 '13 at 14:27
  • Ok. Have you tried complete solution with loading? How do you check if are contents of my.settings changed? It should be somewhere in filesystem. VS contents won't change, they're default. – user35443 Jun 01 '13 at 14:39
  • to check the settings have changed, i double click on `My Project` and go `Settings` and the `settingname` setting still has the default xml code. to carry the score to the listview control i use the code that i have put in the original question – Paul B Jun 01 '13 at 14:45
  • Thats not correct. In Visual Studio there are only default values. `My.Settings.Save()` edits special file located in AppData (not sure how it is on newer systems). You can try to search config file there, or you can simply complete `Form.Load` by adding `My.Settings.Reload()` and set breakpoint there (then you can use Watch to check data in My.Settings). – user35443 Jun 01 '13 at 16:09