Results 1 to 16 of 16

Thread: [Resolved] Loading Listview Problems.........

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2001
    Location
    Maumelle, AR
    Posts
    624

    [Resolved] Loading Listview Problems.........

    I am trying to populate a a listview control with items and subitems that a user enters into a group of textboxes on a form. It looks as if some of the code to do this has changed since VB6 and I am confused again. Here is the current code I am using without success:

    lvSurgProcedure is the name of my listview control
    Code:
     Private Sub btnAddSurgeProcedure(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddSurgProcedure.Click
    
            Dim lvItem As New ListViewItem
    
            lvItem = txtSurgeProcedure.text
    
            lvSurgProcedure.Items.Add(lvItem)
    
            lvItem.SubItems(0).Add(dtpSurgPerformed.Text)
            lvItem.SubItems(1).Add(txtSurgName.Text)
            lvItem.SubItems(2).Add(txtSurgPhone.Text)
            lvItem.SubItems(3).Add(txtSurgFacility.Text)
            lvItem.SubItems(4).add(txtSurgPostOp.Text)
            
    
    
            
    
    
        End Sub
    Any help appreciated.
    Last edited by hipopony66; Feb 15th, 2009 at 11:32 AM. Reason: Resolved

  2. #2
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,950

    Re: Loading Listview Problems.........

    lvItem is a new ListViewItem for which you set the properties and then add that new item to the ListView.

    Code:
            'Define a new item
            Dim lvItem As New ListViewItem
    
            'Set its properties
            lvItem.Text = "The string value"
            lvItem.SubItems.Add("The second string value")
            lvItem.SubItems.Add("The third string value")
    
            'Add the new item to the ListView
            lvSurgProcedure.Items.Add(lvItem)
    Last edited by Bulldog; Feb 13th, 2009 at 04:50 PM.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2001
    Location
    Maumelle, AR
    Posts
    624

    Re: Loading Listview Problems.........

    Ok, here is my modified & updated code, but nothing happens when clicking the button. It looks like I've set the .text property of the Listitem, but haven't actually added the the item to the list.

    Code:
     Private Sub btnAddSurgeProcedure(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddSurgProcedure.Click
    
    
            Dim lvItem As New ListViewItem, strSurgProcedure As String, strSurgPerformed As String, strSurgName As String, strSurgPhone As String, strSurgFacility As String, strSurgPostOp As String
    
            strSurgProcedure = Trim(txtSurgProcedure.Text)
            strSurgPerformed = Trim(dtpSurgPerformed.Text)
            strSurgName = Trim(txtSurgName.Text)
            strSurgPhone = Trim(txtSurgPhone.Text)
            strSurgFacility = Trim(txtSurgFacility.Text)
            strSurgPostOp = Trim(txtSurgPostOp.Text)
    
    
            lvItem.Text = strSurgProcedure
            lvItem.SubItems.Add(strSurgPerformed)
            lvItem.SubItems.Add(strSurgName)
            lvItem.SubItems.Add(strSurgPhone)
            lvItem.SubItems.Add(strSurgFacility)
            lvItem.SubItems.Add(strSurgPostOp)
    
    
        End Sub
    Thanks.

  4. #4
    Fanatic Member vbasicgirl's Avatar
    Join Date
    Jan 2004
    Location
    Manchester, UK
    Posts
    1,016

    Re: Loading Listview Problems.........

    You are setting the listviewitem but you are not adding it to the listview. At the end of your code you need
    Code:
    Listview1.Items.Add(lvItem)
    Casey.

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2001
    Location
    Maumelle, AR
    Posts
    624

    Re: Loading Listview Problems.........

    Ok, I looked at some of my old VB6 code (took some serious digging!) and am using this:

    Code:
     Dim lvItem As New ListViewItem, strSurgProcedure As String, strSurgPerformed As String, strSurgName As String, strSurgPhone As String, strSurgFacility As String, strSurgPostOp As String
    
            strSurgProcedure = Trim(txtSurgProcedure.Text)
            strSurgPerformed = Trim(dtpSurgPerformed.Text)
            strSurgName = Trim(txtSurgName.Text)
            strSurgPhone = Trim(txtSurgPhone.Text)
            strSurgFacility = Trim(txtSurgFacility.Text)
            strSurgPostOp = Trim(txtSurgPostOp.Text)
    
            'add listitem to the listview
            lvItem = lvSurgProcedures.Items.Add(strSurgProcedure)
    
            'add subitems to the lvitem
            lvItem.SubItems(1) = (strSurgPerformed)
            lvItem.SubItems(2) = (strSurgName)
            lvItem.SubItems(3) = (strSurgPhone)
            lvItem.SubItems(4) = (strSurgFacility)
            lvItem.SubItems(5) = (strSurgPostOp)
    
        End Sub
    However, I get errors on the subitems complaining that string can't be converted to listviewitem. lvItem was declared as a ListViewItem, and I do not get error when I set the lvitem = to a string to add the item to the list, so I do not understand why I get this error on the subitems since they are subitems of the lvItem. Can anybody help clear up the confusion here? Thanks.

  6. #6
    Fanatic Member vbasicgirl's Avatar
    Join Date
    Jan 2004
    Location
    Manchester, UK
    Posts
    1,016

    Re: Loading Listview Problems.........

    The Listview is not like the vb6 listview in many ways. If you look at Bulldogs post you will see that you need to set the listviewitem before adding to the listview plus, take notice how he adds the subitems too.

    Saying all that, i usually use a different way of adding listviewitems to listviews. I create a new string array of the items to be added to the listviewitem, then add the listviewitem. In your case it would be.

    Code:
     Dim lvItem As ListViewItem
    
            lvItem = New ListViewItem(New String() {txtSurgProcedure.Text, dtpSurgPerformed.Text, _
                                                    txtSurgName.Text, txtSurgPhone.Text, _
                                                    txtSurgFacility.Text, txtSurgPostOp.Text})
                                                    
    
            lvSurgProcedures.Items.Add(lvItem)
    Casey.

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2001
    Location
    Maumelle, AR
    Posts
    624

    Re: Loading Listview Problems.........

    I have been able to add an item to the listview, but I no idea how to add the subitems in .DOT. Does anybody know of a good sample project out there?

    strSurgPerformed is my listitem, and all the other strings should be subitems of that listitem. I miss VB6!

  8. #8
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,950

    Re: Loading Listview Problems.........

    See my previous post above for adding SubItems.

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2001
    Location
    Maumelle, AR
    Posts
    624

    Re: Loading Listview Problems.........

    Yes, I have tried using a variation of that Bulldog. I do not understand why the code below does not work. No build errors, and it adds the item from txtSurgProcedure that is stored as strSurgProcedure to the listview, but does not add any of the subitems. If I add a second item, it adds it to the row.

    Here is my code:

    Code:
    Private Sub btnAddSurgeProcedure(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddSurgProcedure.Click
    
    
            Dim lvItem As New ListViewItem, strSurgProcedure As String, strSurgPerformed As String, strSurgName As String, strSurgPhone As String, strSurgFacility As String, strSurgPostOp As String
    
            strSurgProcedure = Trim(txtSurgProcedure.Text)
            strSurgPerformed = Trim(dtpSurgPerformed.Text)
            strSurgName = Trim(txtSurgName.Text)
            strSurgPhone = Trim(txtSurgPhone.Text)
            strSurgFacility = Trim(txtSurgFacility.Text)
            strSurgPostOp = Trim(txtSurgPostOp.Text)
    
    
            lvSurgProcedures.Items.Add(lvItem)
            lvItem.Text = (strSurgProcedure)
    
            With lvItem
                .SubItems.Add(strSurgPerformed)
                .SubItems.Add(strSurgName)
                .SubItems.Add(strSurgPhone)
                .SubItems.Add(strSurgFacility)
                .SubItems.Add(strSurgPostOp)
            End With

  10. #10
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,950

    Re: Loading Listview Problems.........

    Ah, I think I see where we have led you astray.

    Have you added columns to the ListView to hold the SubItems?

    So for example, when your program start, you need to setup the listview to have the right number of columns, like this;

    Code:
            ListView1.View = View.Details
            ListView1.Columns.Add("Col0")
            ListView1.Columns.Add("Col1")
            ListView1.Columns.Add("Col2")
            ListView1.Columns.Add("Col3")
            ListView1.Columns.Add("Col4")
            ListView1.Columns.Add("Col5")
    then in your button event add the items like this;

    Code:
            Dim lvitem As New ListViewItem
            lvitem.Text = "Hello"
            With lvitem
                .SubItems.Add("A")
                .SubItems.Add("B")
                .SubItems.Add("C")
                .SubItems.Add("D")
                .SubItems.Add("E")
            End With
            ListView1.Items.Add(lvitem)

  11. #11

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2001
    Location
    Maumelle, AR
    Posts
    624

    Re: Loading Listview Problems.........

    Ahhhh.............that is exactly what I needed! Got it working now. Thanks for everybody's help and patience. Thanks again.

  12. #12
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,950

    Re: Loading Listview Problems.........

    Actually my apologies on this one. It didnt occur to me you had not set up the columns earlier (it seems obvious now).

  13. #13

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2001
    Location
    Maumelle, AR
    Posts
    624

    Re: Loading Listview Problems.........

    NP, I have added a check to verify that the listitem the user is trying to add to the list is not already in the listview, but now the item does not add at all. Maybe this is another VB6 vs. .NET thing?

    Code:
      Private Sub btnAddSurgeProcedure(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddSurgProcedure.Click
    
    
            Dim lvItem As New ListViewItem, strSurgProcedure As String, strSurgPerformed As String, strSurgName As String, strSurgPhone As String, strSurgFacility As String, strSurgPostOp As String, i As Integer
    
            'remove leading and trailing spaces from the entries and store as a variable
            strSurgProcedure = Trim(txtSurgProcedure.Text)
            strSurgPerformed = Trim(dtpSurgPerformed.Text)
            strSurgName = Trim(txtSurgName.Text)
            strSurgPhone = Trim(txtSurgPhone.Text)
            strSurgFacility = Trim(txtSurgFacility.Text)
            strSurgPostOp = Trim(txtSurgPostOp.Text)
    
            'set lvitem to the value of the SurgProcedure string
            lvItem.Text = (strSurgProcedure)
    
            'Set the value of the lvitem subitems
            With lvItem
                .SubItems.Add(strSurgPerformed)
                .SubItems.Add(strSurgName)
                .SubItems.Add(strSurgPhone)
                .SubItems.Add(strSurgFacility)
                .SubItems.Add(strSurgPostOp)
            End With
    
            'check to see if the procedure already exists in the listview
            For i = 1 To lvSurgProcedures.Items.Count
                If strSurgProcedure = lvSurgProcedures.Items(i).Text Then
                    '
                    MessageBox.Show("The Surgical Prodcedure you are trying to add is already in the list")
                Else
                    'add the lvitem to the listview
                    lvSurgProcedures.Items.Add(lvItem)
                End If
            Next

  14. #14
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,950

    Re: Loading Listview Problems.........

    The For loop has to start at zero since the list is zero based.

    Code:
            For i = 0 To lvSurgProcedures.Items.Count - 1

  15. #15

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2001
    Location
    Maumelle, AR
    Posts
    624

    Re: Loading Listview Problems.........

    Ok, I've got a validation check in place now and everything is working. Here is my updated code
    Code:
    Private Sub btnAddSurgeProcedure(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddSurgProcedure.Click
    
    
            Dim lvItem As New ListViewItem, strSurgProcedure As String, strSurgPerformed As String, strSurgName As String, strSurgPhone As String, strSurgFacility As String, strSurgPostOp As String, i As Integer
    
            'remove leading and trailing spaces from the entries and store as a variable
            strSurgProcedure = Trim(txtSurgProcedure.Text)
            strSurgPerformed = Trim(dtpSurgPerformed.Text)
            strSurgName = Trim(txtSurgName.Text)
            strSurgPhone = Trim(mtxtSurgPhone.Text)
            strSurgFacility = Trim(txtSurgFacility.Text)
            strSurgPostOp = Trim(txtSurgPostOp.Text)
    
            'set lvitem to the value of the SurgProcedure string
            lvItem.Text = (strSurgProcedure)
    
            'Set the value of the lvitem subitems
            With lvItem
                .SubItems.Add(strSurgPerformed)
                .SubItems.Add(strSurgName)
                .SubItems.Add(strSurgPhone)
                .SubItems.Add(strSurgFacility)
                .SubItems.Add(strSurgPostOp)
            End With
    
            'if there are no items in the listview, don't check for duplicates
            If lvSurgProcedures.Items.Count = 0 Then
                'add the lvitem to the listview
                lvSurgProcedures.Items.Add(lvItem)
                Exit Sub
    
            Else
    
                'check to see if the procedure already exists in the listview
                For i = 0 To lvSurgProcedures.Items.Count - 1
                    If strSurgProcedure = lvSurgProcedures.Items(i).Text Then
                        '
                        MessageBox.Show("The Surgical Prodcedure you are trying to add is already in the list")
                    Else
                        'add the lvitem to the listview
                        lvSurgProcedures.Items.Add(lvItem)
                    End If
                Next
            End If
    
    
    
    
        End Sub
    I can add 2 items to the listview and if I enter a duplicate, it throws up the message box. However, when I attempt to add the 3rd item to the listview I get an error on the line in red in the above code. The error is
    Code:
    'System.ArgumentException' occurred in System.Windows.Forms.dll

  16. #16
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,950

    Re: Loading Listview Problems.........

    That's because adding a new item changes the Items.Count. Change you code as below.

    Code:
                Dim AlreadyPresent As Boolean = False
                For i = 0 To lvSurgProcedures.Items.Count - 1
                    If strSurgProcedure = lvSurgProcedures.Items(i).Text Then
                        MessageBox.Show("The Surgical Prodcedure you are trying to add is already in the list")
                        AlreadyPresent = True
                        Exit For
                    End If
                Next
                If Not AlreadyPresent Then lvSurgProcedures.Items.Add(lvItem)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width