Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

(2008) Clear subitems ListView

Status
Not open for further replies.

RadjeshNL

Technical User
Sep 1, 2008
32
NL
Ola,

Trying to remove the subitems from a listview, but I can't get it to work.

One of the codes I tried:

Code:
For i As Integer = 0 To 14
    .Items(i).SubItems.Clear()
Next i

Tried "text" and everything, but nothing seems to work for me.
Someone? Thanks in advance. ;)
 
Are you getting an exception (error)? I tried the same code you posted and it worked fine. I'm assuming that you have a With statement somewhere before your For loop, and that With statement includes the name of your listview. Could you post more of your code? Maybe something is erroring out prior to the code you posted.
 
It doesn't give any error, but the code clears also the first "column items".

Code:
With lstvItem
    For i As Integer = 0 To 14
        .Items(i).SubItems.Clear()
    Next i

'   Set the name, surename and date added to the columnheader(s)
            .Columns(1).Text = lstvLijst.SelectedItems(0).Text & " " & lstvLijst.SelectedItems(0).SubItems(1).Text & "  / Toegevoegd: " & lstvLijst.SelectedItems(0).SubItems(15).Text
            '   add values from the listitem to the subitems of the other listview
            .Items(0).SubItems.Add(lstvLijst.SelectedItems(0).SubItems(0).Text)
            .Items(1).SubItems.Add(lstvLijst.SelectedItems(0).SubItems(1).Text)
            .Items(2).SubItems.Add(lstvLijst.SelectedItems(0).SubItems(2).Text)
            .Items(3).SubItems.Add(lstvLijst.SelectedItems(0).SubItems(3).Text)
            .Items(4).SubItems.Add(lstvLijst.SelectedItems(0).SubItems(4).Text)
            .Items(5).SubItems.Add(lstvLijst.SelectedItems(0).SubItems(5).Text)
            .Items(6).SubItems.Add(lstvLijst.SelectedItems(0).SubItems(6).Text)
            .Items(7).SubItems.Add(lstvLijst.SelectedItems(0).SubItems(7).Text)
            .Items(8).SubItems.Add(lstvLijst.SelectedItems(0).SubItems(8).Text)
            .Items(9).SubItems.Add(lstvLijst.SelectedItems(0).SubItems(9).Text)
            .Items(10).SubItems.Add(lstvLijst.SelectedItems(0).SubItems(10).Text)
            .Items(11).SubItems.Add(lstvLijst.SelectedItems(0).SubItems(11).Text)
            .Items(12).SubItems.Add(lstvLijst.SelectedItems(0).SubItems(12).Text)
            .Items(13).SubItems.Add(lstvLijst.SelectedItems(0).SubItems(13).Text)
            .Items(14).SubItems.Add(lstvLijst.SelectedItems(0).SubItems(14).Text)
End With
 
Your code is clearing the subitems, then readding the subitems. So it will look like it's not clearing them, but it probably is. Try to comment out all of the code that is readding the subitems and run your app.
 
Many thanks for the help m8. Mucho appreciation. :thumbs:

Indeed it clears the subitems, which it should.
But it also clears the first items from the first column. And that is not the thing that it should do. Or perhaps I don't understand what you are trying to tell me.
 
If you don't want the first column cleared, start your loop at 1 instead of 0.

Code:
For i As Integer = 1 To 14
    .Items(i).SubItems.Clear()
Next i
 
Thanks man, but nope. Now it only shows the first item and the rest is blanc. The subitems (the items in Column 2) are visible.
 
OOPS...sorry about that. Unfortunately you cannot use the Clear method on a single subitem. You can only Clear all subitems. So what you'll probably need to do is loop through and "blank out" all of the subitems, except for the 1st subitem. (Note: Replace the XX with the number of subitems you have)

Code:
For i As Integer = 0 to 14
   For j As Integer = 1 to [b]XX[/b]
       .Items(i).SubItems(j).Text = ""
   Next
Next
 
Ah like that. Let me have a try. Thanks in advance.
 
Sroory for my very late reply, but I don;t have internet for a few weeks now. I'm at a friends home atm.

Your solution doesn't work m8. To bad. I'll see what I can do.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top