Jump to content
Rickets

[VB] Cum sterg anumiti itemi dintr-un listbox

Recommended Posts

For i As Integer = ListBox1.Items.Count - 1 To 0 Step -1

If ListBox1.Items(i).ToString = "portocale" Then

ListBox1.Items.RemoveAt(i)

End If

Next

sau

For i As Integer = ListBox1.Items.Count - 1 To 0 Step -1

If ListBox1.Items(i).ToString.Contains("portocale") Then

ListBox1.Items.RemoveAt(i)

End If

Next

Link to comment
Share on other sites

For i As Integer = 0 To 4

Dim lv As New ListViewItem

lv.Text = i 'Column 0

lv.SubItems.Add("portocale" & i) 'Column 1

ListView1.Items.Add(lv)

Next

For Each item As ListViewItem In ListView1.Items

If item.SubItems(1).Text = "portocale2" Then

ListView1.Items.Remove(item)

End If

Next

3fgHKCe.png

Link to comment
Share on other sites

For i As Integer = 0 To ListView2.Items.Count

For Each item As ListViewItem In ListView2.Items

If item.SubItems(i).Text = "OFFLINE" Then

ListView2.Items.Remove(item)

End If

Next

Next

Primesc eroarea asta :

InvalidArgument=Value of '2' is not valid for 'index'.

Parameter name: index

Sterge vreo doi itemi si dupa da eroarea asta.

Link to comment
Share on other sites

@Rickets

For Each item As ListViewItem In ListView1.Items

If item.SubItems(1).Text = "portocale2" Then

ListView1.Items.Remove(item)

End If

Next

Codul de mai sus zice :

Pentru fiecare item ca ListViewItem in toate itemele din ListView1

ce nu merge?

Acel

For i As Integer = 0 To 4

este pentru a adauga iteme, sa am ce gasi ... DEMO

Tu trebuie sa adaptezi codul dupa nevoia ta...

Link to comment
Share on other sites

Pot sa stiu de ce folosesti for si foreach in acelasi context?


For Each item As ListViewItem In ListView2.Items
If item.SubItems(1).Text = "OFFLINE" Then
ListView2.Items.Remove(item)


For i As Integer = 0 to ListView2.Items.Count - 1 //(index 0 based)
If item.SubItems(1).Text = "OFFLINE" Then
ListView2.Items.RemoveAt(i)

Cred ca scriem amandoi degeaba aici ca tu nu pricepi nimic.

Edited by Erase
Link to comment
Share on other sites

For i As Integer = 0 To ListView2.Items.Count

For Each item As ListViewItem In ListView2.Items

If item.SubItems(i).Text = "OFFLINE" Then

ListView2.Items.Remove(item)

End If

Next

Next

E o prostie, cum spune si @Erase

1

For i As Integer = 0 To ListView2.Items.Count

Pentru i egal cu 0 pana la lungimea listei

2

 If item.SubItems(i).Text = "OFFLINE" Then

daca item.SubItems(i).Text = "OFFLINE"

Pai daca ai 300 de iteme nu inseamna ca ai si 300 de coloane, tu asta faci aici

SubItems(i) = subitemul itemului

poate

 If item.Text = "OFFLINE" Then
Link to comment
Share on other sites

Incearca sa intelegi ce se intampla in codurile de mai sus.

Ai intrebat la un moment dat cum faci daca nu sti cate items ai. Cu un search pe google aflai imediat raspunsul; sa nu mai zic ca puteai sa scrii ListView1.ListItems. si iti aparea o lista cu toate functiile ce le poti apela.

Edited by Wav3
Link to comment
Share on other sites

Metoda e aceasi.

Parcurgi toate itemurile si verifici daca sunt egale cu ceea ce vrei tu (sau verifici daca contin stringul tau).

Pentru parcurgere ai for-ul, pentru verificare ai if-ul iar pentru stergere ai removeItem-ul (care il gaseai daca scriai ListView1.ListItems.)

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...