|
-
Apr 11th, 2004, 08:54 AM
#1
Thread Starter
New Member
Remove duplicates from a combo box
this is the code for the listbox
---------------------------------------------------------------------
Dim i As Long
Dim j As Long
With List1
For i = 0 To .ListCount - 1
For j = .ListCount To (i + 1) Step -1
If .List(j) = .List(i) Then
.RemoveItem j
End If
Next
Next
End With
End Sub
---------------------------------------------------------------------
this is the best i can alter the code
---------------------------------------------------------------------
Dim i As Long
Dim j As Long
With cboIUpREF
For i = 0 To .Items.Count - 1
For j = .Items.Count To (i + 1) Step -1
If .Items.Item(j) = .Items.Item(i) Then
.Items.Remove(j)
End If
Next
Next
End With
the line "If .Items.Item(j) = .Items.Item(i) Then" causes an error, what could i change it to, or is there another way other removing dups from a combo box, im using vb.net.
-
Apr 11th, 2004, 09:00 AM
#2
The count property is not updated when you do a deletion while looping. So the outer loop will error out... make both loops a step down loop, start at list count end at 1.
-
Apr 11th, 2004, 09:03 AM
#3
Since removing items changes ListCount, the only way to do what you want is to do it backward. In other words
For i = .ListCount - 1 To 0 Step -1
Would you be interested in simple code to prevent duplicates to start with?
-
Apr 11th, 2004, 09:10 AM
#4
Thread Starter
New Member
thanks for the reply, can you change the code with the new loop, im just starting out and im not sure what you mean
-
Apr 11th, 2004, 09:11 AM
#5
Thread Starter
New Member
iv just read your reply MartinLiss, to stop the duplicates from entering the file isnt possible becauseof the type of program
-
Apr 11th, 2004, 09:17 AM
#6
All you need to do is to change the first line of your code.
VB Code:
Dim i As Long
Dim j As Long
With List1
For i = .ListCount - 1 To 0 Step -1
For j = .ListCount To (i + 1) Step -1
If .List(j) = .List(i) Then
.RemoveItem j
End If
Next
Next
End With
BTW you didn't answer my question. It's easy to prevent duplicates from being in the list to begin with. Do you want to know how?
-
Apr 11th, 2004, 09:19 AM
#7
Thread Starter
New Member
thanks for the code, yeah tell me how
-
Apr 11th, 2004, 09:21 AM
#8
Thread Starter
New Member
also im doing the code to remove dups from a combo box, the line List i have replaced with items.item but it doesent work, do you have any sugesstions,
-
Apr 11th, 2004, 09:24 AM
#9
VB Code:
Option Explicit
Private Declare Function SendMessageString Lib "user32" Alias "SendMessageA" _
(ByVal hWnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
ByVal lParam As String) As Long
Private Const CB_FINDSTRINGEXACT = &H158 ' If you use a ComboBox
Private Const LB_FINDSTRINGEXACT = &H1A2
' In a sub or function
Dim lngRetVal As Long
lngRetVal = SendMessageString(List1.hWnd, _
LB_FINDSTRINGEXACT, -1&, _
"string to be added")
If lngRetVal > -1& Then
' It's already in the list and lngRetVal is the listindex
Else
List1.Additem "string to be added"
End If
-
Apr 11th, 2004, 09:35 AM
#10
Thread Starter
New Member
ok im working with the code youve just given me
<VBCODE>lngRetVal = SendMessageString(cboIUpREF.hWnd, _</VBCODE>
in vb.net its giving me a blue line under cboIUpREF.hWnd saying hWnd isnt a member of System.Windows.Forms.ComboBox, is there a different command for vb.net to use
-
Apr 11th, 2004, 09:38 AM
#11
You posted in the wrong forum I can't help you.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|