14 Replies - 1555 Views - Last Post: 01 August 2011 - 12:37 PM Rate Topic: -----

#1 rayne58  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 28-July 11

Reading and sorting text from a .txt

Posted 28 July 2011 - 06:50 AM

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.

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)
***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 :)

Is This A Good Question/Topic? 0
  • +

Replies To: Reading and sorting text from a .txt

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 8915
  • View blog
  • Posts: 33,430
  • Joined: 12-June 08

Re: Reading and sorting text from a .txt

Posted 28 July 2011 - 07:29 AM

Well you need to stop and think about the data. What signifies a new item? Are all the items formatted the same?

What you should be doing is loading up the data in a collection of sorts. Either a "list of" a custom class or simply a dataset. With the dataset create a table, and then add columns. FYI you should have an idea of the number of columns and their types you want.

Dim foo As New DataSet
foo.Tables.Add()

'-- creates a column called "colA" of type "datetime"
foo.Tables(0).Columns.Add("colA", System.Type.GetType("System.DateTime"))



Alternatively you can create a dataset object in a more graphical nature by right clicking on your project, add, data->dataset. Then you can right click and add a table, name it, and add columns in a more visual option. You'll need to create an instance of the dataset object you visually created in your code.



Data storage aside, personally I would design it like this. The concept is if you have a fixed number of rows you want to be sure you know what column in the new row it is supposed to go to. The easiest way to be sure is to have an integer counter that keeps track of the current column.

If the ** signifies a new item then create a new instance of your dataset's row. Set the counter to 0 and process each row coming in the stream reader per what ever needs to happen. Example - with the row that is "price" I assume you you don't want to the text just the number so the processing for that row would to remove the non numeric characters. After each row is processed you would increment the counter and let the stream reader get the next row.

For flexibility have an open file dialog to point to your file.
A stream reader that opens up that file from the OFD.
A loop that reads each each line of the text file.
with each line go through processing, adding the data to the specific column of the new row, and when the counter reaches the last row add the row to your dataset.

Clear as mud?
Was This Post Helpful? 0
  • +
  • -

#3 rayne58  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 28-July 11

Re: Reading and sorting text from a .txt

Posted 28 July 2011 - 08:12 AM

Hahaha clear as mud is a great way to put it :) I am thoroughly confused lol but with the integer counter I figured I could do that the ***new item***1 the 1 is actually a counter but that is something I figured I wouldnt need now that I started to create it further more. The identifiers would be the first line after the **** the numbers 254-2 etc and that could be what starts the group which in turn would be what populates the text boxes.
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 8915
  • View blog
  • Posts: 33,430
  • Joined: 12-June 08

Re: Reading and sorting text from a .txt

Posted 28 July 2011 - 08:44 AM

Yes, that wasn't the most cojent point I have made this morning.

Let me try again.

Each line in the file is basically a cell's worth of data for a specific column in a dataset, right? You only want specific part of that line (ie the price but not the word "price") so you need to process each line based off some knowledge of what column the data's going into.

Say you have this as the item data. New items start with "**new item **". We know we need to have a new row started. We also know we want that item number (first column). The next column/line is the price we want. The third is the name.

Quote

**new item ** 1
price: 2.00
name: sluggo
**new item ** 2
price: 60.00
name: angry bees
**new item ** 4
price: 50.00
name: mudpies



create dataset
create table
add a column called "id" of type integer
add a column called "price" of type double
add a column called "name" fo type string.

using stream reader open up the file
create a column counter
start the loop

read in a line from the stream reader (sLineIn: "**new item ** 1")
does that line contain "**new item **"? (YES.)
create a new datarow from the dataset (tempRow: | | )
column_counter goes to 0 (column_counter: 0)

select case column_counter '-- for processing
case 0:
remove the "**new item ** " from sLineIn (sLineIn:1)
set the temprow.column(column_counter) to sLineIn (tempRow: 1 | | )
...
end select

does column_counter equal the max number of rows (3)? (NO.)
next in the loop


read in line from stream reader (sLineIn: "price: 2")
does that line contain "**new item **"? (NO.)
increment column_counter (column_counter: 1)
select case column_counter '-- for processing each column
case 1:
remove the "price: " from sLineIn (sLineIn:2.00)
set the temprow.column(column_counter) to sLineIn (tempRow: 1 | 2.00 | )
...
end select
does column_counter equal the max number of rows (3)? (NO.)
next in the loop


read in line from stream reader (sLineIn: "name: sluggo")
does that line contain "**new item **"? (NO.)
increment column_counter (column_counter: 2)
select case column_counter '-- for processing each column
case 2:
remove the "name: " from sLineIn (sLineIn:sluggo)
set the temprow.column(column_counter) to sLineIn (tempRow: 1 | 2.00 | sluggo )
...
end select
does column_counter equal the max number of rows-1 (2)? (YES.)
ADD tempRow to our dataset.
(DATASET:)
1 | 2.00 | sluggo


next in the loop

*** I'll cut out the words and just show the processing of each line **

(sLineIn: "**new item ** 2")
(YES.)
(tempRow: | | )
(column_counter: 0)
(sLineIn:2)
(tempRow: 2 | | )
(NO.)
>>read next line<<

(sLineIn: "price: 60.00")
(no.)
(column_counter: 1)
(sLineIn:60.00)
(tempRow: 2 | 60.00 | )
(NO.)
>>read next line<<

(sLineIn: "name: angry bees")
(no.)
(column_counter: 2)
(sLineIn:angry bees)
(tempRow: 2 | 60.00 | angry bees )
(yes.)

(DATASET:)
1 | 2.00 | sluggo
2 | 60.00 | angry bees

>>read next line<<

(sLineIn: "**new item ** 4")
(YES.)
(tempRow: | | )
(column_counter: 0)
(sLineIn:4)
(tempRow: 4 | | )
(NO.)
>>read next line<<

(sLineIn: "price: 50.00")
(no.)
(column_counter: 1)
(sLineIn:50.00)
(tempRow: 4 | 50.00 | )
(NO.)
>>read next line<<

(sLineIn: "name: mudpies")
(no.)
(column_counter: 2)
(sLineIn:mudpies)
(tempRow: 4 | 50.00 | mudpies )
(yes.)

(DATASET:)
1 | 2.00 | sluggo
2 | 60.00 | angry bees
4 | 50.00 | mudpies

no more lines and your program is done loading!


Just to be clear this is one select case statement I just show the relevant parts each iteration.
Was This Post Helpful? 1
  • +
  • -

#5 rayne58  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 28-July 11

Re: Reading and sorting text from a .txt

Posted 28 July 2011 - 09:01 AM

okay okay making sense to me now we got cloudy water :) lol. My problem now is that I dont know how to code that lol. I cant wait for class to start im taking visual basic haha figured id start teaching myself before it starts.
Was This Post Helpful? 0
  • +
  • -

#6 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 8915
  • View blog
  • Posts: 33,430
  • Joined: 12-June 08

Re: Reading and sorting text from a .txt

Posted 28 July 2011 - 09:07 AM

Quote

I dont know how to code that

Well that blows hard doesn't it?

Break it down to components.
  • Stream reader
  • select case statement
  • loops
  • variables
  • generic datsets
  • generic datarows



Honestly if you are just that green then this might be a bigger project than you want to take a bite out of. It's a good place to aim for, but I would hate to have you get all pissed off and frustrated and quite on the whole programming process.

With my example I wrote that in about ten minutes, and then spent another twenty writing it out in a coherent post. With some basic VB and programming base it won't be too hard for you; it's just the time taken to learning what you need to learn first.

I would highly, strongly, and firmly suggest getting a book. Some structured path from point A to point J. Failing around the internet sans any understanding will just complicate things.
Was This Post Helpful? 0
  • +
  • -

#7 rayne58  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 28-July 11

Re: Reading and sorting text from a .txt

Posted 28 July 2011 - 09:15 AM

Yeahh the book is acquired I just need to mess with it but that little bit the bullets you gave me in exactly what I needed something to start with and mess around with. Ill keep ya updated though when I get this baby working lol thank you for the help with the thought process etc.
Was This Post Helpful? 0
  • +
  • -

#8 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 8915
  • View blog
  • Posts: 33,430
  • Joined: 12-June 08

Re: Reading and sorting text from a .txt

Posted 28 July 2011 - 09:48 AM

No problem chief. Have fun learning!
Was This Post Helpful? 0
  • +
  • -

#9 rayne58  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 28-July 11

Re: Reading and sorting text from a .txt

Posted 01 August 2011 - 08:39 AM

Hello yet again lol first off let me say I am a coding noob and this is a program for my dad's company

Okay so pretty much what I am making is a database to store various information about parts that he has made such as price material etc etc.

I have gotten the program to write to the .txt all of the info that I need it to which is this info

***NEW ITEM**
This is the line i want searched but not displayed
Drawing Number: This is the line i want searched but not displayed'(This Line Will Be Displayed)
Part Name: Test
Material: Test
Finish: Test
Price: 50
Invoice: Test
Shipping: Test
Quantity: 10
Purchase Order: Test

***NEW ITEM**
This is the line i want searched but not displayed
Drawing Number: This is the line i want searched but not displayed'(Will Be Displayed
Part Name: Test
Material: Test
Finish: Test
Price: 60
Invoice: Test
Shipping: Test
Quantity: 15
Purchase Order: Test



So now I need the program to be able to read that file and put it into the respective text box which I have gotten it to do, the only problem is that I cant get to the next part and so on it just displays the first part.

Here is my current code
Imports System.IO

Public Class FormPartList

    Dim Path As String = "c:\testfolder1\test.txt"

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

        Dim lines() As String = IO.File.ReadAllLines(Path)
        Dim textboxes() As TextBox = {TextBox1, TextBox2, TextBox3, TextBox4, TextBox5, TextBox6, TextBox7, TextBox8, TextBox9}
        Array.ForEach(textboxes, Sub(tb) tb.Text = lines(2 + Array.IndexOf(textboxes, tb)).Split(New String() {": "}, StringSplitOptions.None)(1))

    End Sub

End Class



Now this was just my first attempt and still does not have the features that I want to implement I want the user to type into TxtSearch and they will put the part number which is the second line on the text box and it will go and read the respective part but I am not sure how exactly to implement that any help is appreciated :)
Was This Post Helpful? 0
  • +
  • -

#10 raziel_  Icon User is offline

  • Like a lollipop
  • member icon

Reputation: 464
  • View blog
  • Posts: 4,255
  • Joined: 25-March 09

Re: Reading and sorting text from a .txt

Posted 01 August 2011 - 08:51 AM

i would strongly suggest using database like myslq or access even instead of using text files for your particular problem
Was This Post Helpful? 0
  • +
  • -

#11 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2240
  • View blog
  • Posts: 9,411
  • Joined: 29-May 08

Re: Reading and sorting text from a .txt

Posted 01 August 2011 - 08:54 AM

Use the following tutorial as a guide. Simple Script Interpretor
Was This Post Helpful? 0
  • +
  • -

#12 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 8915
  • View blog
  • Posts: 33,430
  • Joined: 12-June 08

Re: Reading and sorting text from a .txt

Posted 01 August 2011 - 08:56 AM

Merging threads for continuity.
Was This Post Helpful? 0
  • +
  • -

#13 rayne58  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 28-July 11

Re: Reading and sorting text from a .txt

Posted 01 August 2011 - 09:02 AM

ty Modi I was looking for my old one but I couldnt find it lol.
And speight what exactly is that? I'm a noob lol looking at that huge code I just lose track of what it is doing :/
Was This Post Helpful? 0
  • +
  • -

#14 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2240
  • View blog
  • Posts: 9,411
  • Joined: 29-May 08

Re: Reading and sorting text from a .txt

Posted 01 August 2011 - 09:26 AM

It would beneficial in you do examine that tutorial especial the part ExecuteCommand.

As it process a text file line by line.

It is simple algorithm.


Is end of file?
No:
Read A Line
Does line contain : ?
Yes: Split line into sections
Select corresponding section
Select Case SL(0)
 Case "Drawing Number"
 Case "Part Name"
 Case "Material"
' etc'
 Case Else
' unknown '
End Select


No: The line must be either:-
blank
***NEW ITEM**

This post has been edited by AdamSpeight2008: 01 August 2011 - 09:27 AM

Was This Post Helpful? 0
  • +
  • -

#15 fixo  Icon User is offline

  • D.I.C Regular

Reputation: 85
  • View blog
  • Posts: 335
  • Joined: 10-May 09

Re: Reading and sorting text from a .txt

Posted 01 August 2011 - 12:37 PM

View Postrayne58, on 01 August 2011 - 09:39 AM, said:

Now this was just my first attempt and still does not have the features that I want to implement I want the user to type into TxtSearch and they will put the part number which is the second line on the text box and it will go and read the respective part but I am not sure how exactly to implement that any help is appreciated :)


See quick example in attachment, just for the start:

http://dl.dropbox.co...ormPartList.rar

But the best way which was suggested by *NoBrain* I think

This post has been edited by fixo: 01 August 2011 - 12:40 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1