I am quite puzzled here. I have a program inwhich when the user clicks on the command button, a random line should appear but only once.

I am storing each line with different variables.

Right now i have the random line coming up but it is repeating itself.
For instance, when i click on the button, line 1 appear, then line 4, and them line 1 again. How can i stop the line 1 to show again?
Thank you

Edited 6 Years Ago by london-G: n/a


1. Add lines into the List (collection framework)
2. Get line at random index
3. Draw a line
4. Remove that line from the list.
5. Go to step 2 till count is >0

my code for the moment is:

Function LoadNewQuestion() As String 'To lad a phrase
        Dim ThisNewQuestion As String
        Dim LoadedQuestionArray(10) As String
        Dim QuestionCount As Integer = 0
dim mycount2 as string
 FileOpen(1, "G:\Qu.txt", OpenMode.Input) 'open the file
                While Not EOF(1) 'end of file
                    QuestionCount += 1
            LoadedQuestionArray(QuestionCount) = (LineInput(1))

                    'the phrase is a random loaded phrase
                End While
                FileClose(1) 'close file
        ThisNewQuestion = LoadedQuestionArray(RandomNum(QuestionCount))
        mycount2 = LoadedQuestionArray(RandomNum(QuestionCount - 1))
        If mycount2 = ThisNewQuestion Then
            GoTo start

        Else : Label1.Text = ThisNewQuestion

        End If

        Return ThisNewQuestion 'returning the function
    End Function
    Function RandomNum(ByVal maxnum As Integer) As Integer
        Return Math.Ceiling(Rnd() * maxnum)

    End Function

End Class

an error say bad file name or number

Thank you

Edited 6 Years Ago by __avd: For easy readability, always wrap programming code within posts in [code] (code blocks)

Have a look,

 Dim lines As List(Of String) = System.IO.File.ReadAllLines("c:\xyz\file.txt").ToList()

        Dim rnd As New Random
        Dim ind As Integer
        While (lines.Count <> 0)
            ind = rnd.Next(0, lines.Count)
        End While
This article has been dead for over six months. Start a new discussion instead.