Reading textboxes with Windows API

Posted on 2006-03-29
Last Modified: 2013-12-03
What is the appropriate method (probably using SendMessage) for reading a textbox when all you have is the hwnd?  I keep looking and all I can see are constants to set the text, not read.  I don't see an EM_GETTEXT constant anywhere.
Question by:awptimus
    LVL 86

    Accepted Solution

    All you need to do is using 'WM_GETTEXT' - for an edit box, this will retrieve the text.
    LVL 48

    Expert Comment

    Or use GetWindowText function.

    Author Comment

    Thanks JKR, I had tried it, but gave up when I didn't get the right result, but I tried again and it works just fine now.

    This is the code I'm now using to get the text of the textbox
    Public Function GetText(hwnd As Long) As String
        Const WM_GETTEXTLENGTH = &HE
        Const WM_GETTEXT = &HD
        Dim strTemp As String
        Dim lngLength As Long

        lngLength = SendMessage(hwnd, WM_GETTEXTLENGTH, ByVal 0, ByVal 0)

        strTemp = Space(lngLength)

        SendMessage hwnd, WM_GETTEXT, ByVal lngLength + 1, ByVal strTemp

        GetText = strTemp
    End Function

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    This article shows how to make a Windows 7 gadget that extends its U/I with a flyout panel -- a window that pops out next to the gadget.  The example gadget shows several additional techniques:  How to automatically resize a gadget or flyout panel t…
    What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
    This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now