• Welcome to Valhalla Legends Archive.
 

(VB-Intermediate) EDIT control class

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

Previous topic - Next topic

Grok

More junk from my miscellaneous projects.  This code lets you get or set the value of an edit control in some process.  Obviously you want to use this on a foreign process, as you can change your own edit controls (text boxes) directly.

Option Explicit

Private ghWnd As Long
Private gdlgID As Long

Private Declare Function SendMessageSTR Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal WMSG As Long, ByVal wParam As Long, ByVal lParam As String) 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 GetText(ByRef EditText As String, Optional ByVal dlgID As Long, Optional ByVal hWnd As Long) As Long
    Dim hItem As Long
    Dim ret As Long
    Dim sMsg As String
    
    GetText = -1
    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
    sMsg = Space(255)
    GetText = SendMessageSTR(hWnd, WM_GETTEXT, Len(sMsg), sMsg)
    EditText = StripNulls(sMsg)
End Function

Public Function SetText(ByVal TextToSet As String, Optional ByVal dlgID As Long, Optional ByVal hWnd As Long) As Long
    
    Dim hItem As Long, ret As Long
    
    If IsMissing(hWnd) = True Then hWnd = ghWnd             'no edit handle provided, use module copy
    If hWnd <> 0 Then ghWnd = hWnd                          'if hWnd is now nonzero, save to module var
    If IsMissing(dlgID) = True Then dlgID = gdlgID          'if no control ID passed, use module copy
    If dlgID <> 0 Then gdlgID = dlgID                       'if control ID is now nonzero, save to module var
    If ghWnd = 0 Then Exit Function                         'still no valid handle
    SetText = SendMessageSTR(ghWnd, WM_SETTEXT, Len(TextToSet), TextToSet)
    
End Function

HTH someone.