find value in VB 6.0 combo box

hi all,
i need to do something like this using VB 6.0 combo box:

in the combo box's list, i want to search for a particular value, if found i want to remove it  using .removeitem method from the combo box.my problem is how to locate the particular value and get its index so that i can use use <.removeitem index> method to remove from the combo box.

thx.

daniel
LVL 1
daniellyhAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

bingieCommented:
This should work fine for you.

Private Sub Combo1_Click()
 RemItem = Combo1.ListIndex
 Combo1.RemoveItem (RemItem)
End Sub

When you click on the Item in the combo box, it will be removed.
bingieCommented:
If you want it to remove on a command button, change to this:


Private Sub Combo1_Click()
 RemItem = Combo1.ListIndex
End Sub

Private Sub Command1_Click()
 Combo1.RemoveItem (RemItem)
End Sub

Then you can select the item and click remove.
daniellyhAuthor Commented:
hi bingie,
thx for ur comment but this is not what i want, let me explain my question with 1 example:

let say i have a combo box with value "A","B","C".
in a function i MAY add value "D" in that combo box in certain condition.

but in another function i need to find whether the value "D" is inside the combo box,
if it is inside i need to remove it,
else i proceed to other statement.

the problem i facing is how to find the item "D" in my combo box, and get its index so that i can remove it using .removeitem method.

thx again.
Daniel
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

rajaamirapuCommented:
Using CB_FINDSTRING  you can find a perticular item
Public Const CB_FINDSTRING = &H14C
If you want to find out D you could use
Combo1.ListIndex = SendMessage(Combo1.hwnd, LB_FINDSTRING, -1, ByVal CStr("D"))
Get back to me if you have any problem
Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
Here you go:

Private Sub removeFromCombo(combo As ComboBox, value As String)
    Dim a As Integer, b As Integer
   
    b = combo.ListIndex
    For a = 0 To combo.ListCount - 1
        If combo.List(a) = value Then
            combo.RemoveItem (a)
            Exit For
        End If
    Next a
End Sub

Usage:

Private Sub someSub()
    removeFromCombo Combo1, "D" ' Remove "D" from Combo1 if it exists
End Sub
Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
Sorry, had some extra declartions in there...this is the barebones version:

Private Sub removeFromCombo(combo As ComboBox, value As String)
    Dim a As Integer
   
    For a = 0 To combo.ListCount - 1
        If combo.List(a) = value Then
            combo.RemoveItem (a)
            Exit For
        End If
    Next a
End Sub

Idle_Mind
rajaamirapuCommented:
Public Const CB_FINDSTRING = &H14C
No need for a loop you can do like this
Private Sub removeFromCombo(combo As ComboBox, value As String)
    Dim a As Integer
 
   a = SendMessage(Combo1.hwnd, LB_FINDSTRING, -1, ByVal CStr(value))
   combo.RemoveItem (a)

    End Sub

HTH
rajaamirapuCommented:
Sorry it should be CB_FINDSTRING
Public Const CB_FINDSTRING = &H14C
No need for a loop you can do like this
Private Sub removeFromCombo(combo As ComboBox, value As String)
    Dim a As Integer
 
   a = SendMessage(Combo1.hwnd, CB_FINDSTRING, -1, ByVal CStr(value))
   combo.RemoveItem (a)

    End Sub

HTH
daniellyhAuthor Commented:
hi,
what is the declaration for "sendmessage"?it return the error of "sub or function not defined."

thx

rgds,
daniel
rajaamirapuCommented:
sorry
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Integer, ByVal lParam As Any) As Long
daniellyhAuthor Commented:
hi all,
generally i think both idea of idle_mind and rajaamirapu can work (provided the "sendmessage" error solved). but idle_mind's code is a loop, so it will take some time to loop and find the value. this become a problem when the combo box's collection is huge.(for example few thousand items).

so actually if rajaamirapu's code works it actually meet my requirement because it directly search the desired item in the combo box.

anyway thanks for bother of ur efforts, i m looking forward for rajaamirapu's feedback. :)

rgds,
daniel
rajaamirapuCommented:
How can i help you? daniel

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
daniellyhAuthor Commented:
hi rajaamirapu,

thanks for ur fast response.

the code work after i change the LB_FINDSTRING to CB_FINDSTRING in your
a = SendMessage(Combo1.hwnd, LB_FINDSTRING, -1, ByVal CStr(value)) statement.

anyway its ok,thanks to idle_mind and for rajaamirapu, i will accept ur answer.


rgds,
daniel
Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
rajaamirapu,

If your going to pass in a combobox as a parameter you might as well use it.  This code only works conincidentally because you are working with Combo1 the whole time.

Public Const CB_FINDSTRING = &H14C

Private Sub removeFromCombo(combo As ComboBox, value As String) ' Passed in combo <--------
    Dim a As Integer
 
   a = SendMessage(Combo1.hwnd, CB_FINDSTRING, -1, ByVal CStr(value)) ' Searched Combo1 <-------
   combo.RemoveItem (a) ' Removed from combo <--------
End Sub

Otherwise, it looks great.

Idle_Mind
rajaamirapuCommented:
Yeah it should combo in the combo box
rajaamirapuCommented:
SendMessage(Combo.hwnd, CB_FINDSTRING, -1, ByVal CStr(value))
Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
Also, you state that, "No need for a loop you can do like this"

Using SendMessage() with CB_FINDSTRING uses a loop just like my code.

Reference http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/commctls/comboboxes/comboboxreference/comboboxmessages/cb_findstring.asp

"Specifies the zero-based index of the item preceding the first item to be searched. When the search reaches the bottom of the list box, it continues from the top of the list box back to the item specified by the wParam parameter. If wParam is 1, the entire list box is searched from the beginning. "

Also your code will puke if the item wasn't found in combobox as the SendMessage() call will return an error code which you are not checking for.

"If the search is unsuccessful, it is CB_ERR"

So you should have also declared the constant for CB_ERR and checked the result before attempting to remove the item.

When all said and done, your code would have been the same size as mine and no faster....except that mine was written correctly the first time.

Idle_Mind
daniellyhAuthor Commented:
hi all,
yes we must pass in an object combo box to the sub removefromcombo.

the a = SendMessage(Combo1.hwnd, CB_FINDSTRING, -1, ByVal CStr(value))  should be
a = SendMessage(Combo.hwnd, CB_FINDSTRING, -1, ByVal CStr(value))

so it will work to any combo object you pass in.

i think for other object like treeview, list, or listview should it work after we change the combo.hwnd to treeview.hwnd for example?

anyway listview got its own .finditem method, i just curious. :-)

rgds,
daniel
daniellyhAuthor Commented:
hi idle_mind,
i know it will return an error 5 when the value is not found, so i have modified rajaamirapu's code:

Private Sub removeFromCombo(combo As ComboBox, value As String)
    On Error GoTo err_handler
   
    Dim a As Integer
 
   a = SendMessage(combo.hwnd, CB_FINDSTRING, -1, ByVal CStr(value))
   combo.RemoveItem (a)
    Exit Sub
err_handler:
    Select Case Err.Number
    Case 5
        Resume Next
    Case Else
        MsgBox "Error " & Err.Number & ": " & Err.Description, vbExclamation, "Error"
        Exit Sub
    End Select
End Sub

rajaamirapuCommented:
Ok Idle_Mind said is correct. I will take back my statement.My code reduces Lines of Code.
My code is only 3 lines

daniellyhAuthor Commented:
hi,

hmm...i havn't do the testing on the performance issue of both ur codes. but the combo box in this case may contain 4-5 thousand items. those items i actually load from database and only loading the combo box it actually take about 1 min at a P4 machine.

so by physical looking on idle_mind's codes it shall take longer time when we need use loop to find a value inside a 4-5 thousand items combo box.

anyway thanks to idle_mind for ur opinion n idea.i really appreciate it.

rgds,
daniel
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.