Reading delimited text content
Reading delimited text content
Reading delimited text content
Reading delimited text content
Reading delimited text content
Reading delimited text content Reading delimited text content Reading delimited text content Reading delimited text content Reading delimited text content Reading delimited text content Reading delimited text content Reading delimited text content
Reading delimited text content Reading delimited text content
Reading delimited text content
Go Back  Xtreme Visual Basic Talk > > > Reading delimited text content


Reply
 
Thread Tools Display Modes
  #1  
Old 12-23-2004, 10:26 PM
kobezt08 kobezt08 is offline
Regular
 
Join Date: Dec 2004
Posts: 72
Question Reading delimited text content


hi I am having this problem where i have a Text file with data

000189,380661,SUPERSTAR VIRGO,410081,0813,2.200,5.76,12.67,.....

which i need to read and spreadly placed into selected colume of a ListView.

Below is the VB.net code i currently have which i dun really know how to improve on:
...
If (openFiledlg.CheckFileExists()) Then
flag = True
txtSAPsrc.Text = openFiledlg.FileName
FileOpen(1, openFiledlg.FileName, OpenMode.Input)
Do While Not EOF(1)
Input(1, str)
If Microsoft.VisualBasic.Left(LTrim(str), 1) <> "," And Trim(str) <> "" Then

mitem = New ListViewItem(str.Split(","), 0)
lstvReport.Items.AddRange(New ListViewItem() {mitem})

End If
Loop

End If
.....

Help greatly appreciated.
Reply With Quote
  #2  
Old 12-24-2004, 08:30 AM
AFterlife's Avatar
AFterlife AFterlife is offline
Contributor
 
Join Date: Dec 2003
Location: Mi
Posts: 654
Default

Ok, is this VB6? If you are using .NET then you are using all legacy functions.Then i would post in the legacy section. Otherwise you have a lot to learn. If this is .NET use the stream reader class. Here is a tutorial on File I/O in VB.NET.

File I/O In VB.Net

But as far as your scenario goes ...What i personally would do for something like that would be create a class.That has propertys to be set for each row in the text file. Lets call it Class Personal. Then i would create another class called PersonalList. In this class you would loop through each line of the file and instantiate a new Personal Class then Split them into a string array. Then using the items you split into the string array to set all the property's for the new Personal class you instantiated after all the propertys have been set you would add that item to a collection.Then you could have 2 methods one would return the count in the collection and the other would return the index to the collection that you specify. It would make it so much better IMHO. But first learn the .NET way of File I/O. Hopefully that makes sense. It would be better to learn a more OO way of doing things then a procedural solution. Then it would be very simple to loop through your PersonalList and add them to the ListView.

Last edited by AFterlife; 12-24-2004 at 08:36 AM.
Reply With Quote
  #3  
Old 12-24-2004, 10:48 PM
kobezt08 kobezt08 is offline
Regular
 
Join Date: Dec 2004
Posts: 72
Default

Quote:
Originally Posted by AFterlife
Ok, is this VB6? If you are using .NET then you are using all legacy functions.Then i would post in the legacy section. Otherwise you have a lot to learn. If this is .NET use the stream reader class. Here is a tutorial on File I/O in VB.NET.

File I/O In VB.Net

But as far as your scenario goes ...What i personally would do for something like that would be create a class.That has propertys to be set for each row in the text file. Lets call it Class Personal. Then i would create another class called PersonalList. In this class you would loop through each line of the file and instantiate a new Personal Class then Split them into a string array. Then using the items you split into the string array to set all the property's for the new Personal class you instantiated after all the propertys have been set you would add that item to a collection.Then you could have 2 methods one would return the count in the collection and the other would return the index to the collection that you specify. It would make it so much better IMHO. But first learn the .NET way of File I/O. Hopefully that makes sense. It would be better to learn a more OO way of doing things then a procedural solution. Then it would be very simple to loop through your PersonalList and add them to the ListView.
Thanks fo the reply.

Firstly, im using VB.Net, but b4 this i was given 3 functions code which is part of a program that is doing just what i want. Unfortunately, they are all in VB6 which certain methods such as spliting of the data by Comma is missing.

Secondly, I don't really get what you mean by
Quote:
Then i would create another class called PersonalList. In this class you would loop through each line of the file and instantiate a new Personal Class then Split them into a string array. Then using the items you split into the string array to set all the property's for the new Personal class you instantiated after all the propertys have been set you would add that item to a collection.
is there any1 to find a sample tutorial on the above method?
Sorry for the trouble

Last edited by kobezt08; 12-24-2004 at 11:15 PM.
Reply With Quote
  #4  
Old 12-25-2004, 07:28 AM
AFterlife's Avatar
AFterlife AFterlife is offline
Contributor
 
Join Date: Dec 2003
Location: Mi
Posts: 654
Default

Ill post a simple example later.
Reply With Quote
  #5  
Old 12-25-2004, 08:54 AM
AFterlife's Avatar
AFterlife AFterlife is offline
Contributor
 
Join Date: Dec 2003
Location: Mi
Posts: 654
Default

Ok. I worked out a simple example for you.
You would have a class personal:

Code:
Public Class CPersonal Private _myFirstItem As Long, _mySecondItem As Long, _myHoroscopeItem As String, _myFourthItem As Long, _ _myFifthItem As Long, _mySixthItem As Double, _mySeventhItem As Double, _myEighthItem As Double 'The above ^^^^^^^ variables are used to set the propertys Public Property First() As Long Get Return _myFirstItem End Get Set(ByVal Value As Long) _myFirstItem = Value End Set End Property Public Property Second() Get Return _mySecondItem End Get Set(ByVal Value) _mySecondItem = Value End Set End Property Public Property Horoscope() As String Get Return _myHoroscopeItem End Get Set(ByVal Value As String) _myHoroscopeItem = Value End Set End Property Public Property Fourth() As Long Get Return _myFourthItem End Get Set(ByVal Value As Long) _myFourthItem = Value End Set End Property Public Property Fifth() As Long Get Return _myFifthItem End Get Set(ByVal Value As Long) _myFifthItem = Value End Set End Property Public Property Sixth() As Double Get Return _mySixthItem End Get Set(ByVal Value As Double) _mySixthItem = Value End Set End Property Public Property Seventh() As Double Get Return _mySeventhItem End Get Set(ByVal Value As Double) _mySeventhItem = Value End Set End Property Public Property Eighth() As Double Get Return _myEighthItem End Get Set(ByVal Value As Double) _myEighthItem = Value End Set End Property End Class

Then you would have a class called PersonalList:

Code:
Public Class CPersonalList Private _PersonalObjects As New Collection 'collection holds instances of personal objects Public Sub New() End Sub Public Sub LoadPersonalObjects(ByVal Filename As String) Dim ReadFile As New System.IO.StreamReader(Filename) Dim PersonalInfo As String, PersonalSplit As String() Dim Personal As CPersonal 'read employees from file Do Until ReadFile.Peek = -1 PersonalInfo = ReadFile.ReadLine Personal = New CPersonal PersonalSplit = PersonalInfo.Split(",") Personal.First = PersonalSplit(0) Personal.Second = PersonalSplit(1) Personal.Horoscope = PersonalSplit(2) Personal.Fourth = PersonalSplit(3) Personal.Fifth = PersonalSplit(4) Personal.Sixth = PersonalSplit(5) Personal.Seventh = PersonalSplit(6) Personal.eighth = PersonalSplit(7) 'stores instances of Personal objects and collects them into this array _PersonalObjects.Add(Personal) Loop ReadFile.Close() End Sub Public Function GetObjectCount() As Integer Return _PersonalObjects.Count End Function Public Function getObject(ByVal index As Integer) As CPersonal Return _PersonalObjects.Item(index) End Function End Class
Then in your main form you would have :
Code:
Private Sub cmdGet_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdGet.Click open.ShowDialog() Dim myObjectsFromFile As New CPersonalList myObjectsFromFile.LoadPersonalObjects(open.FileName) Dim i As Integer For i = 0 To myObjectsFromFile.GetObjectCount - 1 lvwObjects.Items.Add(myObjectsFromFile.getObject(i + 1).First) lvwObjects.Items(i).SubItems.Add(myObjectsFromFile.getObject(i + 1).Second) lvwObjects.Items(i).SubItems.Add(myObjectsFromFile.getObject(i + 1).Horoscope) lvwObjects.Items(i).SubItems.Add(myObjectsFromFile.getObject(i + 1).Fourth) lvwObjects.Items(i).SubItems.Add(myObjectsFromFile.getObject(i + 1).Fifth) lvwObjects.Items(i).SubItems.Add(myObjectsFromFile.getObject(i + 1).Sixth) lvwObjects.Items(i).SubItems.Add(myObjectsFromFile.getObject(i + 1).Seventh) lvwObjects.Items(i).SubItems.Add(myObjectsFromFile.getObject(i + 1).Eighth) Next End Sub
Its christmas and im feeling real nice. Im about to leave. Ill even give you the example project.
Attached Files
File Type: zip WindowsApplication2.zip (27.8 KB, 11 views)
Reply With Quote
  #6  
Old 12-25-2004, 09:06 AM
AFterlife's Avatar
AFterlife AFterlife is offline
Contributor
 
Join Date: Dec 2003
Location: Mi
Posts: 654
Default

I failed to mention this only reads lines like structured like this
000189,380661,SUPERSTAR VIRGO,410081,0813,2.200,5.76,12.67
If there is more then that in aline then you would have to add support for those. Via properties. Here is the text file i used to test it.
Attached Files
File Type: txt test.txt (538 Bytes, 8 views)

Last edited by AFterlife; 12-25-2004 at 09:08 AM. Reason: Failed to include test file
Reply With Quote
  #7  
Old 12-25-2004, 11:39 AM
kobezt08 kobezt08 is offline
Regular
 
Join Date: Dec 2004
Posts: 72
Default

I see now i understand...
Thanks for the help.
Merry Christmas! Cheers~~~
Reply With Quote
  #8  
Old 12-29-2004, 07:14 AM
kobezt08 kobezt08 is offline
Regular
 
Join Date: Dec 2004
Posts: 72
Default

now i meet with yet another problem, how can i specify the column for each data to be displayed? i have tried replacing the "i" in Item()

lvwObjects.Items(i).SubItems.Add(....)

but was unsucessful
sorry if it was a newbie question
Reply With Quote
  #9  
Old 12-29-2004, 07:39 AM
AFterlife's Avatar
AFterlife AFterlife is offline
Contributor
 
Join Date: Dec 2003
Location: Mi
Posts: 654
Default

You ran the project? It puts each item in there own column.
Thats what this does:
Code:
open.ShowDialog() Dim myObjectsFromFile As New CPersonalList myObjectsFromFile.LoadPersonalObjects(open.FileName) Dim i As Integer For i = 0 To myObjectsFromFile.GetObjectCount - 1 lvwObjects.Items.Add(myObjectsFromFile.getObject(i + 1).First) lvwObjects.Items(i).SubItems.Add(myObjectsFromFile.getObject(i + 1).Second) lvwObjects.Items(i).SubItems.Add(myObjectsFromFile.getObject(i + 1).Horoscope) lvwObjects.Items(i).SubItems.Add(myObjectsFromFile.getObject(i + 1).Fourth) lvwObjects.Items(i).SubItems.Add(myObjectsFromFile.getObject(i + 1).Fifth) lvwObjects.Items(i).SubItems.Add(myObjectsFromFile.getObject(i + 1).Sixth) lvwObjects.Items(i).SubItems.Add(myObjectsFromFile.getObject(i + 1).Seventh) lvwObjects.Items(i).SubItems.Add(myObjectsFromFile.getObject(i + 1).Eighth) Next End Sub
Do you you have your listview set to details? Or im i thinking about the wrong thing?
Reply With Quote
  #10  
Old 12-29-2004, 07:49 AM
kobezt08 kobezt08 is offline
Regular
 
Join Date: Dec 2004
Posts: 72
Default

Quote:
Originally Posted by AFterlife
You ran the project? It puts each item in there own column.
Thats what this does:
Code:
open.ShowDialog() Dim myObjectsFromFile As New CPersonalList myObjectsFromFile.LoadPersonalObjects(open.FileName) Dim i As Integer For i = 0 To myObjectsFromFile.GetObjectCount - 1 lvwObjects.Items.Add(myObjectsFromFile.getObject(i + 1).First) lvwObjects.Items(i).SubItems.Add(myObjectsFromFile.getObject(i + 1).Second) lvwObjects.Items(i).SubItems.Add(myObjectsFromFile.getObject(i + 1).Horoscope) lvwObjects.Items(i).SubItems.Add(myObjectsFromFile.getObject(i + 1).Fourth) lvwObjects.Items(i).SubItems.Add(myObjectsFromFile.getObject(i + 1).Fifth) lvwObjects.Items(i).SubItems.Add(myObjectsFromFile.getObject(i + 1).Sixth) lvwObjects.Items(i).SubItems.Add(myObjectsFromFile.getObject(i + 1).Seventh) lvwObjects.Items(i).SubItems.Add(myObjectsFromFile.getObject(i + 1).Eighth) Next End Sub
Do you you have your listview set to details? Or im i thinking about the wrong thing?
yeah i set to details. I applied the method learnt from u onto my project which have abit more datas and more cloumns, where some colume will be filled wif data from an access database later on
Reply With Quote
  #11  
Old 12-29-2004, 08:03 AM
AFterlife's Avatar
AFterlife AFterlife is offline
Contributor
 
Join Date: Dec 2003
Location: Mi
Posts: 654
Default

Each subsequent subitem in the for loop represents a new column. Is this a new Listview or is this the same listview that contains the data from the text file? If you are using an access database. Then i wouldnt even use a text file. I would import the data(textfile) into access. Then using the same methodology with a datareader object instead of a stream reader object. Is this what you are asking?
Reply With Quote
  #12  
Old 12-29-2004, 08:17 AM
kobezt08 kobezt08 is offline
Regular
 
Join Date: Dec 2004
Posts: 72
Default

Quote:
Originally Posted by AFterlife
Each subsequent subitem in the for loop represents a new column. Is this a new Listview or is this the same listview that contains the data from the text file? If you are using an access database. Then i wouldnt even use a text file. I would import the data(textfile) into access. Then using the same methodology with a datareader object instead of a stream reader object. Is this what you are asking?

the whole string of data in a line looks like this

000189,380661,SUPERSTAR VIRGO,410081,0813,2.200,5.76,12.67,0050600217,20041121,WATER FROM M,K.LTR,WM0094,20041117,4.22,7.027,11.251,0.45,5.063,0050601001,46.41

basically the program im doing required the extraction of info from both text file and data base and subsequently check is if the data matches on both side.
Reply With Quote
  #13  
Old 12-29-2004, 08:30 AM
AFterlife's Avatar
AFterlife AFterlife is offline
Contributor
 
Join Date: Dec 2003
Location: Mi
Posts: 654
Default

Quote:
Originally Posted by kobezt08
the whole string of data in a line looks like this

000189,380661,SUPERSTAR VIRGO,410081,0813,2.200,5.76,12.67,0050600217,20041121,WATER FROM M,K.LTR,WM0094,20041117,4.22,7.027,11.251,0.45,5.063,0050601001,46.41

basically the program im doing required the extraction of info from both text file and data base and subsequently check is if the data matches on both side.
Ok. All you need to do is modify the code i gave you and add more properties to account for the extra fields in your text file(Personal Class). Via properties. Then in the Personal List Class you would have to set those additional properties. Then back in the main form you would have to add additional subitems in the for loop to account for the extra fields. That you set via properties in the Personal List class. That will get you the extra columns to put in to the listview. Make sense? Remember the text file i used for a an example only had 8 columns.Thats why you will have to make some slight changes.
Reply With Quote
  #14  
Old 12-29-2004, 08:35 AM
kobezt08 kobezt08 is offline
Regular
 
Join Date: Dec 2004
Posts: 72
Default

Quote:
Originally Posted by AFterlife
Ok. All you need to do is modify the code i gave you and add more properties to account for the extra fields in your text file(Personal Class). Via properties. Then in the Personal List Class you would have to set those additional properties. Then back in the main form you would have to add additional subitems in the for loop to account for the extra fields. That you set via properties in the Personal List class. That will get you the extra columns to put in to the listview. Make sense? Remember the text file i used for a an example only had 8 columns.Thats why you will have to make some slight changes.
yeah i did all that this afternoon, and was able to display all datat in Listview column 1 - 20. But now is i need to separately place the data into their rite full columns as some columns are meant for the data fromt eh database.

Sorry to trouble u AFterlife
Reply With Quote
  #15  
Old 12-29-2004, 08:35 AM
AFterlife's Avatar
AFterlife AFterlife is offline
Contributor
 
Join Date: Dec 2003
Location: Mi
Posts: 654
Default

Also you can add columns via the columns collection in the propertys window of the control. Rememeber each item has an array of subitems. So that the sub item columnn is represented as a numeric value based on its position in the columns collection. .Subitems(0) .subitems(1) .Subitems(2) ...etc...etc..
Its ok. I just woke up and im not all together yet. Im working on a program of my own at the same time. Need caffeine now. So to add columns you would have to specify which column to add to and which explicit row by the .Item() index.

Last edited by AFterlife; 12-29-2004 at 08:40 AM.
Reply With Quote
  #16  
Old 12-29-2004, 08:49 AM
kobezt08 kobezt08 is offline
Regular
 
Join Date: Dec 2004
Posts: 72
Default

Quote:
Originally Posted by AFterlife
Also you can add columns via the columns collection in the propertys window of the control. Rememeber each item has an array of subitems. So that the sub item columnn is represented as a numeric value based on its position in the columns collection. .Subitems(0) .subitems(1) .Subitems(2) ...etc...etc..
Its ok. I just woke up and im not all together yet. Im working on a program of my own at the same time. Need caffeine now. So to add columns you would have to specify which column to add to and which explicit row by the .Item() index.

I still dun get it maybe cos im a bit messy up by all the Items sub items, columns and stuff as this is the very 1st time i'm using a listview.

is Items refering to Columns?
Reply With Quote
  #17  
Old 12-29-2004, 09:02 AM
AFterlife's Avatar
AFterlife AFterlife is offline
Contributor
 
Join Date: Dec 2003
Location: Mi
Posts: 654
Default

No. The .Item() represents the Row. Or the first column in the listview. The .SubItem() represents the columns after the first item. so .Item()=Row
so if you said .item(0) that would represent the first column in the first row.
If you said .Item(1) that would represent the 2nd row in the first column.
Each item has its own collection of subitems which are represented as columns. So if you said .Item(0).subitems(1) you would be refering to the data in the first row of the second column.
Reply With Quote
  #18  
Old 12-29-2004, 09:13 AM
kobezt08 kobezt08 is offline
Regular
 
Join Date: Dec 2004
Posts: 72
Default

Quote:
Originally Posted by AFterlife
No. The .Item() represents the Row. Or the first column in the listview. The .SubItem() represents the columns after the first item. so .Item()=Row
so if you said .item(0) that would represent the first column in the first row.
If you said .Item(1) that would represent the 2nd row in the first column.
Each item has its own collection of subitems which are represented as columns. So if you said .Item(0).subitems(1) you would be refering to the data in the first row of the second column.

Code:
open.ShowDialog()
        Dim myObjectsFromFile As New CPersonalList
        myObjectsFromFile.LoadPersonalObjects(open.FileName)
        Dim i As Integer
        For i = 0 To myObjectsFromFile.GetObjectCount - 1
            lvwObjects.Items.Add(myObjectsFromFile.getObject(i + 1).First)
            lvwObjects.Items(i).SubItems.Add(myObjectsFromFile.getObject(i + 1).Second)
            lvwObjects.Items(i).SubItems.Add(myObjectsFromFile.getObject(i + 1).Horoscope)
            lvwObjects.Items(i).SubItems.Add(myObjectsFromFile.getObject(i + 1).Fourth)
            lvwObjects.Items(i).SubItems.Add(myObjectsFromFile.getObject(i + 1).Fifth)
            lvwObjects.Items(i).SubItems.Add(myObjectsFromFile.getObject(i + 1).Sixth)
            lvwObjects.Items(i).SubItems.Add(myObjectsFromFile.getObject(i + 1).Seventh)
            lvwObjects.Items(i).SubItems.Add(myObjectsFromFile.getObject(i + 1).Eighth)
        Next
    End Sub
oic, that means from the code above "First" is added into the 1st row 1st column. So if i wan "Second" which is currently in the 1st row 2nd column to be on the 1st row 4th column, i juz need to code it like lvwObjects.Items(i).SubItems(3).Add(myObjectsFromFile.getObject(i + 1).Second)?
Reply With Quote
  #19  
Old 12-29-2004, 09:17 AM
AFterlife's Avatar
AFterlife AFterlife is offline
Contributor
 
Join Date: Dec 2003
Location: Mi
Posts: 654
Default

That should work.
Reply With Quote
  #20  
Old 12-29-2004, 09:25 AM
kobezt08 kobezt08 is offline
Regular
 
Join Date: Dec 2004
Posts: 72
Default

it's not working, got blue lines from the code
lvwObjects.Items(i).SubItems(3).Add(myObjectsFromFile.getObject(i + 1).Second)
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Reading delimited text content
Reading delimited text content
Reading delimited text content Reading delimited text content
Reading delimited text content
Reading delimited text content
Reading delimited text content Reading delimited text content Reading delimited text content Reading delimited text content Reading delimited text content Reading delimited text content Reading delimited text content
Reading delimited text content
Reading delimited text content
 
Reading delimited text content
Reading delimited text content
 
-->