|
-
Jun 15th, 2002, 10:09 AM
#1
Removing an item from a combo control...
When I delete an item from a listview, I also want to remove it from the associated combo. I have this at the moment:
cboName.Item.RemoveItem (index as integer)
How do I know what the index value is? Or is there another way of doing this?
Thanks.
-
Jun 15th, 2002, 10:30 AM
#2
Stuck in the 80s
ComboBoxes don't have a .Item property?
-
Jun 15th, 2002, 12:15 PM
#3
PowerPoster
Well
You would have to loop all items in combobox and when the right was is found use your code:
VB Code:
cboName.Item.RemoveItem (index as integer)
Remaining quiet down here !!!
BRAD HAS GIVEN ME THE ULTIMATIVE. I have chosen to stay....
-
Jun 15th, 2002, 04:39 PM
#4
Stuck in the 80s
Maybe I'm going crazy here...but my ComboBoxes don't have a .Item Property. They do have:
So if you want to remove an item with certain text, say "dog," then do:
VB Code:
Dim i As Integer
For i = 0 To Combo1.ListCount - 1
If Combo1.List(i) = "dog" Then
Combo1.RemoveItem i
Exit For
End If
Next
-
Jun 15th, 2002, 05:45 PM
#5
You will find that it is much faster to use the SendMessageString API than to loop through all the entries.
Public 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
Public Const CB_FINDSTRING = &H14C ' For a ComboBox
Public Const LB_FINDSTRING = &H18F ' For a ListBox
Dim intIndex As Integer
' This returns the ListIndex for the search value. If not found it returns -1
intIndex = SendMessageString(cboName.hwnd, CB_FINDSTRING, -1&, "value to find")
-
Jun 15th, 2002, 06:07 PM
#6
PowerPoster
Well
MartinLiss : Is it always faster, even with say 50 items? Is this the preferred method? I like to program based on certain "frequently" used paractices... I know most people use loops. Just wondering...
Remaining quiet down here !!!
BRAD HAS GIVEN ME THE ULTIMATIVE. I have chosen to stay....
-
Jun 15th, 2002, 06:14 PM
#7
Cheers...the loop works nice, but leaves the combo with a blank item. It's not a major issue, but it looks a bit asthetically unpleasing :/
Any suggestions?
Also I haven't really touched on the whole API thing yet, so normal vb is better because I understand it!
-
Jun 15th, 2002, 06:33 PM
#8
I just did a test with a ComboBox containing the numbers 1 to 50 and I found that the loop code takes 3 to 4 times longer than the SendMessageString to find the middle entry.
VB Code:
Private Sub Command1_Click()
Dim lngStart As Long
Dim intIndex As Integer
For intIndex = 1 To 50
Combo1.AddItem intIndex
Next
lngStart = GetTickCount
Combo1.RemoveItem (SendMessageString(Combo1.hwnd, CB_FINDSTRING, -1&, "25"))
'For intIndex = 0 To Combo1.ListCount - 1
' If Combo1.List(intIndex) = "25" Then
' Combo1.RemoveItem intIndex
' End If
'Next
MsgBox GetTickCount - lngStart
-
Jun 15th, 2002, 07:41 PM
#9
Doh
It doesn't leave a blank item at all. I had a bit of code in the wrong place!
Thanks again all, just what I needed.
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
|