Results 1 to 6 of 6

Thread: [Help] Reading and Sorting Text from a .txt

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2011
    Posts
    3

    [Help] Reading and Sorting Text from a .txt

    Okay so I am very new to visual basic I am currently using VB 2010 I know that this is not the correct forum but I figured it was the closest to it since there is no VB 2010.

    Anyways I am making a program for my Dad's company that can keep track of inventory so I need to store the info in a .txt and load it up etc. I currently am using a stream writer to write to the text and I got that down pat I am just having troubles loading it back up and putting the info in specific text boxes aka sorting or parsing? the information. I have looked at several tutorials and the problem is that I just simply don't know what I am looking at I've been tinkering with it for a couple hours now with no luck so I hope you guys could help me out .

    Here is my current code. It is kind of useless because I have tried so many methods I thought maybe I could use a ReadAll function have it go to a string and parse from there hoping it would have been easier but it has not worked out for me so far.

    Code:
    Imports System.IO
    
    Public Class FormPartList
        Dim fileReader As System.IO.StreamReader
        Dim Path As String = "c:\testfolder1\test.txt"
        Dim StrStorage As String
    
        Dim StrTest As String
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
    
            If File.Exists(Path) Then
                Using tr As TextReader = New StreamReader(Path)
    
                    StrStorage = tr.ReadToEnd()
                    
                End Using
            End If
    
            TextBox1.Text = StrStorage
    
        End Sub
    End Class
    This was just for testing purposes to see if I could get the reading to go the current code does read the text file and TextBox1.text displays all of the information in the text file (test.txt)
    Code:
    ***NEW ITEM**1
    254-2
    Drawing Number: 254-2
    Part Name: Mikes Part
    Material: Aluminum
    Finish: Brushed
    Price: 50.00
    Invoice: 323
    Shipping: 654-256
    Quantity: 80
    Purchase Order: 956
    ^This above is just the output in test.txt and what will eventually need to get sorted into respective text/list boxes

    Thank you in advance I'm hoping someone could help me out here

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: [Help] Reading and Sorting Text from a .txt

    Welcome to VBForums

    Thread moved from the 'VB6 and Earlier' forum to the 'VB.Net' (VB2002 and later) forum

  3. #3

    Thread Starter
    New Member
    Join Date
    Jul 2011
    Posts
    3

    Re: [Help] Reading and Sorting Text from a .txt

    haha ty for the moving stupid question lol is VB 2010 VB.net??

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: [Help] Reading and Sorting Text from a .txt

    Yes, as implied by my post and the forum description, VB 2002 and later (which includes VB 2010) are all VB.Net

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,419

    Re: [Help] Reading and Sorting Text from a .txt

    try this. it reads your textfile, discards the first 2 lines (let me know if that's wrong), + splits the next 9 lines on the ": " + puts the second part of those lines in individual textboxes:

    vb Code:
    1. Public Class Form1
    2.  
    3.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    4.         Dim lines() As String = IO.File.ReadAllLines("c:\testfolder1\test.txt")
    5.         Dim textboxes() As TextBox = {TextBox1, TextBox2, TextBox3, TextBox4, TextBox5, TextBox6, TextBox7, TextBox8, TextBox9}
    6.         Array.ForEach(textboxes, Sub(tb) tb.Text = lines(2 + Array.IndexOf(textboxes, tb)).Split(New String() {": "}, StringSplitOptions.None)(1))
    7.     End Sub
    8.  
    9. End Class

  6. #6
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: [Help] Reading and Sorting Text from a .txt

    If you are to use a text file as a way of storing tabular data, you at least should pick the appropriate format such as csv or tab delimited where each text line represents a record and the commas or the tabs are field separators.
    However, for a program to be used in real business (your Dad's company), I strongly advice the use of a database.
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width