Read specific line from text file, according to Checked Listbox selection number.

Posted by Manolis on Stack Overflow See other posts from Stack Overflow or by Manolis
Published on 2010-06-14T22:58:43Z Indexed on 2010/06/14 23:02 UTC
Read the original article Hit count: 277

Filed under:
|
|
|
|

Heya,
i want to create an application which will read a specific line from a text file and show it in a textbox. The line will be chosen according to the number of the listbox selection i will make.
Here's the code:

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim i As Integer
        For i = 0 To Me.CheckedListBox1.CheckedIndices.Count - 1
Me.CheckedListBox1.SetItemChecked(Me.CheckedListBox1.CheckedIndices(0),False)
        Next i
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        If CheckedListBox1.CheckedItems.Count <> 0 Then
            Dim reader As New System.IO.StreamReader(CurDir() & "\" & "READ.txt")
            Dim x As Integer
            Dim s As String = ""
            For x = 0 To CheckedListBox1.CheckedItems.Count - 1
                s = s & "Answer " & (x + 1).ToString & ") " & CheckedListBox1.CheckedItems(x).ToString & ControlChars.CrLf & reader.ReadLine() & ControlChars.CrLf & ControlChars.CrLf

            Next x
            Answer.Text = (s)

        Else
            MessageBox.Show("Please select questions.", "Error", _
                        MessageBoxButtons.OK, _
                        MessageBoxIcon.Information)
            Return
        End If
    End Sub
End Class

So lets say i 'check' the first, second, and fifth items from the checked listbox, i want it to read from the text file the first, second, and fifth lines of text and show them in the textbox.
The current code just reads line 1, 2, 3 (...) in order, no matter what item i have 'checked'.
Thanks in advance!

© Stack Overflow or respective owner

Related posts about select

Related posts about read