0

Not sure if my title makes much sense, so I will try to explain my question here. So basically I am expanding my program by allowing things to be customized within it.

Say for example I do this: I click on File -> Options, and a new form is opened with tabs. I have different settings that you can toggle via dropdown box and checkboxes. Now once a user sets the settings they want, or don't want, they click on a button that says either "OK" or "Cancel".

What is the method to saving these settings, or reverting back to the original settings? Do you save via txt file, or is this a default function within a certain line of code?

UPDATE:

So I fixed my previous issue. Now I am having another with the saves. The saves are working good, but I want to use them in selecting my CheckListBox Collection range and also have that range load on start as well. so these are the 2 things that I have been using to do so, that results in adding to the previous, set, collection.

Working for RNG:

Dim rand As New Random()
    Dim winners = Enumerable.Range(1, My.Settings.numberSetting).OrderBy(Function(r) rand.Next()).Take(5).ToArray()

Not working for Onload CheckListBox:

Me.LotteryNumbers.Items.Add(1, My.Settings.numberSetting)

If I remove the 1 from Me.LotteryNumbers.Items.Add, the result is this: Sample 1-100 CheckListBox

3 Answers3

1

This ought not compile:

LotteryNumbers.Items.Add(1, My.Settings.numberSetting)

The overload which takes a second argument expect a Boolean to set the item added to Checked or not. One way is to add items in a loop:

Dim maxNums = My.Settings.numberSetting

' make sure it is empty
clb.Items.Clear()

For n As Int32 = 1 To maxNums
    clb.Items.Add(n.ToString)
Next

I don't like using items in Settings as variables, so it grabs the current value to use. Another way uses AddRange:

clb.Items.AddRange(Enumerable.Range(1, maxNums).Select(Function(s) s.ToString()).ToArray())

Items is an collection of Object, so the Select converts to string to add them.

Ňɏssa Pøngjǣrdenlarp
  • 38,411
  • 12
  • 59
  • 178
  • I've managed to get it to create the amount of checkboxes on load, but any time I change it, nothing happens and no matter what number I select, there is always 18 checkboxes. – RockGuitarist1 Mar 20 '16 at 02:01
  • Where does 18 come from, is it the default you entered into Settings? Be sure that you re assign a new value back to settings; `My.Settings.numberSetting = Cint(somecontrol.Text)` isnt a permanent mapping where changes to the control automagically updates settings. Also be sure to save Settings before the app ends if it is not set to automatically do so. – Ňɏssa Pøngjǣrdenlarp Mar 20 '16 at 13:27
  • I figured it out. Thanks for the help. Turned out I just had my ordering wrong. I'll post my solution. – RockGuitarist1 Mar 21 '16 at 12:50
  • 1
    Gave you credit on the About page of my program since you helped on some key parts of the coding. – RockGuitarist1 Mar 21 '16 at 17:37
1

NEVER ORDER BY RANDOM.NEXT()

Mostly you get lucky, but it's not guaranteed. It's only a matter of time before that code blows up on you at run time. The longer the sequence to be sorted, the more likely you are to get an exception.

What you should do instead is implement a Fisher-Yates sort method:

Private rand As New Random()
Public Function Shuffle(Of T)(ByVal items As IList(Of T)) As IList(Of T)
    For i As Integer = items.Count - 1 To 1 Step -1
        Dim j As Integer = rand.Next(i + 1)
        Dim temp As T= items(i)
        items(i) = items(j)
        items(j) = temp
    Next
    Return items
End Function
Community
  • 1
  • 1
Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
0

Solution for the working code to update and save checklistbox box count.

Private Sub OptionOkButton_Click(sender As Object, e As EventArgs) Handles OptionOkButton.Click
    Main.LotteryNumbers.Items.Clear()
    My.Settings.numberSetting = CInt(NumberCombo.Text)
    Dim maxNum = My.Settings.numberSetting
    Main.LotteryNumbers.Items.AddRange(Enumerable.Range(1, maxNum).Select(Function(s) s.ToString()).ToArray())
    My.Settings.Save()
    Me.Close()
End Sub