Display a text file with a ListView

  • Thread starter Thread starter Marco
  • Start date Start date
M

Marco

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.
 
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
 
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
 
Marco said:
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
 
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
 
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
 
Back
Top