question, reading txt file vb2010

  • (5 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »

72 Replies - 16847 Views - Last Post: 01 July 2011 - 05:28 PM Rate Topic: -----

#1 Chillidog  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 06-June 11

question, reading txt file vb2010

Posted 22 June 2011 - 09:59 AM

hi everyone, i hope i post this message in the right question section. thanks in advance for the help.

i am trying to teach myself vb (i am only 14) to help with our family business. what i am trying to do is read and display a text file of customer names (first N, and last N in names.txt). for right now i am just trying to open them with my file/open menustrip and get them displayed in a list box. as you see i tried writing a module, structure, ect, i learned these from my book. i wrote each of these by them selves. i am just having a time putting them together?

at some point i will have to figure out to sort the last names by alpbetical order, add new names and add phone numbers...but one step at a time. i am not looking for answers because i will have to do it again later. i just need someone to point me in the right direction. thanks again

this is my gui...

Posted Image

Module Module1

    Sub Main()
        ' Read the information into a single string
        Dim objReader As New System.IO.StreamReader("C:\names.txt")
        Dim fileString As String = objReader.ReadToEnd()
        objReader.Close()
        objReader.Dispose()

        ' Convert the input string into an array
        Dim theArray As Char() = fileString.ToCharArray()
        theArray(0) = "FirstName"
        ' Declare a resultString to hold the value 
        Dim resultString As String = ","

        ' Increment each item in the string 
        Dim curChar As Char
        For i As Integer = 0 To (theArray.Length - 1)
            curChar = theArray(i)
            If (Char.IsLetterOrDigit(curChar)) Then
                theArray(i) = Chr(Asc(curChar) + 1)
            End If
        Next

        ' Convert the modified array back to a string
        Dim newString As String = New String(theArray)

        ' Write the array to file-out.txt
        Dim objWriter As New System.IO.StreamWriter("C:\names.txt")
        objWriter.Write(newString)
        objWriter.Close()
        objWriter.Dispose()
    End Sub
End Module

Public Class Form1
   Public Sub OpenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Click
        ' Read the information into a single string
        Dim objReader As New System.IO.StreamReader("C:\names.txt")
        Dim fileString As String = objReader.ReadToEnd()
        objReader.Close()
        objReader.Dispose()

        ' Convert the input string into an array
        Dim theArray As Char() = fileString.ToCharArray()

        ' Declare a resultString to hold the value 
        Dim resultString As String = ","

        ' Increment each item in the string 
        Dim curChar As Char
        For i As Integer = 0 To (theArray.Length - 1)
            curChar = theArray(i)
            If (Char.IsLetterOrDigit(curChar)) Then
                theArray(i) = Chr(Asc(curChar) + 1)
            End If
        Next

        ' Convert the modified array back to a string
        Dim newString As String = New String(theArray)

        ' Write the array to file-out.txt
        Dim objWriter As New System.IO.StreamWriter("C:\names.txt")
        objWriter.Write(newString)
        objWriter.Close()
        objWriter.Dispose()
    End Sub

    Public Structure People
        Public intFullName() As String
        <VBFixedString(15)> Public FirstName As String
        <VBFixedString(15)> Public LastName As String
        Public Sub New(ByVal new_FullName As String, ByVal first_name As String,
        ByVal last_name As String)
            intFullName = new_FullName
            intFullName(1) = last_name
            intFullName(0) = first_name
            FirstName = first_name
            LastName = last_name
        End Sub
        Public Overrides Function ToString() As String
            Return LastName & " " & FirstName
        End Function
    End Structure

    Private Sub Records()
        ' Declare a record variable.
        Dim peep As New People
        ' Get an available file number.
        Dim file_num As Integer = FreeFile()
        ' Open the file.
        FileOpen(file_num, "C:\names.txt", OpenMode.Random,
        OpenAccess.ReadWrite, OpenShare.Shared, Len(peep))

        ' Fetch and display the records.
        Dim obj As ValueType = DirectCast(peep, ValueType)
        For Each i As Integer In New Integer() {3, 1, 5, 2, 6}
            FileGet(file_num, obj, i)
            peep = DirectCast(obj, People)
            Debug.WriteLine("[" & peep.ToString() & "]")
        Next i
        ' Close the file.
        FileClose(file_num)

    End Sub


Is This A Good Question/Topic? 0
  • +

Replies To: question, reading txt file vb2010

#2 Chillidog  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 06-June 11

Re: question, reading txt file vb2010

Posted 22 June 2011 - 10:09 AM

I also tried this as a array. i cant figure out why it never worked...

Public Interface IIdentity
        Property Identity() As string = {"first name", "last name"}
End Interface

Public Class Customer
    Implements IIdentity

    Public Sub New(ByVal FullName As String, ByVal lastName As String, ByVal firstname As String)
        _FullName = firstname & lastName
        _LastName = lastName
        _FirstName = firstname
    End Sub

    Private _FullName As String

    Public Property FullName As String Implements IIdentity.Identity
        Get
            Return _FullName
        End Get
        Set(ByVal value As String)
            _FullName = value
        End Set
    End Property

    Private _LastName As String
    Public Property LastName() As String
        Get
            Return _LastName
        End Get
        Set(ByVal value As String)
            _LastName = value
        End Set
    End Property

    Private _FirstName As String
    Public Property FirstName() As String
        Get
            Return _FirstName
        End Get
        Set(ByVal value As String)
            _FirstName = value
        End Set
    End Property
End Class

Public Class CustomerCollection
    Inherits IdentityCollection(Of Customer)
End Class

This post has been edited by Chillidog: 22 June 2011 - 10:18 AM

Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6039
  • View blog
  • Posts: 23,436
  • Joined: 23-August 08

Re: question, reading txt file vb2010

Posted 22 June 2011 - 10:13 AM

Your topic was posted in the VB6 forum, but the provided code and/or question actually is not VB6, but rather VB.NET code. If you are writing code in Visual Basic 2003, Visual Basic 2005, Visual Basic 2008, or Visual Basic 2010, this code is written in VB.NET. While VB6 code and VB.NET code have some similarities in syntax, their differences outweigh these. For this reason we have a dedicated VB.NET forum.

Your post has been moved to this VB.NET forum. Please post your future questions on VB.NET in this more appropriate location.

Thank you for your cooperation.
Was This Post Helpful? 0
  • +
  • -

#4 Chillidog  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 06-June 11

Re: question, reading txt file vb2010

Posted 22 June 2011 - 10:15 AM

thanks jack
Was This Post Helpful? 0
  • +
  • -

#5 Chillidog  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 06-June 11

Re: question, reading txt file vb2010

Posted 22 June 2011 - 10:58 AM

I am sorry if i wasn't clear about my problem. of course displaying it isn't the problem. i have got this code to work
        Dim readText As String = (C:\FS_Inc\names.txt")
        Dim textParser As New ListBox
        textParser.Text = My.Computer.FileSystem.ReadAllText(readText)
        lbxNameList.Items.Add(textParser.Text())


but I thought streamwriter was better for many files and edits...i am pretty sure this is what is i need...but i can't get it to open the initial directory to open my names.txt
  Dim str As String
        Dim srdNameFile As System.IO.StreamReader
        OpenFD.InitialDirectory = "C:\FS_Inc\"
        OpenFD.Title = "Open a Text File"
        OpenFD.ShowDialog()
        ' Select your text file.
        ' ShowDialog() returns FileName you selected
        srdNamesFile = New System.IO.StreamReader(OpenFD.FileName)  
        
        '  my peek = -1 implies end of file
        Do Until srdNamesFile.Peek = -1                          
        '  Reads a line from the file.
        Str = srdNamesFile.ReadLine()            
        'skipps null lines
        If Str() <> "" Then
             lbxNamesList.Items.Add(str + vbNewLine)		
        '  Callin parseLine to identify the token line.                
      parseLine(str)                      			
        End If

    End Sub
  


hope this helps clear things up. what is my problem?

This post has been edited by Chillidog: 22 June 2011 - 11:01 AM

Was This Post Helpful? 0
  • +
  • -

#6 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1817
  • View blog
  • Posts: 4,625
  • Joined: 14-March 10

Re: question, reading txt file vb2010

Posted 22 June 2011 - 11:02 AM

Can you please tell us what error you are getting, or if no error what output is getting out which is different from what you expected? This will help us to easily see your problem instead of reading all lines of code.

Q: Are your names added every line per name? Why dont you simply use .readLine()? to get the line in to the array
Was This Post Helpful? 0
  • +
  • -

#7 Chillidog  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 06-June 11

Re: question, reading txt file vb2010

Posted 22 June 2011 - 11:16 AM

thanks smohd. keeping to my last post (forgetting the first few) it is like there isn't any file there. when i try to click 'open' in my menu I get nothing. i need to select a file to open...vs not making a program that just opens one file.

my family will be using this to keep lists of different customer groups...tho for right now i want to just get one group to work...my test file = "C:\FS_Inc\names.txt". so in the file we have one fullname per line, like: first name last name (ex. joe smith). and our lists can have anywhere between 15-100 names.

we already have some lists, and from what i have learned i thought i could take those names and display them, and having the option of sorting them alphabetically if to find the people we needed from that list.

but for now, it seems the dialogbox won't show up...and i think this is the problem i need help with

This post has been edited by Chillidog: 22 June 2011 - 11:21 AM

Was This Post Helpful? 0
  • +
  • -

#8 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1817
  • View blog
  • Posts: 4,625
  • Joined: 14-March 10

Re: question, reading txt file vb2010

Posted 22 June 2011 - 11:20 AM

Quote

but i can't get it to open the initial directory to open my names.txt

The open file dialog box will go to "C:\FS_Inc\" directory and you will be required to select the file you need to read.
Also you need to make sure if user click OK, otherwise you will get error if user click cancel since there will be no file selected
Was This Post Helpful? 0
  • +
  • -

#9 Chillidog  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 06-June 11

Re: question, reading txt file vb2010

Posted 22 June 2011 - 11:32 AM

thats the problem, there is no error when i try to open. if i change openFD to the orginal OpenFileDialog, i get an error that reference to a non-shared member needs a object reference. like this...
OpenFileDialog.InitialDirectory

but for what ever reason, i get no syntax error or runtime error with openFD, but syntax error with OpenFileDialog

I thought OpenFileDialog was a class...why then a object reference? and what reference to what object?

This post has been edited by Chillidog: 22 June 2011 - 11:33 AM

Was This Post Helpful? 0
  • +
  • -

#10 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1817
  • View blog
  • Posts: 4,625
  • Joined: 14-March 10

Re: question, reading txt file vb2010

Posted 22 June 2011 - 11:39 AM

I can fill list box using this line of code:
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        OpenFileDialog1.InitialDirectory = "D:\"
        OpenFileDialog1.ShowDialog()
        Dim srdNamesFile = New System.IO.StreamReader(OpenFileDialog1.FileName)
        Do Until srdNamesFile.Peek = -1
            lvlogs.Items.Add(srdNamesFile.ReadLine())
        Loop
    End Sub 


EDIT: OpenFileDialog is a class name o you cant use it as an object name(cause confusion), use like OpenFileDialog1 or other name :)

This post has been edited by smohd: 22 June 2011 - 11:42 AM

Was This Post Helpful? 1
  • +
  • -

#11 Btu  Icon User is offline

  • D.I.C Regular

Reputation: 36
  • View blog
  • Posts: 250
  • Joined: 16-May 11

Re: question, reading txt file vb2010

Posted 22 June 2011 - 11:39 AM

Just to clear something up, a class is basically an object. When you create an instance of an object, it inherits all the properties described in the class.

Your openfiledialog might need to be instantiated with the "new" keyword. I can't find it in your code though.
Was This Post Helpful? 1
  • +
  • -

#12 Chillidog  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 06-June 11

Re: question, reading txt file vb2010

Posted 22 June 2011 - 11:41 AM

thanks guys! i have been working on this all day...let me try this...again many thanks!!
Was This Post Helpful? 0
  • +
  • -

#13 Btu  Icon User is offline

  • D.I.C Regular

Reputation: 36
  • View blog
  • Posts: 250
  • Joined: 16-May 11

Re: question, reading txt file vb2010

Posted 22 June 2011 - 11:53 AM

View PostChillidog, on 22 June 2011 - 11:41 AM, said:

thanks guys! i have been working on this all day...let me try this...again many thanks!!

 Dim openFileDialog1 As New OpenFileDialog()

        openFileDialog1.InitialDirectory = "c:\"
        openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
        openFileDialog1.FilterIndex = 2
        openFileDialog1.RestoreDirectory = True


Source: OpenFileDialog

This post has been edited by Btu: 22 June 2011 - 11:54 AM

Was This Post Helpful? 1
  • +
  • -

#14 Chillidog  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 06-June 11

Re: question, reading txt file vb2010

Posted 22 June 2011 - 11:55 AM

haha! it works! thats awesome! though I may be back...thanks for helping out such a n00b

This post has been edited by Chillidog: 22 June 2011 - 11:56 AM

Was This Post Helpful? 0
  • +
  • -

#15 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1817
  • View blog
  • Posts: 4,625
  • Joined: 14-March 10

Re: question, reading txt file vb2010

Posted 22 June 2011 - 12:06 PM

Glad we could help
Was This Post Helpful? 0
  • +
  • -

  • (5 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »