Solved

find value in VB 6.0 combo box

Posted on 2003-11-20
21
947 Views
Last Modified: 2013-12-26
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
0
Comment
Question by:daniellyh
  • 8
  • 7
  • 4
  • +1
21 Comments
 
LVL 11

Expert Comment

by:bingie
ID: 9793825
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.
0
 
LVL 11

Expert Comment

by:bingie
ID: 9793921
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.
0
 
LVL 1

Author Comment

by:daniellyh
ID: 9793964
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
0
 
LVL 5

Expert Comment

by:rajaamirapu
ID: 9794900
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
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 9794908
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
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 9794926
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
0
 
LVL 5

Expert Comment

by:rajaamirapu
ID: 9794985
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
0
 
LVL 5

Expert Comment

by:rajaamirapu
ID: 9795034
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
0
 
LVL 1

Author Comment

by:daniellyh
ID: 9795041
hi,
what is the declaration for "sendmessage"?it return the error of "sub or function not defined."

thx

rgds,
daniel
0
 
LVL 5

Expert Comment

by:rajaamirapu
ID: 9795053
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
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 1

Author Comment

by:daniellyh
ID: 9795059
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
0
 
LVL 5

Accepted Solution

by:
rajaamirapu earned 80 total points
ID: 9795066
How can i help you? daniel
0
 
LVL 1

Author Comment

by:daniellyh
ID: 9795075
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
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 9795108
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
0
 
LVL 5

Expert Comment

by:rajaamirapu
ID: 9795122
Yeah it should combo in the combo box
0
 
LVL 5

Expert Comment

by:rajaamirapu
ID: 9795125
SendMessage(Combo.hwnd, CB_FINDSTRING, -1, ByVal CStr(value))
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 9795143
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
0
 
LVL 1

Author Comment

by:daniellyh
ID: 9795144
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
0
 
LVL 1

Author Comment

by:daniellyh
ID: 9795162
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

0
 
LVL 5

Expert Comment

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

0
 
LVL 1

Author Comment

by:daniellyh
ID: 9795192
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
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now