6 Replies - 22160 Views - Last Post: 16 April 2008 - 03:44 PM Rate Topic: -----

#1 keithmoloney  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 41
  • Joined: 04-April 08

listbox duplicates

Posted 09 April 2008 - 09:31 AM

im parsing a text file and reading certain strings into a list box.
there are lots of duplicates.
i want to prevent dublicates from going into my listbox. i dont want the user to have to remove them. can this be done?

Private Sub btnLoadDir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoadDir.Click



		'declare directory to loop through
		Dim di As New DirectoryInfo("C:\Documents and Settings\kmoloney\Desktop\kmoloney\ALTIUMtest")
		'for every subdirectory, add its name to the listbox
		For Each subdi As DirectoryInfo In di.GetDirectories
			lstDirectories.Items.Add(subdi.Name)
			Next

		Dim DirList As New ArrayList
		GetDirectories("C:\Documents and Settings\kmoloney\Desktop\kmoloney\ALTIUMtest", DirList)

		' DirList now contains a list of 
		' directories under C:\Documents and Settings\kmoloney\Desktop\kmoloney\ALTIUMtest
		Dim libpkgpath As String

		For Each fullpath As String In lstDirectories1.Items
			libpkgpath = fullpath


			For Each file_name As String In Directory.GetFiles(libpkgpath.ToString)
				Using MyReader As New Microsoft.VisualBasic.FileIO.TextFieldParser(file_name)
					MyReader.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited
					MyReader.Delimiters = New String() {"\"} 'Delimiter

					Dim currentRow As String()
					Dim docpath As String = "DocumentPath=.." 'Search for all instances of this in the text file


					While Not MyReader.EndOfData
						'read until the end of the text file
						Try
							currentRow = MyReader.ReadFields()

							For Each currentField As String In currentRow
								'for every field that contains the text being searched for:
								If currentField.Contains(docpath) = True Then

									'add the row to the listbox
									lstLibraries.Items.Add(currentRow(2))

								End If
							Next



						Catch 'ex As Microsoft.VisualBasic.FileIO.MalformedLineException
							'MsgBox("Line " & ex.Message & " is invalid.  Skipping")
						End Try
					End While
				End Using
			Next

		Next

		

	End Sub

	Sub GetDirectories(ByVal StartPath As String, ByRef DirectoryList As ArrayList)
		Dim Dirs() As String = Directory.GetDirectories(StartPath)
		DirectoryList.AddRange(Dirs)
		For Each Dir As String In Dirs
			GetDirectories(Dir, DirectoryList)

			lstDirectories1.Items.Add(Path.GetDirectoryName(Dir))
		Next

	End Sub



Is This A Good Question/Topic? 0
  • +

Replies To: listbox duplicates

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4319
  • View blog
  • Posts: 12,101
  • Joined: 18-April 07

Re: listbox duplicates

Posted 09 April 2008 - 10:11 AM

Well one way you can do this is using the Contains() method of the listbox.items collection. Passing it the value you want to add to the listbox will return true or false if the value is already in the list.

' Returns true if the listbox contains the value "myvalue" and shows a messagebox notifying the user it is there.
If Listbox1.Items.Contains("myvalue") Then
     MessageBox.Show("Yes the value 'myvalue' is in the list")
End If



Another way you could do this is sorting the listbox and employ a search algorithm like a binary search to quickly check the items collection for the value. This might be an option if you have a HUGE number of listbox items and you see a problem with the contains() method.

Once you have determined if it is in the list, you can use the IndexOf method of the collection to pinpoint its exact location if you want to.

Hope this helps!

"At DIC we be item searching and returning code ninjas... now only if I could find my keys." :snap:
Was This Post Helpful? 0
  • +
  • -

#3 keithmoloney  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 41
  • Joined: 04-April 08

Re: listbox duplicates

Posted 10 April 2008 - 06:44 AM

Dim di As New DirectoryInfo("C:\Documents and Settings\kmoloney\Desktop\kmoloney\ALTIUMtest" + "\" + SelectedDir)
				Dim diar1 As IO.FileInfo() = di.GetFiles()
				Dim dra As IO.FileInfo
				'for every file in the selected folder, add files with these extensions to the list
				'if it is a .libpkg then parse it to find dependant files.

For Each dra In diar1
					If dra.Extension = ".PcbLib" Then
						lstLibraries.Items.Add(dra)
					ElseIf dra.Extension = ".SchLib" Then
						lstLibraries.Items.Add(dra)
					ElseIf dra.Extension = ".Lb" Then
						lstLibraries.Items.Add(dra)
					ElseIf dra.Extension = ".PCB3DLib" Then
						lstLibraries.Items.Add(dra)



it wont let me check for dra using
if lstLibraries.contains(dra) then



any other ideas?
Was This Post Helpful? 0
  • +
  • -

#4 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1639
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: listbox duplicates

Posted 10 April 2008 - 07:05 AM

You forgot the Items property


if lstLibraries.Items.Contains(dra) then




Now that that is out of the way, you need to use one of the Members of the DirectoryInfo Class. In this case we would check for the FullName Property to see if that directory already exists in your ListBox


If Not lstLibraries.Items.Contains(dra.FullName) Then
      'Do your work here
End If


Was This Post Helpful? 0
  • +
  • -

#5 keithmoloney  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 41
  • Joined: 04-April 08

Re: listbox duplicates

Posted 10 April 2008 - 07:29 AM

that isnt working coz im using a parser and all other kinds of things aswel and for some reason there is still duplicates. i think an easier solution would be do delete all duplicates in the listbox afterwards. can u tell me how to do this?
Was This Post Helpful? 0
  • +
  • -

#14 kaymaf  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 24
  • Joined: 03-October 07

Re: listbox duplicates

Posted 16 April 2008 - 03:04 PM

you can use this function to remove duplicate from listbox. just put the name of the listbox in the parameter where you call the function.
 
 Function RemoveDuplicateItem(ByVal listboxName As ListBox)
		listboxName.Sorted = True
		listboxName.Refresh()
		Dim index As Integer
		Dim itemcount As Integer = listboxName.Items.Count

		If itemcount > 1 Then
			Dim lastitem As String = listboxName.Items(itemcount - 1)

			For index = itemcount - 2 To 0 Step -1
				If listboxName.Items(index) = lastitem Then
					listboxName.Items.RemoveAt(index)
				Else
					lastitem = listboxName.Items(index)
				End If
			Next
		End If
	End Function



Usege: RemoveDuplicateItem(Listbox1)
Was This Post Helpful? 1

#15 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: listbox duplicates

Posted 16 April 2008 - 03:44 PM

The easy way to handle this situation is to loop through the results already stored in the ListBox as you are adding new ones from the text file you are reading. If you find one that is already in the ListBox, then don't add it. You can do this with a For Each loop and one IF statement.

Here is an example of how you might go about it.
		For Each item As Object In Me.ListBox1.Items
			If item.Equals(someValue) Then
				'ListBox already contains this value
			End If
		Next

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1