Connecting Tech Pros Worldwide Forums | Help | Site Map

Open a file, read text and display in a listbox

Newbie
 
Join Date: Sep 2008
Location: New Zealand
Posts: 1
#1: Sep 15 '08
Hi, I am very new to programming and have just started to use Visual Basic 2005 Express Edition. I am trying to read from a text file to draw a rectangles and lines. However to begin I'm trying to get the information to show in a listbox so that I know the application is reading the file correctly. I seem to be able to open the file but it doesn't display anything in the listbox.
Expand|Select|Wrap|Line Numbers
  1. Dim FileName As String
  2.         Dim reader As System.IO.StreamReader
  3.  
  4.         uieOpenFileDialog.InitialDirectory = "C:\"
  5.         uieOpenFileDialog.Title = "Open a File"
  6.         uieOpenFileDialog.Filter = "Text Files(*.txt)|*.txt|All Files(*.*)|*.*"
  7.         uieOpenFileDialog.ShowDialog()
  8.  
  9.         MsgBox(FileName)
  10.         Dim DidWork As Integer = uieOpenFileDialog.ShowDialog()
  11.         If DidWork = DialogResult.Cancel Then
  12.             MsgBox("Cancel Button Clicked")
  13.         Else
  14.             FileName = uieOpenFileDialog.FileName
  15.             MsgBox(FileName)
  16.         End If
  17.  
  18.         reader = System.IO.File.OpenText(uieOpenFileDialog.FileName)
  19.         Dim shape As String
  20.         Dim code As Integer
  21.  
  22.         Try
  23.             Me.ListBox1.Items.Clear()
  24.  
  25.             Do While reader.Peek <> -1
  26.                 shape = reader.ReadLine
  27.                 If shape = "Rectangle" Then
  28.                     Select Case code
  29.                         Case 1
  30.  
  31.                             rect.X = Convert.ToInt32(reader.ReadLine)
  32.                             rect.Y = Convert.ToInt32(reader.ReadLine)
  33.                             rect.Width = Convert.ToInt32(reader.ReadLine)
  34.                             rect.Height = Convert.ToInt32(reader.ReadLine)
  35.  
  36.                             ListBox1.Items.Add()
  37.  
  38.  
  39.                         Case 2
  40.                             rect.X = Convert.ToInt32(reader.ReadLine)
  41.                             rect.Y = Convert.ToInt32(reader.ReadLine)
  42.                             rect.Width = Convert.ToInt32(reader.ReadLine)
  43.                             rect.Height = Convert.ToInt32(reader.ReadLine)
  44.  
  45.                             ListBox1.Items.Add
  46.  
  47.                     End Select
  48.                 Else
  49.                 End If
  50.             Loop
  51.         Catch
  52.             Me.ListBox1.Items.Add("file is empty")
  53.         End Try
  54.         reader.Close()
  55.     End Sub
The error code I'm receiving is: Error 1 Argument not specified for parameter 'item' of 'Public Function Add(item As Object) As Integer'.

Newbie
 
Join Date: Sep 2008
Posts: 10
#2: Sep 15 '08

re: Open a file, read text and display in a listbox


You should add something to ListBox when you're using Add method (lines 36 and 45). Change lines to following:

Expand|Select|Wrap|Line Numbers
  1. ListBox1.Items.Add("Code=" & code &  ", Rect.X=" & rect.X & ", Rect.Y=" & rect.Y & ", Rect.Width=" & rect.Width & ", Rect.Height=" & rect.Height)
  2.  
and you'll see what you read from your file.
Member
 
Join Date: Aug 2008
Location: Sao Paulo - Brasil
Posts: 77
#3: Sep 15 '08

re: Open a file, read text and display in a listbox


Hi friend.

Give a example of the line you have in this file text this way I could help you.

Your code is missing information. For example:

shape = reader.ReadLine
If shape = "Rectangle" Then
Select Case code *******You never put any value in code always is 0 (zero)
Case 1
****Convert the same value in this 4 lines. This will give you a DOT on screen
rect.X = Convert.ToInt32(reader.ReadLine)
rect.Y = Convert.ToInt32(reader.ReadLine)
rect.Width = Convert.ToInt32(reader.ReadLine)
rect.Height = Convert.ToInt32(reader.ReadLine)

ListBox1.Items.Add()


Case 2
rect.X = Convert.ToInt32(reader.ReadLine)
rect.Y = Convert.ToInt32(reader.ReadLine)
rect.Width = Convert.ToInt32(reader.ReadLine)
rect.Height = Convert.ToInt32(reader.ReadLine)

***here you are adding nothing to the listbox1
ListBox1.Items.Add

End Select
Else
End If

Put here a line of the text file that I'll help you.

Rogerio
Reply