I am using the following code to search for a given string in either a listbox or a combo box, but the function always returns the index as "-1". The function fires from the change event of a textbox.
This is a program that I am converting from vb6.0, so I think that there may have been a problem in translation. There were two upgrade warnings, which I have left in in code, as they may be the root of the problem!
SendMessage Declaration:
Expand|Select|Wrap|Line Numbers
- Public Declare Function SendMessage Lib "User32" Alias "SendMessageA" (ByVal hWnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByRef lParam As String) As Integer
- Public Const CB_FINDSTRING As Short = &H14Cs
- Public Const CB_FINDSTRINGEXACT As Short = &H158s
- Public Const LB_FINDSTRING As Short = &H18Fs
- Public Const LB_FINDSTRINGEXACT As Short = &H1A2s
Expand|Select|Wrap|Line Numbers
- Index = FindFirstMatch(lstProductCode, txtProductCode.Text, -1, False)
Expand|Select|Wrap|Line Numbers
- Public Function FindFirstMatch(ByVal ctlSearch As System.Windows.Forms.Control, ByVal SearchString As String, ByVal FirstRow As Short, ByVal Exact As Boolean) As Short
- Dim Index As Integer
- 'UPGRADE_WARNING: TypeOf has a new behavior. Click for more: 'ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
- If TypeOf ctlSearch Is System.Windows.Forms.ComboBox Then
- If Exact Then
- Index = SendMessage(ctlSearch.Handle.ToInt32, CB_FINDSTRINGEXACT, FirstRow, SearchString)
- Else
- Index = SendMessage(ctlSearch.Handle.ToInt32, CB_FINDSTRING, FirstRow, SearchString)
- End If
- 'UPGRADE_WARNING: TypeOf has a new behavior. Click for more: 'ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
- ElseIf TypeOf ctlSearch Is System.Windows.Forms.ListBox Then
- If Exact Then
- Index = SendMessage(ctlSearch.Handle.ToInt32, LB_FINDSTRINGEXACT, FirstRow, SearchString)
- Else
- Index = SendMessage(ctlSearch.Handle.ToInt32, LB_FINDSTRING, FirstRow, SearchString)
- End If
- End If
- FindFirstMatch = Index
- If Index <> -1 Then
- Call frmMain.Populate_Screen(VB6.GetItemString(frmMain.lstProductCode, Index))
- End If
- End Function
Many Thanks
Tom