mardi 29 mars 2016

Generate Random Name and Save From Selecting Again X Times

So I found an example, which generates a random name loaded from a text files. Works just fine, as I want to load 1 name each time, however it's tied to a button click, so removing the name from the list each time is mute because the same name could appear again after X amount of clicks.

The text file has 10 names currently. I want to randomly select a name and mark it as being used. Rinse/repeat until all 10 names are used. Then I want to mark all the names as being un-used and rinse/repeat another 10 times.

I'm having a tough time determining how to mark and save each name. Suggestions?

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    If IO.File.Exists("c:\temp\test file.txt") Then
        Dim sr As New IO.StreamReader("c:\temp\test file.txt")
        Dim fileContents As String = sr.ReadToEnd
        Dim fileList As New List(Of String)

        'Stores the contents of fileContents as items 
        'where the delimiator is a new line
        'I use vbCrLf in this case instead of environment.newline
        'Because environment.newline adds a blank space
        For Each Str As String In fileContents.Split(CChar(vbCrLf))
            fileList.Add(Str)
        Next

        'X = to the amount of times you want to add a random name
        Dim x As Integer = 0
        For i As Integer = 0 To x
            'Gets a random number from 0 to the fileList's item count
            Dim randItem As Integer = rand.Next(0, fileList.Count)
            'Adds the item to the listbox
            ListBox1.Items.Add(fileList.Item(randItem))
            'Removes the item from the list
            fileList.RemoveAt(randItem)
        Next
    Else
        MessageBox.Show("File Not Found")
    End If
End Sub




Aucun commentaire:

Enregistrer un commentaire