can anyone help me in adding items to a listview?
here is what i want to do: when i click the login button the date and time will be added in the column date and time in, here is my code and it worksClick Here:

Private Sub LoginButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LoginButton.Click
    For Each item As ListViewItem In TimeSheetListView.Items
        Select Case True
            Case item.SubItems(0).Text = dates.Text
                Return
            Case item.SubItems(1).Text = time.Text
                Return

        End Select
    Next
    Dim lv As ListViewItem = TimeSheetListView.Items.Add(dates.Text)
    lv.SubItems.Add(time.Text)
    lv.SubItems.Add(time.Text)
    If TimeSheetListView.Items.Count >= 1 Then
        MsgBox("You are logged in.", MsgBoxStyle.Information, "Message")
        LoginButton.Enabled = False
        LogoutButton.Enabled = True
    End If
End Sub

my problem is the log out button, i want to add the time, in the time out column but i dont know how
i had attach a printscreen of my work..hope anyone can help

Attachments Untitled.png 457.5 KB

Try something like this in your login button's click event:

With lviNew
    .Text = Now.ToShortDateString
    .SubItems.Add(Now.ToShortTimeString)
End With

ListView1.Items.Add(lviNew)

is it possible to add the date in subitem(0) and clockin in subitem(1) using the login button and add the clockout in subitem(2) using the logout button?
can anyone help me in doing this?

When creating listview items, the format will look like this:

|FirstColumn|SecondColumn|ThirdColumn|ForthColumn|ect..

.Text|SubItem(0)|SubItem(1)|SubItem(2)|ect...

So when loading data into a new listview item:

'This data will look like:
' |Hello|World|In|VB| in the listview's columns
Dim lviNew As New ListViewItem

With lviNew
   .Text = "Hello"
   .SubItems.Add("World")
   .SubItems.Add("In")
   .SubItems.Add("VB")
End With

ListView1.Items.Add(lviNew)

So with this being said, you need to format your listview with a unique value in the columns. Something you can reference. Should look something like:

'Place this code in the login button.

With lviNew
    .Text = EmployeeNumber.Text
    .SubItems.Add(Now.Date)
    .SubItems.Add(InTextBox.Text)
End With

'This will look something like this:
 |0|10/11/2012|9:00AM|

 'Now, to reference it to add the logout on the logout button's click do this:

 For Each lviItem as ListViewItem in ListView1.Items
     If lviItem.Text = EmployeeNumber.Text Then 'Your unique value here
         lviItem.SubItems.Add(OutTime.Text)
     End If
 Next

Hope this helps.

Edited 4 Years Ago by Begginnerdev

thank you very much for the code but when i click login button it doesn't add items to listview as well as in logout button

Can you post the section of code you are using to add it to the list view?

So far, I can't see where you add it to your listview.

I see where you create the item, but you need to add it.

ListView1.Items.Add(lv)

its working now but my problem is that when i click the log out button the entries are place in the next row

Private Sub LoginButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LoginButton.Click
    Dim lviNew As New ListViewItem
    With lviNew
        .Text = EmpNumTextBox.Text
        .SubItems.Add(dates.Text)
        .SubItems.Add(timein.Text)
    End With
    TimeSheetListView.Items.Add(lviNew)
End Sub

Private Sub LogoutButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LogoutButton.Click
    Dim lviNew As New ListViewItem
    For Each lviItem As ListViewItem In TimeSheetListView.Items
        If lviItem.Text = EmpNumTextBox.Text Then 'Your unique value here
            lviItem.SubItems.Add(timeout.Text)
        End If
    Next
    TimeSheetListView.Items.Add(lviNew)
        End Sub

Remove the line:

TimeSheetListView.Items.Add(lviNew)

You are adding the item to the listview item that exists, then adding another.

thanks a lot it works..but can i ask if you know how do i add the time in and time out to get the total hours work?

By using a time span.

Something like this:

    Dim dat1 As DateTime = #10/11/2012 9:00:00 AM#
    Dim dat2 As DateTime = #10/11/2012 5:00:00 PM#

    Dim datDif As TimeSpan = dat2 - dat1

datDif will return a value of 08:00:00 or 8 hours.

By the way

Dim lviNew As New ListViewItem
With lviNew
    .Text = EmpNumTextBox.Text
    .SubItems.Add(dates.Text)
    .SubItems.Add(timein.Text)
End With
TimeSheetListView.Items.Add(lviNew)

can be written as

TimeSheetListView.Items.Add(New ListViewItem({EmpNumTextBox.Text,dates.Text,timein.Text}))

but how about if there is no fixed value? because i plan to add the time in and time out that is added in the listview or from two textboxes?

This article has been dead for over six months. Start a new discussion instead.