locked
Starting with VB 2008 Express Edition RRS feed

  • Question

  • Hi

    I like to have assistance with the VB 6 codes below;  a few cheeky lines dock into positions I do not mean them to be in; sorry;  I am on VB 2008 Express; a few lines have errors; adjacent to them I have described what VB 2008 reports.

    Public

     

    Class Form1

     

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

     

    Dim arData As Object -I CHANGED IT FROM VARIANT

     

    Dim i As Integer

     

    'Load the array

    arData = GetFileData(textFile1.Text) - I INSERTED A TEXT FILE INTO THE PROJECT

     

    'Display the data in the listbox

     

    For i = 0 To UBound(arData) 

     

     

    Next

     

    End Sub Class

    End

     

     

     

    Module

     

    Module1

     

    Public Function GetFileData(ByVal pFileName As String) As Object - I CHANGED IT FROM VARIANT

     

    Dim intFileNum As Integer

     

    Dim arData As Object - I CHANGED IT FROM VARIANT

     

    Dim flFirst As Boolean

    intFileNum = FreeFile()

     

    'Open the text file

    Open pfilename

     

    for input lock read as intFileNum - THE PROBLIME LINE; SAYS (OPEN) NOT DECLARED;  SAYS METHODS/ARGUEMENTS(PFILENAME) MUST IN PARENTHESES; SAYS COMMA OR A VALID EXPRESSION CONTINUATION REQUIRED INSTEAD OF (FOR).

     

    'First time through

    flFirst =

     

    'Load the array

     

    Do While Not EOF(intFileNum)

     

    If Not flFirst Then

     

    ReDim Preserve arData(UBound(arData) + 1)

     

    Else

     

    ReDim arData(0) False

    flFirst =

     

    End If

    LineInput

     

    #intfilenum, ardata(uBound(arData)) - SAYS EXPRESSION EXPECTED AT (#)

     

    Loop

     

    'Close file

    Close intFileNum - SAYS (CLOSE) NOT DECLARED; PARENTHESES IMPOSED AUTOMATICALLY AROUND (INTFILENUM).

     

    'Return the array

    GetFileData = arData

     

    End Function Module

    End

     

    Many Thanks

     

     

     

     

     

     

     

     

     

     

     

     

     

    Saturday, September 11, 2010 10:04 AM

Answers

  • The code example you provided looks like a real old way of reading the contents of a text file.

    There are a number of ways to do this the following show three different ways of loading text into listbox or textbox.

    The book is showing you really old ways of file handling - probably VB6 book.   Use one of the methods here for more up to date method of handling files.

    Public Class Form1
    
     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
      'Read lines into listbox
      ListBox1.Items.Clear()
      ListBox1.Items.Add("Method1")
      Dim TheText() As String = System.IO.File.ReadAllLines("TextFile1.txt")
      ListBox1.Items.AddRange(TheText)
    
     End Sub
    
     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
      'Read lines into listbox
      ListBox1.Items.Clear()
      ListBox1.Items.Add("Method2")
      Dim TheText() As String = System.IO.File.ReadAllText("TextFile1.txt").Split(vbCrLf)
      ListBox1.Items.AddRange(TheText)
    
      'Read lines into textbox
      TextBox1.Text = System.IO.File.ReadAllText("TextFile1.txt")
     End Sub
    
     Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
      'Read lines into listbox
      ListBox1.Items.Clear()
      ListBox1.Items.Add("Method3")
      Dim TheText() As String = My.Computer.FileSystem.ReadAllText("TextFile1.txt").Split(vbCrLf)
      ListBox1.Items.AddRange(TheText)
    
      'Read lines into textbox
      TextBox1.Text = My.Computer.FileSystem.ReadAllText("TextFile1.txt")
     End Sub
    End Class
    
    

     

    • Proposed as answer by spotty Thursday, September 16, 2010 7:32 PM
    • Marked as answer by Varnen Friday, September 17, 2010 7:15 AM
    Thursday, September 16, 2010 7:30 PM
  • This is one way to read the text from a text file into a listbox


        
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            
    Dim TheText() As String = System.IO.File.ReadAllLines("c:\SomeDirectory\SomeFile.txt")
            ListBox1.Items.AddRange(TheText)
        
    End Sub


    Coding4fun Be a good forum member mark posts that contain the answers to your questions or those that are helpful
    Please format the code in your posts with the button . Makes it easier to read . Or use the Forum Code Formatter by JohnWein http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/bf977a14-d9d4-4e84-9784-bf76b9e23261
    • Proposed as answer by Chao KuoModerator Tuesday, September 14, 2010 8:08 AM
    • Marked as answer by Varnen Tuesday, September 14, 2010 6:20 PM
    Saturday, September 11, 2010 7:31 PM

All replies

  • Are you trying to write the contents of a textfile into an array or just trying to read a textfile ? Let us know what you want done instead of bothering with old code .


    Coding4fun Be a good forum member mark posts that contain the answers to your questions or those that are helpful
    Please format the code in your posts with the button . Makes it easier to read . Or use the Forum Code Formatter by JohnWein http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/bf977a14-d9d4-4e84-9784-bf76b9e23261
    Saturday, September 11, 2010 10:57 AM
  • Hi

    Thanks for responding.

    Yeah; trying to write contents from a text file.

    I have limited access to Computers, Internet and Time; so I have to use this old book called Pure Visual Basic; if you can sort it out great; unfortunately I only have VB 2008 Express to work with.

    If it is not possible please send a fresh set of codes; in the last few minutes I found something on the net; but it too seems to have the same problem.

    Saturday, September 11, 2010 11:26 AM
  • This is one way to read the text from a text file into a listbox


        
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            
    Dim TheText() As String = System.IO.File.ReadAllLines("c:\SomeDirectory\SomeFile.txt")
            ListBox1.Items.AddRange(TheText)
        
    End Sub


    Coding4fun Be a good forum member mark posts that contain the answers to your questions or those that are helpful
    Please format the code in your posts with the button . Makes it easier to read . Or use the Forum Code Formatter by JohnWein http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/bf977a14-d9d4-4e84-9784-bf76b9e23261
    • Proposed as answer by Chao KuoModerator Tuesday, September 14, 2010 8:08 AM
    • Marked as answer by Varnen Tuesday, September 14, 2010 6:20 PM
    Saturday, September 11, 2010 7:31 PM
  • Hi

    Of course I wanted to read from a text file but I also like to have some assistance in getting my codes right; could someone help please?

    Many Thanks.

    Tuesday, September 14, 2010 6:23 PM
  • The code example you provided looks like a real old way of reading the contents of a text file.

    There are a number of ways to do this the following show three different ways of loading text into listbox or textbox.

    The book is showing you really old ways of file handling - probably VB6 book.   Use one of the methods here for more up to date method of handling files.

    Public Class Form1
    
     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
      'Read lines into listbox
      ListBox1.Items.Clear()
      ListBox1.Items.Add("Method1")
      Dim TheText() As String = System.IO.File.ReadAllLines("TextFile1.txt")
      ListBox1.Items.AddRange(TheText)
    
     End Sub
    
     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
      'Read lines into listbox
      ListBox1.Items.Clear()
      ListBox1.Items.Add("Method2")
      Dim TheText() As String = System.IO.File.ReadAllText("TextFile1.txt").Split(vbCrLf)
      ListBox1.Items.AddRange(TheText)
    
      'Read lines into textbox
      TextBox1.Text = System.IO.File.ReadAllText("TextFile1.txt")
     End Sub
    
     Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
      'Read lines into listbox
      ListBox1.Items.Clear()
      ListBox1.Items.Add("Method3")
      Dim TheText() As String = My.Computer.FileSystem.ReadAllText("TextFile1.txt").Split(vbCrLf)
      ListBox1.Items.AddRange(TheText)
    
      'Read lines into textbox
      TextBox1.Text = My.Computer.FileSystem.ReadAllText("TextFile1.txt")
     End Sub
    End Class
    
    

     

    • Proposed as answer by spotty Thursday, September 16, 2010 7:32 PM
    • Marked as answer by Varnen Friday, September 17, 2010 7:15 AM
    Thursday, September 16, 2010 7:30 PM
  • Hi

    Of course I wanted to read from a text file but I also like to have some assistance in getting my codes right; could someone help please?

    Many Thanks.


    You want help getting your code right not codes . Code is always sigular . All the code you have ever written is your code not codes .
    Coding4fun Be a good forum member mark posts that contain the answers to your questions or those that are helpful
    Please format the code in your posts with the button . Makes it easier to read . Or use the Forum Code Formatter by JohnWein http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/bf977a14-d9d4-4e84-9784-bf76b9e23261
    Thursday, September 16, 2010 11:33 PM
  • Hi Spotty

    That is helpful.

    Many Thanks.

    Friday, September 17, 2010 7:19 AM
  • Hi

    'The codes I wrote for 2 applications....'; is that wrong?

    Many Thanks.  

    Friday, September 17, 2010 7:35 AM