daniellyh
asked on
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
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
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.
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.
ASKER
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
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
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
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
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
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
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
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
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
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
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
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
ASKER
hi,
what is the declaration for "sendmessage"?it return the error of "sub or function not defined."
thx
rgds,
daniel
what is the declaration for "sendmessage"?it return the error of "sub or function not defined."
thx
rgds,
daniel
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
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
ASKER
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
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
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
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
Yeah it should combo in the combo box
SendMessage(Combo.hwnd, CB_FINDSTRING, -1, ByVal CStr(value))
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
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
ASKER
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
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
ASKER
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
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
Ok Idle_Mind said is correct. I will take back my statement.My code reduces Lines of Code.
My code is only 3 lines
My code is only 3 lines
ASKER
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
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
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.