Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27333

VS 2010 [RESOLVED] IO.Streamreader Repeats

$
0
0
Code:

Dim lineno As Integer = 1
        Dim r As New IO.StreamReader("C:\SuperPad\LineNumbers.txt")
        While (r.Peek() > -1)
            ListBox1.Items.Add(lineno.ToString & "  " & r.ReadLine)
            lineno = lineno + 1
        End While : r.Close()

This is my code. I am trying to take lines from a file (C:\Superpad\LineNumbers.txt) and list them in a listbox, one by one, numbering them. The problem is that the listbox lists all the lines in the file multiple times. For example, if the file says:

Hello.
Bye.
Hi.

The listbox will say:

1 Hello
2 Bye
3 Hi
4
5
6 Hello
7 Bye
8 Hi

And so on, for at least 3 times. The following is the code that saves the RichTextBox lines to a file (yes, I'm reading each line of a RichTextBox):

[CODE]
Dim charIndex As String
Dim lineIndex As String
Dim line As String
IO.Directory.CreateDirectory("C:\SuperPad")
Dim w As New IO.StreamWriter("C:\SuperPad\LineNumbers.txt")
Dim i As Integer
Dim length As Integer = CType(TabControl1.SelectedTab.Controls.Item(0), RichTextBox).Lines.Length
charIndex = CType(TabControl1.SelectedTab.Controls.Item(0), RichTextBox).Text.LastIndexOf(lastchar)
lineIndex = CType(TabControl1.SelectedTab.Controls.Item(0), RichTextBox).GetLineFromCharIndex(charIndex)
line = CType(TabControl1.SelectedTab.Controls.Item(0), RichTextBox).Text.Substring(lineIndex)
For i = 0 To length - 1
w.WriteLine(line)
Next
w.Close()
[CODE\]


"lastchar" is the last typed character of the richtextbox. (in richtextbox keydown event)

Thanks for your time in advance,
Bill W.

Viewing all articles
Browse latest Browse all 27333

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>