TonyFlora63

Hi

I'm looking to write the contents of a text file to an array and view one line at a time in a listbox. When I say one line at a time I mean only view one line in the listbox and as It loops through the text file the line that is showing is replaced with the next line, and so on.

TonyFlora63



Re: Visual Basic General Loop through text file and view one line in listbox

gasukegon

Hi Tony,

I was not sure exactly what you meant by "view one line in the listbox" but I assume it means that your ListBox Control's size will be only large enough to show one line at a time. In this case the following code should work:

Code Snippet

'Written in VS2003

'Get the Text File into the Array
Dim MyList As New ArrayList
Dim reader As System.IO.StreamReader = New System.IO.StreamReader(MyFilePath)
With reader
While .Peek > -1
MyList.Add(.ReadLine)
End While
End With
Me.ListBox1.DataSource = MyList 'Bind the Array to the Control

Note! - the ListBox can be scrolled by the Up/Down arrow keys if the ListBox's scrollbuttons are too small.

You could also accomplish this by using a text box and up/down buttons; you would just increment/decrement the index counter in the array for each button click and display the ArrayList's data in the TextBox at that index.

Hope this helps.

gasukegon





Re: Visual Basic General Loop through text file and view one line in listbox

TonyFlora63

Hi

I need this code for a tip of the day form. This code works good with the exception that it needs to skip a few lines after the Did you know..., and it needs to loop through the text file showing one tip at a time.

Thanks

Tony





Re: Visual Basic General Loop through text file and view one line in listbox

Tech_Prince

Use if condition or use Select Case statement. If the condition is true then just continue the loop.


Here is the modified code block :

'Get the Text File into the Array
Dim MyList As New ArrayList
Dim reader As System.IO.StreamReader = New System.IO.StreamReader(MyFilePath)
With reader
While .Peek > -1


If (condition) then

Continue While

End If


MyList.Add(.ReadLine)
End While
End With
Me.ListBox1.DataSource = MyList 'Bind the Array to the Control


Or simple method :

Dim fso as new IO.StreamReader(FilePath as String)
Dim fso As IO.StreamReader
While not fso.EndOfStream
If condition Then
Continue While
End If
list1.add(fso.ReadLine())
End While
fso.close





Re: Visual Basic General Loop through text file and view one line in listbox

TonyFlora63

Thanks but this is not working. The form just freezes. Maybe I'm missing something here.

Private Sub Btn_TipOday_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_TipOday.Click

'Get the Text File into the Array

Dim MyList As New ArrayList

Dim reader As System.IO.StreamReader = New System.IO.StreamReader("Tips.txt")

With reader

While .Peek > -1

If File.Exists("Tips.txt") Then

Continue While

'Else

'Exit Sub

End If

MyList.Add(.ReadLine)

End While

End With

Me.LB_TipOday.DataSource = MyList 'Bind the Array to the Control

End Sub





Re: Visual Basic General Loop through text file and view one line in listbox

gasukegon

Hi Tony,

I am not sure why your code hangs but it seems unnecessary to check if the file exists for every line in the text file. I modified the code to check if the file exists, skip the beginning specified number of lines in the text file (10 in this example), and generate a random number to randomly pull up different lines in the ListBox. Maybe I am not understanding exactly how the user will interact with the ListBox control but hopefully this helps and/or points you in the right direction.

Code Snippet

Private Sub Btn_TipOday_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_TipOday.Click
Dim MyFile As String = "C:\Tips.txt"
Dim LinesToSkip as Integer = 10
'Make sure file exists before continuing
If System.IO.File.Exists(MyFile) = True Then
Dim MyList As New ArrayList
Dim reader As System.IO.StreamReader = New System.IO.StreamReader(MyFile)
Dim lineNum As Integer
Dim line As String
With reader
'Skip a certain number of lines in the text file
While .Peek > -1 AndAlso lineNum <= LinesToSkip
line = .ReadLine
lineNum += 1
End While
'Add line to ArrayList if we have not reached EOF
While .Peek > -1
MyList.Add(.ReadLine)
End While
.Close()
End With
Me.ListBox1.DataSource = MyList 'Bind the Array to the Control
'Select line to show based on a Random number.
Dim rand As New Random
Dim rndNum As Integer = rand.Next(0, (MyList.Count - 1))
Me.ListBox1.SelectedItem = Me.ListBox1.Items(rndNum)
End If
End Sub

You will want to modify the declarations for MyFile and LinesToSkip so as to work with your text file.

gasukegon





Re: Visual Basic General Loop through text file and view one line in listbox

TonyFlora63

I'm trying to display a line of text in a Tip of the day form. This has really been a challange for me because I can't seem to find too much info on the topic. Anyway thanks for your help.

I now get the following error:

'minValue' cannot be greater than maxValue.
Parameter name: minValue

rndNum As Integer = rand.Next(0, (MyList.Count - 1))

Tony





Re: Visual Basic General Loop through text file and view one line in listbox

gasukegon

Hi Tony,

The error is happening because MyList has no elements in it which causes the (MyList.Count - 1) to equal -1 and therefore the maxValue is less than the minValue (0). I am not exactly sure why the count is 0 but it may be due to there not being enough lines in the the text file. In any case you can wrap the random generation with an If statement to check that MyList.Count is greater than 1.

Old Code:

Code Snippet
'Select line to show based on a Random number.
Dim rand As New Random
Dim rndNum As Integer = rand.Next(0, (MyList.Count - 1))
Me.ListBox1.SelectedItem = Me.ListBox1.Items(rndNum)

New Code:

Code Snippet

'Select line to show based on a Random number.

If MyList.Count > 1 Then
Dim rand As New Random
Dim rndNum As Integer = rand.Next(0, (MyList.Count - 1))
Me.ListBox1.SelectedItem = Me.ListBox1.Items(rndNum)

End If

What does the text file look like Does it have a header that must be skipped and then each line is a separate tip

gasukegon





Re: Visual Basic General Loop through text file and view one line in listbox

TonyFlora63

You were right on about not enough lines in the text file. Here's the contents of the text file:

This is a test
This is a test2
This is a test3
This is a test3
This is a test4
This is a test5
This is a test6
This is a test7
This is a test8
This is a test9
This is a test10

Instead of one line being displayed all of the lines are displayed. I need to have only one line displayed every time that i click the button. Tip1, tip2, etc, etc.

Tony





Re: Visual Basic General Loop through text file and view one line in listbox

gasukegon

Sounds like you need to have two routines. One will load the Tips.txt file into the ArrayList by an event such as FormLoad and then when the user clicks the button it will increment through the ArrayList.

I threw this code into the Form's Class and made alot of changes.

Note! - :

- MyList and TipCount are Class Level variables

- LinesToSkip is set to 0 so that no lines in the text file will be skipped

- Form_Load Event fills out MyList

- Button_Click Event increments TipCount and adds to ListBox

Code Snippet

Public Class Form1

Private MyList As New ArrayList 'List to contain all of the Tips
Private TipCount as integer 'Counter to loop through ArrayList

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim MyFile As String = "C:\Tips.txt"
Dim LinesToSkip As Integer = 0
'Make sure file exists before continuing
If System.IO.File.Exists(MyFile) = True Then
Dim reader As System.IO.StreamReader = New System.IO.StreamReader(MyFile)
Dim lineNum As Integer
Dim line As String
With reader
'Skip a certain number of lines
While .Peek > -1 AndAlso lineNum < LinesToSkip
line = .ReadLine
lineNum += 1
End While
'Add line to ArrayList if we have not reached EOF
While .Peek > -1
MyList.Add(.ReadLine)
End While
.Close()
End With

'Select line to show based on a Random number.
If MyList.Count > 1 Then
Dim rand As New Random
TipCount = rand.Next(0, (MyList.Count - 1))
Me.ListBox1.Items.Add(Me.MyList.Item(TipCount))
End If
End If
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
TipCount += 1
'reset TipCount to beginning if we reached the end
If TipCount >= MyList.Count Then
TipCount = 0
End If
Me.ListBox1.Items.Clear() 'clear the ListBox
Me.ListBox1.Items.Add(Me.MyList.Item(TipCount)) 'add Tip

End Sub

End Class

gasukegon




Re: Visual Basic General Loop through text file and view one line in listbox

TonyFlora63

I'm almost there!

the tips show up in the list box randomly, and when I click the button the next tip is displayed! I need 2 more things to happen then it's a go.

1. the tip is being displayed above the

Did you know...

I need the tip to be displayed here

2. I need the code for this form to Show Tip at Startup if it is checked.

Tony





Re: Visual Basic General Loop through text file and view one line in listbox

gasukegon

Just a couple questions:

1. What kind of control is the "Did you kow..." Is it a Label or is it inside of the ListBox

2. Are you saving the Checked State of the "Show Tip at Startup" Control (ie. a settings file)

3. Is the Tip Of The Day a separate Form that will be loaded if the "Show Tip at Startup" Control is checked

gasukegon





Re: Visual Basic General Loop through text file and view one line in listbox

TonyFlora63

1. Did you know.. is a label inside of the ListBox next to the lightbulb icon.

2. Not sure how to save the checked state

3. The Tip Of The Day a separate Form that will be loaded if the "Show Tip at Startup" Control is checked

Tony





Re: Visual Basic General Loop through text file and view one line in listbox

gasukegon

1. To add the label you can change the ListBox.Add lines:

From:

Me.ListBox1.Items.Add(Me.MyList.Item(TipCount))

To:

Me.ListBox1.Items.Add("Did you know... " & Me.MyList.Item(TipCount))

2. To save a setting I usually write to an XML file or a Text file on the user's system. On the FormLoad of the main Form I then read that setting and assign it to the control. You can also use the Registry but I try to stay away from that.

3. Depending on how you show your Tip Of The Day form you will just want to add logic to your Main form to only show the Tip Of The Day form if the Checked State is True.

gasukegon





Re: Visual Basic General Loop through text file and view one line in listbox

TonyFlora63

Hi

The lines are not apperaing in the listbox properly. Like word wrap not being checked in a text file

result:

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

Desired result:

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

Also is there a way to show the lines in the listBox at line 10 instead of the first line in the listbox Should I maybe use a RT box instead