List Box

Add items in txt file to listbox

Page 1 of 1

5 Replies - 2048 Views - Last Post: 01 October 2007 - 08:58 PM Rate Topic: -----

#1 vols  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 25-September 07

List Box

Post icon  Posted 01 October 2007 - 04:32 PM

I need help on adding items to list boxes.

I have already created a txt file using Stream Writer and I want some of the items on the txt file to show on the listbox. I need help on what are the steps needed to be taken to be able to do this. eg Is the first step creating an array?
Is This A Good Question/Topic? 0
  • +

Replies To: List Box

#2 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1634
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: List Box

Posted 01 October 2007 - 06:05 PM

Try doing it this way (this will work if you have a single item per line):

Dim stream_reader As New IO.StreamReader("YourFileName")
		Dim line As String

		' Read the file one line at a time.
		line = stream_reader.ReadLine()
		Do While Not (line Is Nothing)
			' Trim and make sure the line isn't blank.
			line = line.Trim()
			If line.Length > 0 Then _
				ListBox1.Items.Add(line)

			' Get the next line.
			line = stream_reader.ReadLine()
		Loop



Hope this helps :)
Was This Post Helpful? 0
  • +
  • -

#3 vols  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 25-September 07

Re: List Box

Posted 01 October 2007 - 06:51 PM

View PostPsychoCoder, on 1 Oct, 2007 - 06:05 PM, said:

Try doing it this way (this will work if you have a single item per line):

Dim stream_reader As New IO.StreamReader("YourFileName")
		Dim line As String

		' Read the file one line at a time.
		line = stream_reader.ReadLine()
		Do While Not (line Is Nothing)
			' Trim and make sure the line isn't blank.
			line = line.Trim()
			If line.Length > 0 Then _
				ListBox1.Items.Add(line)

			' Get the next line.
			line = stream_reader.ReadLine()
		Loop



Hope this helps :)




I tried the coding from the form load area and nothing displays on the listbox. Attached is the coding.

Attached File(s)

  • Attached File  List.doc (26.5K)
    Number of downloads: 87

Was This Post Helpful? 0
  • +
  • -

#4 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1634
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: List Box

Posted 01 October 2007 - 06:52 PM

Post the code here, I'm not going to download a file from someone I don't know
Was This Post Helpful? 0
  • +
  • -

#5 vols  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 25-September 07

Re: List Box

Posted 01 October 2007 - 08:10 PM

View PostPsychoCoder, on 1 Oct, 2007 - 06:52 PM, said:

Post the code here, I'm not going to download a file from someone I don't know


Sorry for that, heres the code


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
		Dim swrStreamWriter As IO.StreamWriter
		If IO.File.Exists("C:\Name.txt") = False Then
			swrStreamWriter = IO.File.CreateText("C:\Name.txt")

		End If

	End Sub

	Private Sub Names()

		Dim swrName As IO.StreamWriter
		swrName = IO.File.CreateText("C:\Name.txt")

		swrName.WriteLine("Tim")
		swrName.WriteLine("Mr")
		swrName.WriteLine("*")

		swrName.WriteLine("Timoci")
		swrName.WriteLine("Mr")
		swrName.WriteLine("*")


	End Sub

	Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		
		Dim Stream_Reader As New IO.StreamReader("C:\Name.txt")
		Dim line As String

		line = Stream_Reader.ReadLine
		Do While Not (line Is Nothing)
			line = line.Trim
			If line.Length > 0 Then
				ListBox1.Items.Add(line)
				line = Stream_Reader.ReadLine
			End If
		Loop
	End Sub

	Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
	  

	End Sub

	Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
		
	End Sub
End Class



*Always use code tags :)

This post has been edited by PsychoCoder: 01 October 2007 - 08:32 PM

Was This Post Helpful? 0
  • +
  • -

#6 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1634
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: List Box

Posted 01 October 2007 - 08:58 PM

In your Form_Load even put that code in a try...catch block and see if it displays any errors, like this, I also changed to a Do...While loop as well

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		Try
			   Dim Stream_Reader As New  _
			   StreamReader("C:\Name.txt")
			   'Clear the ListBox (that way 
			   'its reloaded on each form load)
			   Me.ListBox1.Items.Clear()
			   'Start your loop
			   Do 
					 'Add the next line to the ListBox
					  ListBox1.Items.Add(Stream_Reader.ReadLine)
			   'Continue to loop until the end of the file
			   Loop Until Stream_Reader.Peek = -1

		Catch ex As Exception
			  MsgBox(ex.Message)
		 Finally
			  reader.Close()
		 End Try
	End Sub



That should solve your problems :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1