开发者

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

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, 开发者_JS百科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'.


Increment a counter each time your read a line from the file to track which line you are reading and only add a line to the text box when your line number matches a selected line number.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜