TechTalkz.com Logo Ask the Experts!

Go Back   TechTalkz.com Technology & Computer Troubleshooting Forums > Tech Support Archives > Programing Languages > VB.NET

Display a text file with a ListView

VB.NET

 
 
Thread Tools Display Modes
Unread 27-09-2007, 02:02 PM   #1
Marco
Guest
 
Posts: n/a
Display a text file with a ListView

Hi everybody,
I should create a little utility that can give the chance to view a
log file, created by an external application, in .txt format; for
example I could have a similar log file:

27/09/2007 15:47:00 operator1 GENERAL CYCLE STARTED
27/09/2007 15:48:19 operator2 GENERAL CYCLE SUSPENDED

The file is only one and it has a fixed name in a fixed path;at
maximum I could make a control if the file exists.
I'm not a VB programmer,my experience is limited at some VBS
scripting, so if anyone could give me any suggestion, I will be very
grateful with him.

Cheers.
Marco.

 
Unread 27-09-2007, 03:03 PM   #2
rowe_newsgroups
Guest
 
Posts: n/a
Re: Display a text file with a ListView

On Sep 27, 9:57 am, Marco <pesk...@tin.it> wrote:
> Hi everybody,
> I should create a little utility that can give the chance to view a
> log file, created by an external application, in .txt format; for
> example I could have a similar log file:
>
> 27/09/2007 15:47:00 operator1 GENERAL CYCLE STARTED
> 27/09/2007 15:48:19 operator2 GENERAL CYCLE SUSPENDED
>
> The file is only one and it has a fixed name in a fixed path;at
> maximum I could make a control if the file exists.
> I'm not a VB programmer,my experience is limited at some VBS
> scripting, so if anyone could give me any suggestion, I will be very
> grateful with him.
>
> Cheers.
> Marco.


You can use a StreamReader's ReadLine method to grab one line at a
time and add it to the ListView. Below is a sample, all you need to do
is create a new windows app and replace the code for Form1 with mine
(and change the file path).

////////////////////////
Imports System.IO

Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim lv As New ListView()
lv.Dock = DockStyle.Fill
lv.View = View.List

Me.Controls.Add(lv)

'// You'll need to change this to your file's path
Dim txtFile As String = "C:\Documents and Settings\srowe
\Desktop\document.txt"

If File.Exists(txtFile) Then
Using sr As New StreamReader(txtFile)
While Not sr.EndOfStream
lv.Items.Add(sr.ReadLine())
End While
End Using
Else
MsgBox("Text file not found!")
End If
End Sub

End Class
////////////////////////

Thanks,

Seth Rowe

 
Unread 27-09-2007, 03:03 PM   #3
Marco
Guest
 
Posts: n/a
Re: Display a text file with a ListView

Hi Seth,

I'll try your code; thanks for your help.

Bye.
Marco


 
Unread 27-09-2007, 03:03 PM   #4
Marco
Guest
 
Posts: n/a
Re: Display a text file with a ListView

Hi Seth,

I'm sorry but I didn't explain correctly the result that I want to
obtain.
Every fields in a row will be separated by a special character (for
example or by a tabulation; I want to display separated columns of
a row.

Date Time Operator Command
27/09/2007 15:47:00 operator1 GENERAL CYCLE STARTED
27/09/2007 15:48:19 operator2 GENERAL CYCLE SUSPENDED

So I can order the row displayed by Date, Time, or by Operator, etc.

Can you give me another suggestion?

Thanks.
Marco

 
Unread 27-09-2007, 05:02 PM   #5
Andrew Morton
Guest
 
Posts: n/a
Re: Display a text file with a ListView

Marco wrote:
> I'm sorry but I didn't explain correctly the result that I want to
> obtain.
> Every fields in a row will be separated by a special character (for
> example or by a tabulation; I want to display separated columns of
> a row.
>
> Date Time Operator Command
> 27/09/2007 15:47:00 operator1 GENERAL CYCLE STARTED
> 27/09/2007 15:48:19 operator2 GENERAL CYCLE SUSPENDED
>
> So I can order the row displayed by Date, Time, or by Operator, etc.
>
> Can you give me another suggestion?


It sounds like you could parse the data into a DataTable and then display it
in a DataGrid.

Andrew


 
Unread 27-09-2007, 06:03 PM   #6
rowe_newsgroups
Guest
 
Posts: n/a
Re: Display a text file with a ListView

On Sep 27, 10:59 am, Marco <pesk...@tin.it> wrote:
> Hi Seth,
>
> I'm sorry but I didn't explain correctly the result that I want to
> obtain.
> Every fields in a row will be separated by a special character (for
> example or by a tabulation; I want to display separated columns of
> a row.
>
> Date Time Operator Command
> 27/09/2007 15:47:00 operator1 GENERAL CYCLE STARTED
> 27/09/2007 15:48:19 operator2 GENERAL CYCLE SUSPENDED
>
> So I can order the row displayed by Date, Time, or by Operator, etc.
>
> Can you give me another suggestion?
>
> Thanks.
> Marco


The following code assumes the text file is delimited by semi-colons.
The sample text I used was:

////////////////
27/09/2007; 15:47:00; operator1; GENERAL CYCLE STARTED
27/09/2007; 15:48:19; operator2; GENERAL CYCLE SUSPENDED
///////////////

Here's the actual code:

//////////////
Imports System.IO

Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim lv As New ListView()
lv.Dock = DockStyle.Fill
lv.View = View.Details
lv.HeaderStyle = ColumnHeaderStyle.Clickable
lv.Columns.Add("Date", 100)
lv.Columns.Add("Time", 100)
lv.Columns.Add("Operator", 100)
lv.Columns.Add("Command", 200)

Me.Controls.Add(lv)
Me.Width = 550

'// You'll need to change this to your file's path
Dim txtFile As String = "C:\Documents and Settings\srowe
\Desktop\document.txt"

If File.Exists(txtFile) Then
Using sr As New StreamReader(txtFile)
While Not sr.EndOfStream
Dim lineText As String() =
sr.ReadLine().Split(";"c)

If lineText.Length = 4 Then
lv.Items.Add(New ListViewItem(lineText))
End If
End While
End Using
Else
MsgBox("Text file not found!")
End If
End Sub

End Class
//////////////

Thanks,

Seth Rowe

 
Unread 28-09-2007, 08:00 AM   #7
Marco
Guest
 
Posts: n/a
Re: Display a text file with a ListView

On 27 Set, 19:10, rowe_newsgroups <rowe_em...***********> wrote:
> On Sep 27, 10:59 am, Marco <pesk...@tin.it> wrote:
>
>
>
>
>
> > Hi Seth,

>
> > I'm sorry but I didn't explain correctly the result that I want to
> > obtain.
> > Every fields in a row will be separated by a special character (for
> > example or by a tabulation; I want to display separated columns of
> > a row.

>
> > Date Time Operator Command
> > 27/09/2007 15:47:00 operator1 GENERAL CYCLE STARTED
> > 27/09/2007 15:48:19 operator2 GENERAL CYCLE SUSPENDED

>
> > So I can order the row displayed by Date, Time, or by Operator, etc.

>
> > Can you give me another suggestion?

>
> > Thanks.
> > Marco

>
> The following code assumes the text file is delimited by semi-colons.
> The sample text I used was:
>
> ////////////////
> 27/09/2007; 15:47:00; operator1; GENERAL CYCLE STARTED
> 27/09/2007; 15:48:19; operator2; GENERAL CYCLE SUSPENDED
> ///////////////
>
> Here's the actual code:
>
> //////////////
> Imports System.IO
>
> Public Class Form1
>
> Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
> System.EventArgs) Handles MyBase.Load
> Dim lv As New ListView()
> lv.Dock = DockStyle.Fill
> lv.View = View.Details
> lv.HeaderStyle = ColumnHeaderStyle.Clickable
> lv.Columns.Add("Date", 100)
> lv.Columns.Add("Time", 100)
> lv.Columns.Add("Operator", 100)
> lv.Columns.Add("Command", 200)
>
> Me.Controls.Add(lv)
> Me.Width = 550
>
> '// You'll need to change this to your file's path
> Dim txtFile As String = "C:\Documents and Settings\srowe
> \Desktop\document.txt"
>
> If File.Exists(txtFile) Then
> Using sr As New StreamReader(txtFile)
> While Not sr.EndOfStream
> Dim lineText As String() =
> sr.ReadLine().Split(";"c)
>
> If lineText.Length = 4 Then
> lv.Items.Add(New ListViewItem(lineText))
> End If
> End While
> End Using
> Else
> MsgBox("Text file not found!")
> End If
> End Sub
>
> End Class
> //////////////
>
> Thanks,
>
> Seth Rowe- Nascondi testo tra virgolette -
>
> - Mostra testo tra virgolette -


Seth, thank you very much for your support.

Cheers.
Marco


 
 

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Q How to display Hindi text on JSP using Resource Bundle and properties file ? Amit Jain Java 2 19-09-2007 10:02 AM
Word 2007 does not display text or cursor Gary Microsoft Office 1 29-08-2007 02:00 PM
How to search for hyperlinks that use display text HankJ8195 Microsoft Office 3 28-08-2007 01:19 PM
paint in xp will not display text tool box neldaj Microsoft Office 4 28-08-2007 10:42 AM
Display text fragments Barry Karas Windows XP 4 16-08-2007 05:01 PM


< Home - Windows Help - MS Office Help - Hardware Support >


New To Site? Need Help?

All times are GMT. The time now is 12:01 AM.


vBulletin, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO
Copyright © 2005-2013, TechTalkz.com. All Rights Reserved - Privacy Policy
Valid XHTML 1.0 Transitional