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.
Printable View
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.
ComboBoxes don't have a .Item property? :confused:
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)
Maybe I'm going crazy here...but my ComboBoxes don't have a .Item Property. They do have:
VB Code:
Combo1.RemoveItem Index
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
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")
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...
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!
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
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.