I am trying to add the History page in my application which will work like as i shown below:
1st Column: (Date); 2nd column: (Login Time); 3rd Column: (Logout Time)

I added the following code:

Public Class Form1
   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
      ListView1.Items.Add(DateString)
      ListView1.Items(0).SubItems.Add(TimeOfDay)
   End Sub
End Class

This all my efforts now what I am Facing ?
I want to save all the data of this in the settings.
And One more things is I am unknown how to add the Subitem.
The above code will just replace the 1st Items (subitems) as What i want is that it should be changed as for new item...

So I want the fully New code...
Please Help me soon.

When you say that you are saving the data in the settings, are you referring to application settings? If so, then this will be an extremely inefficient method of storage.

Why not store the history in a csv? (If the data is not confidential!)

Then you could load your listview with the data from the CSV.

When saving, create a small function to write to the file and return a value.

For example:

Private Function SaveHistoryItem(ByVal sDateString As String, ByVal dTimeOfDay As DateTime) As Boolean
    Try
        Dim fp As String = My.Settings.FilePath 'String variable in Application settings containing the full path to the CSV File

        'Check if the file exists, if not, create it.
        If Not File.Exists(fp) Then File.Create(fp)

        Dim sw As New StreamWriter(fp, True)

        'Formats the data:
        '01/20/2013,11/21/2013 14:36:04'
        sw.WriteLine(sDateString & "," & dTimeOfDay.ToString)

        sw.Close()
        sw.Dispose()
        fp = Nothing

        Return True 'SAVED
    Catch ex As Exception
        MsgBox(ex.ToString)
        Return False 'FAILED
    End Try
End Function

You can use a function like this like so:

If SaveHistoryItem("01/01/2013",Now()) Then
    'Clean Up
Else
    'Didn't save, do something!
End If

Now, to load the data, write another small function to retreive the values from the CSV file and to pass them to your form.

For example:

Private Function ReadFromHistory() As List(Of List(Of String))
    Try
        Dim fp As String = My.Settings.FilePath

        If Not File.Exists(fp) Then Return New List(Of List(Of String))

        Dim sr As New StreamReader(fp)
        Dim lstStrings As New List(Of List(Of String))

        While sr.Peek <> -1
            lstStrings.Add(sr.ReadLine.Split(",").ToList)
        End While

        'List will have every line as a sub-list.
        'Reverse list for new entry at top.
        lstStrings.Reverse()

        sr.Close()
        sr.Dispose()
        fp = Nothing

        Return lstStrings
    Catch ex As Exception
        MsgBox(ex.ToString)
        Return New List(Of List(Of String))
    End Try
End Function

You can use this function like so:

Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
    Try
        Dim lstHistory As List(Of List(Of String)) = ReadFromHistory()

        If lstHistory.Count > 0 Then
            For Each lst As List(Of String) In lstHistory
                Dim lvi As New ListViewItem

                lvi.Text = lst(0)
                lvi.SubItems.Add(lst(1))

                ListView1.Items.Add(lvi)
            Next
        End If
    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try
End Sub

But beware! A method like this will slow down over time! The larger the history file gets, the slower it will become.

commented: Very thorough +12
commented: good suggestion but can you still do something? +3

Begginnerdev
Dude, CSV??? - I watch your code, I think you are talking to save data in notepad?
If so then why i am not interested in it is because: I don't want the user to change the text unless he/she login the app and clean the History...

So Can you Help me with this:

More Info:
I am using just 4 columns:
1st col: Sr. No.
2nd col: Date (format: Jan date, year)
3rd col: Login Time (format: 00:00:00 PM/AM)
4th col: Logout Time (format: 00:00:00 PM/AM)

I don't matter, whether it saves to the application settings or elsewhere, But i don't want any free user to change the settings...

And also.
How can i list only top 10/20/50 history.?

i will be thanks to you if you can able to create the projectfor this so that I can get it soon/better... with comments (')

Then too thanks

reason for top 50 is only because my application does not slow down

As for CSV, it does not matter what application is writing - it is the format of the data that qualifies it for a CSV. See this. (Yes, I know it is Wikipedia.)

If you are needing the file to be unreadable to an unautherized user then you should look into file serialization.

This can be used to serialize the file and only access it from the application. This will not stop a user from deleting the file if they do happen to find it.

Just don't name it "YourApplicationName History".

If you are wanting to retreive the top 10/20/50 records - just modify the code to reflect this:

Dim iRecordCount As Integer = 1
While sr.Peek <> -1
    lstStrings.Add(sr.ReadLine.Split(",").ToList)
    iRecordCount += 1
    If iRecordCount = iNumberOfRecords Then Exit While
End While

This will get the oldest 10/20/50 (Entries are stored in a FIFO method.)

If you want the newest 10/20/50 you will have to change the code that populates the listview:

Dim iRecordCount As Integer = 1
For Each lst As List(Of String) In lstHistory

    Dim lvi As New ListViewItem
    lvi.Text = lst(0) 

    lvi.SubItems.Add(lst(1))
    ListView1.Items.Add(lvi)

    iRecordCount += 1
    IF iRecordCount = iNumberOfRecords Then Exit For
Next

Either way, it is a simple refactor/recode.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.