in vb.net?
Printable View
in vb.net?
if you are getting the values from a database using a SQL statement you could do select distinct row and i think that will only bring out individual values.
its not like that...
Add a Key Value by inherting the ListViewItem in your own class like below. Then iterate through the keys every time you add a listitem to see if it exsists. Don't worry This is very fast due to the IDictionaryEnumerator Interface.
Public Class MyListViewItem
Inherits System.Windows.Forms.ListViewItem
Implements IDictionaryEnumerator
Private ListItemEntry As DictionaryEntry
Private enumerator As IEnumerator
Public Sub New()
enumerator = MyBase.SubItems.GetEnumerator
End Sub
Public Property NodeKey() As String
Get
Return ListItemEntry.Key.ToString()
End Get
Set(ByVal Value As String)
ListItemEntry.Key = Value
End Set
End Property
Public Property NodeValue() As Object
Get
Return ListItemEntry.Value
End Get
Set(ByVal Value As Object)
ListItemEntry.Value = Value
End Set
End Property
Public Overridable Overloads ReadOnly Property Entry() As DictionaryEntry _
Implements IDictionaryEnumerator.Entry
Get
Return ListItemEntry
End Get
End Property
Public Overridable Overloads Function MoveNext() As Boolean _
Implements IDictionaryEnumerator.MoveNext
Dim Success As Boolean
Success = enumerator.MoveNext()
Return Success
End Function
Public Overridable Overloads ReadOnly Property Current() As Object _
Implements IEnumerator.Current
Get
Return enumerator.Current
End Get
End Property
Public Overridable Overloads ReadOnly Property Key() As Object _
Implements IDictionaryEnumerator.Key
Get
Return ListItemEntry.Key
End Get
End Property
Public Overridable Overloads ReadOnly Property Value() As Object _
Implements IDictionaryEnumerator.Value
Get
Return ListItemEntry.Value
End Get
End Property
Public Overridable Overloads Sub Reset() _
Implements IEnumerator.Reset
enumerator.Reset()
End Sub
End Class
Very easy way ...
VB Code:
If listbox.Items.Count = 0 Then listbox.Items.AddRange(fields) Else Exit Sub End If
is there another way? is it possible to add all the items first, and then check for duplicates and remove them after?
Hmmm , have you tried that code I posted , I did the trick for me ?Quote:
Originally posted by qpabani
is there another way? is it possible to add all the items first, and then check for duplicates and remove them after?
what is fields?
Uh..that's array of returned rows . How are you populating the listbox ?:rolleyes:Quote:
Originally posted by qpabani
what is fields?
VB Code:
List1.Items.Add("Hello") List1.Items.Add("Hello") List1.Items.Add("Hello") If ListBox1.Items.Count = 0 Then ListBox1.Items.AddRange(List1.Items) Else Exit Sub End If
doesnt work, it adds hello 3 times tothe second box..
in my program, im just adding items from data retrieved on a webpage, no arrays invovled..
I know it won't work the way you have done it , rather you can do it this way :
VB Code:
Dim Arry() as String ={"item1","item2","item"} If ListBox1.Items.Count = 0 Then ListBox1.Items.AddRange(Arry) Else Exit Sub End If
Try explaining more about where the data is coming from or how it is collected. The best way is not to put duplicate info into the listbox, but if its already there then you have to write a routine to find and delete dupes. I'd make use of the Contains method of the items collection before putting an item in.
well.. the app retrieves data from numerous pages, parses the html and adds items to the listbox.. after its done this routine, i need to be able to remove the duplicates...
anyone know how?
Instead of just adding them all in, before one is added check if it is already there, this is by far the easiest most efficent way.
VB Code:
'assuming these are strings and one is assigned to the item variable If Not Listbox1.items.Contains(item) then Listbox1.items.Add(item) End If
edit....nm im just gonna setup a loop threw to look for dups
AcE
I agree with Edneeis, check the list before adding items rather than add them then delete duplicates.
well heres a lil loop code i cam up with.....its very lame and there is something wrong...i cant put my fingure on it...tell me wut u think....this is ofcourse ran after the id is added....
AcECode:Private Sub RunDupCheck()
If LV_IDList.Items.Count <> 0 Then
Dim i As Integer = LV_IDList.Items.Count - 1
Dim ii As Integer = LV_IDList.Items.Count - 1
Do Until i = -1
Do Until ii = -1
If i <> ii Then
If LV_IDList.Items(ii).Text = LV_IDList.Items(i).Text Then
LV_IDList.Items(ii).Remove()
End If
ii = ii - 1
Else
ii = ii - 1
End If
Loop
i = i - 1
Loop
End If
End Sub
Just like Edneeis stated, but this checks the text value.
Code:Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If ListBox.Items.IndexOf("Hello World") = -1 Then
ListBox.Items.Add("Hello World")
Else
MsgBox("Item - Hello World already exists")
End If
End Sub
There are a couple of obvious problems, firstly when you would need to reset the 'ii' count before attempting to do the second loop again and when you remove an item from the list all the indexes are shifted by one, so 'i' can point to an item that no longer exists.
This seems to work, tho I've not compiled and run it!
Code:Dim i as integer
Dim ii as integer
i = 0
Do While i < LV_IDList.Items.Count
ii = i + 1
Do While ii < LV_IDList.Items.Count
If LV_IDList.Items(i).Text = LV_IDList.Items(ii).Text Then
LV_IDList.Items(ii).Remove()
Else
ii += 1
End If
Loop
i += 1
Loop