Rickets Posted March 2, 2015 Report Posted March 2, 2015 Sa zicem ca am un listbox ce contine urmatoarele :bananeportocalebananebanane bananeportocaleCum fac sa sterg toti itemii care contin "portocale", sau toti itemii care contin "banane"? Quote
Maximus Posted March 2, 2015 Report Posted March 2, 2015 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 Nextsau 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 Quote
Maximus Posted March 2, 2015 Report Posted March 2, 2015 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 Quote
Rickets Posted March 3, 2015 Author Report Posted March 3, 2015 Dar la ListView, daca nu stiu cati itemi o sa am, cum fac sa sterg anumiti itemi, daca contin un anumit text...? Quote
Active Members MrGrj Posted March 3, 2015 Active Members Report Posted March 3, 2015 In loc sa iei for-ul ala pana la 4 banuiesc ca iei un for de la 0 la cati itemi ai si aplici acelasi algoritm. Parca se folosea:lvSearch.Items.Count Quote
Rickets Posted March 4, 2015 Author Report Posted March 4, 2015 In loc sa iei for-ul ala pana la 4 banuiesc ca iei un for de la 0 la cati itemi ai si aplici acelasi algoritm. Parca se folosea:lvSearch.Items.CountNu are cum sa mearga asa pentru ca declari lv abia dupa ce pui for-ul. Quote
Erase Posted March 4, 2015 Report Posted March 4, 2015 Rickets, cu toate ca trebuia sa intelegi din postul de mai sus, arunca un ochi aici.Daca nu intelegi incearca ceva mai usor. Quote
Rickets Posted March 4, 2015 Author Report Posted March 4, 2015 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 NextPrimesc eroarea asta : InvalidArgument=Value of '2' is not valid for 'index'.Parameter name: indexSterge vreo doi itemi si dupa da eroarea asta. Quote
Maximus Posted March 4, 2015 Report Posted March 4, 2015 @RicketsFor Each item As ListViewItem In ListView1.Items If item.SubItems(1).Text = "portocale2" Then ListView1.Items.Remove(item) End IfNextCodul de mai sus zice :Pentru fiecare item ca ListViewItem in toate itemele din ListView1ce nu merge?Acel For i As Integer = 0 To 4 este pentru a adauga iteme, sa am ce gasi ... DEMOTu trebuie sa adaptezi codul dupa nevoia ta... Quote
Erase Posted March 4, 2015 Report Posted March 4, 2015 (edited) 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 March 4, 2015 by Erase Quote
Maximus Posted March 4, 2015 Report Posted March 4, 2015 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 NextE o prostie, cum spune si @Erase1For i As Integer = 0 To ListView2.Items.CountPentru i egal cu 0 pana la lungimea listei2 If item.SubItems(i).Text = "OFFLINE" Thendaca item.SubItems(i).Text = "OFFLINE"Pai daca ai 300 de iteme nu inseamna ca ai si 300 de coloane, tu asta faci aiciSubItems(i) = subitemul itemuluipoate If item.Text = "OFFLINE" Then Quote
Rickets Posted March 4, 2015 Author Report Posted March 4, 2015 Am inteles si rezolvat, multumesc. Quote
Wav3 Posted March 4, 2015 Report Posted March 4, 2015 (edited) 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 March 4, 2015 by Wav3 Quote
Rickets Posted March 4, 2015 Author Report Posted March 4, 2015 Nu mi-am dat seama cum functioneaza, la ListBox era simplu, dar la asta nu m-am prins... Quote
Wav3 Posted March 4, 2015 Report Posted March 4, 2015 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.) Quote