• Welcome to Valhalla Legends Archive.
 

(VB-Intermediate) - Combo Box stuff

Started by Grok, March 10, 2003, 06:22 AM

Previous topic - Next topic

Grok

These functions work on a combo box.

Here are some other functions I wrote for work, to help me automate another process from VB.  Actually, I ended up wrapping these functions into a class, making their usage even too easy.

Option Explicit

Private ghWnd As Long
Private gdlgID As Long

Public Property Get hWnd() As Long
    hWnd = ghWnd
End Property

Public Property Let hWnd(ByVal hWnd As Long)
    ghWnd = hWnd
End Property

Public Property Get dlgID() As Long
    dlgID = gdlgID
End Property

Public Property Let dlgID(ByVal dlgID As Long)
    gdlgID = dlgID
End Property

Public Function AddString(ByVal NewString As String, Optional ByVal dlgID As Long, Optional ByVal hWnd As Long) As Long
    Dim ret As Long
    AddString = CB_ERR
    If hWnd = 0 Then hWnd = ghWnd
    If hWnd = 0 Then
        If IsMissing(dlgID) = True Then Exit Function       'no way to get hWnd if no dlgID
        hWnd = GetDlgItem(ghWnd, dlgID)
    End If
    If hWnd = 0 Then Exit Function                          'still 0, unable to continue
    AddString = SendMessageSTR(hWnd, CB_ADDSTRING, 0, NewString)
End Function

Public Function FindString(ByRef SearchString As String, Optional ByVal dlgID As Long, Optional ByVal hWnd As Long) As Long
    Dim ret As Long
    FindString = CB_ERR
    If hWnd = 0 Then hWnd = ghWnd
    If hWnd = 0 Then
        If IsMissing(dlgID) = True Then Exit Function       'no way to get hWnd if no dlgID
        hWnd = GetDlgItem(ghWnd, dlgID)
    End If
    If hWnd = 0 Then Exit Function                          'still 0, unable to continue
    FindString = SendMessageSTR(hWnd, CB_FINDSTRING, -1, SearchString)
End Function

Public Function SelectString(ByVal SearchString As String, Optional ByVal dlgID As Long, Optional ByVal hWnd As Long) As Long
    Dim ret As Long
    SelectString = CB_ERR
    If hWnd = 0 Then hWnd = ghWnd
    If hWnd = 0 Then
        If IsMissing(dlgID) = True Then Exit Function       'no way to get hWnd if no dlgID
        hWnd = GetDlgItem(ghWnd, dlgID)
    End If
    If hWnd = 0 Then Exit Function                          'still 0, unable to continue
    SelectString = SendMessageSTR(hWnd, CB_SELECTSTRING, -1, SearchString)
End Function

Usage is, for example:
   Dim objCCombo As New CCombo
    Dim hWnd As Long, hCbo As Long, lngCtrlId As Long
    Dim strReturn As String
    
    hWnd = FindWindow(vbNullString, "Some Window Caption")
    lngCtrlId = &H23F    'get value using Spy++
    hCbo = GetDlgItem(hWnd, lngCtrlId)
    objCCombo.hWnd = hWnd
    lret = objCCombo.SelectString(strReturn)

That should cause the given string to be selected, if it exists in the combobox values list.