Results 1 to 9 of 9

Thread: Removing an item from a combo control...

  1. #1
    seh
    Guest

    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.

  2. #2
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    ComboBoxes don't have a .Item property?
    My evil laugh has a squeak in it.

    kristopherwilson.com

  3. #3
    PowerPoster
    Join Date
    Aug 2000
    Location
    IN SILENCE
    Posts
    6,441

    Well

    You would have to loop all items in combobox and when the right was is found use your code:

    VB Code:
    1. cboName.Item.RemoveItem (index as integer)
    Remaining quiet down here !!!

    BRAD HAS GIVEN ME THE ULTIMATIVE. I have chosen to stay....

  4. #4
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Maybe I'm going crazy here...but my ComboBoxes don't have a .Item Property. They do have:

    VB Code:
    1. Combo1.RemoveItem Index

    So if you want to remove an item with certain text, say "dog," then do:

    VB Code:
    1. Dim i As Integer
    2.  
    3.   For i = 0 To Combo1.ListCount - 1
    4.     If Combo1.List(i) = "dog" Then
    5.       Combo1.RemoveItem i
    6.       Exit For
    7.     End If
    8.   Next
    My evil laugh has a squeak in it.

    kristopherwilson.com

  5. #5
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    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")

  6. #6
    PowerPoster
    Join Date
    Aug 2000
    Location
    IN SILENCE
    Posts
    6,441

    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....

  7. #7
    seh
    Guest
    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!

  8. #8
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    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:
    1. Private Sub Command1_Click()
    2.  
    3. Dim lngStart As Long
    4. Dim intIndex As Integer
    5.  
    6. For intIndex = 1 To 50
    7.     Combo1.AddItem intIndex
    8. Next
    9.  
    10. lngStart = GetTickCount
    11.  
    12. Combo1.RemoveItem (SendMessageString(Combo1.hwnd, CB_FINDSTRING, -1&, "25"))
    13.  
    14. 'For intIndex = 0 To Combo1.ListCount - 1
    15. '    If Combo1.List(intIndex) = "25" Then
    16. '        Combo1.RemoveItem intIndex
    17. '    End If
    18. 'Next
    19.  
    20. MsgBox GetTickCount - lngStart

  9. #9
    seh
    Guest

    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
  •  



Click Here to Expand Forum to Full Width