 |
 |

10-07-2004, 02:06 PM
|
 |
Senior Contributor
|
|
Join Date: Sep 2003
Posts: 814
|
|
combobox: automatch item, select in other combobox, remove from both
|
I have 2 comboboxes and a listbox. I want the user to select an item from one combo, with then matches the item to its corresponding item in the 2nd combo, then add both to a list box. The user can either select from the 1st combo or from the 2nd...either one will match to the corresponding item in the other. Both comboboxes also have to be autocompleting. Once the selection is added to the listbox, the user can remove it which will delete the item from the list and add them back into the appropriate comboboxes. I have this working except for one thing.
When autocompleting, if the user types in what they want and its found and they hit ENTER, I am getting an error when trying to remove the item from the combos. It tells me the index is invalid and sure enough, when I check what the index is, its -1. It seems that the user isn't selecting an item in the combo even though its matched.
This is what I tried. It works if an item is clicked or chosen by pressing the up/down arrows. It also works if they type what they want, its matched, then they press up/down or some other number but will NOT work if they just type it, its found and they hit enter:
Code:
Private Sub cmdAdd_Click()
List1.AddItem cmbo(0).Text & " " & cmbo(1).Text
cmbo(0).RemoveItem (cmbo(0).ListIndex)
cmbo(1).RemoveItem (cmbo(1).ListIndex)
End Sub
Oddly, if I put the opposite combo item to be deleted, it works...so, if I just selected something from combo 1, the following works:
Code:
Private Sub cmdAdd_Click()
List1.AddItem cmbo(0).Text & " " & cmbo(1).Text
cmbo(0).RemoveItem (cmbo(1).ListIndex)
cmbo(1).RemoveItem (cmbo(1).ListIndex)
End Sub
Any ideas anyone? Perhaps I need to post the function I use for autocompleting? Let me know please...
|
|

10-07-2004, 02:21 PM
|
|
Senior Contributor
* Expert *
|
|
Join Date: May 2004
Location: Manchester, England.
Posts: 1,254
|
|
|
i think it would be best if we could see the function.
|
|

10-08-2004, 06:45 AM
|
 |
Senior Contributor
|
|
Join Date: Sep 2003
Posts: 814
|
|
Quote:
|
Originally Posted by stevo
i think it would be best if we could see the function.
|
I got it off a thread here and changed it a bit for my needs:
Code:
Option Explicit
'
'Implement CComboBox:SelectString to search and select item
'Y. Huang <yinghsuan_h@yahoo.com>
'
'Copyright? Naaa! But Copyleft...
'http://www.gnu.org/copyleft/copyleft.html
'
'In KeyPress Event Method: KeyAscii = AutoMatchCBBox(ComBoBox, KeyAscii)
'
'Reference: WinUser.h
'VB ComboBox doesn't have SelectString(), so SendMessage to the Window Handle
'#define CB_SELECTSTRING 0x014D
'#define CB_SHOWDROPDOWN 0x014F
'#define CBN_SELENDOK 9
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" ( _
ByVal hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
lParam As Any) As Long
Private Const CB_ERR = -1, CB_SELECTSTRING = &H14D, CB_SHOWDROPDOWN = &H14F, CBN_SELENDOK = 9
'call this function in KeyPress event method
Public Function AutoMatchCBBox(ByRef cbBox As ComboBox, ByVal KeyAscii As Integer, CmbFollow As ComboBox) As Integer
Dim strFindThis As String, bContinueSearch As Boolean
Dim lResult As Long, lStart As Long, lLength As Long
AutoMatchCBBox = 0 ' block cbBox since we handle everything
bContinueSearch = True
lStart = cbBox.SelStart
lLength = cbBox.SelLength
On Error GoTo ErrHandle
If KeyAscii < 32 Then 'control char
bContinueSearch = False
cbBox.SelLength = 0 'select nothing since we will delete/enter
If KeyAscii = Asc(vbBack) Then 'take care BackSpace and Delete first
If lLength = 0 Then 'delete last char
If Len(cbBox) > 0 Then ' in case user delete empty cbBox
cbBox.Text = Left(cbBox.Text, Len(cbBox) - 1)
End If
Else 'leave unselected char(s) and delete rest of text
cbBox.Text = Left(cbBox.Text, lStart)
End If
cbBox.SelStart = Len(cbBox) 'set insertion position @ the end of string
ElseIf KeyAscii = vbKeyReturn Then 'user select this string
' MsgBox cbBox.ListIndex <- when this is active, it returns -1
cbBox.SelStart = Len(cbBox)
lResult = SendMessage(cbBox.hwnd, CBN_SELENDOK, 0, 0)
AutoMatchCBBox = KeyAscii 'give caller a chance to handle "Enter"
End If
Else 'generate searching string
If lLength = 0 Then
strFindThis = cbBox.Text & Chr(KeyAscii) 'No selection, append it
Else
strFindThis = Left(cbBox.Text, lStart) & Chr(KeyAscii)
End If
End If
If bContinueSearch Then 'need to search
Call VBComBoBoxDroppedDown(cbBox) 'open dropdown list
lResult = SendMessage(cbBox.hwnd, CB_SELECTSTRING, -1, ByVal strFindThis)
If lResult = CB_ERR Then 'not found
cbBox.Text = strFindThis 'set cbBox as whatever it is
cbBox.SelLength = 0 'no selected char(s) since not found
cbBox.SelStart = Len(cbBox) 'set insertion position @ the end of string
Else
'found string, highlight rest of string for user
cbBox.SelStart = Len(strFindThis)
cbBox.SelLength = Len(cbBox) - cbBox.SelStart
CmbFollow.ListIndex = cbBox.ListIndex
End If
End If
On Error GoTo 0
Exit Function
ErrHandle:
'got problem, simply return whatever pass in
Debug.Print "Failed: AutoCompleteComboBox due to : " & Err.Description
Debug.Assert False
AutoMatchCBBox = KeyAscii
On Error GoTo 0
End Function
|
Last edited by msmeth; 10-08-2004 at 07:21 AM.
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
|
|
|
 |
|