Get text from edit box

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Apach

    Get text from edit box

    Hellou!

    Please can anybody tell me how to Get text from edit box in some window
    (etc. notepad) if i have HWND of this edit box.

    I try by using GetWindowText API butt this API return text only if this text
    is caption of some window in title bar.

    Thanks!


  • Jay Taplin

    #2
    Re: Get text from edit box

    Here is a routine to do it. It is a modified version of what can be found
    at http://www.freevbcode.com/ShowCode.asp?ID=2493

    Public Function GetTextBoxLine( hWnd As Long) As String
    'INPUT: hWnd = Handle to text box
    'OUTPUT: Text of specified TextBox Control

    Dim lngLineCount As Long
    Dim lngLineNumber As Long
    Dim lngRet As Long
    Dim lngLen As Long
    Dim lngFirstCharPos As Long
    Dim bytBuffer() As Byte
    Dim strAns As String

    'get number of lines
    lngLineCount = SendMessage(hWn d, EM_GETLINECOUNT , 0, 0&)

    For lngLineNumber = 0 To lngLineCount - 1
    'first character position of the line
    lngFirstCharPos = SendMessage(hWn d, EM_LINEINDEX, lngLineNumber, 0&)

    'length of line
    lngLen = SendMessage(hWn d, EM_LINELENGTH, lngFirstCharPos , 0&)

    ReDim bytBuffer(lngLe n) As Byte

    bytBuffer(0) = lngLen

    'text of line saved to bytBuffer
    lngRet = SendMessage(hWn d, EM_GETLINE, lngLineNumber, bytBuffer(0))

    If lngRet Then
    strAns = strAns & Left$(StrConv(b ytBuffer, vbUnicode), lngLen)
    End If
    Next

    GetTextBoxLine = strAns
    End Function

    You can call it by GetTextBoxLine([hWnd of Control])

    Jay Taplin MCP
    "Apach" <nema@aha.com > wrote in message news:dqh2jn$ine $1@bagan.srce.h r...[color=blue]
    > Hellou!
    >
    > Please can anybody tell me how to Get text from edit box in some window
    > (etc. notepad) if i have HWND of this edit box.
    >
    > I try by using GetWindowText API butt this API return text only if this
    > text is caption of some window in title bar.
    >
    > Thanks!
    >[/color]


    Comment

    • Randy Birch

      #3
      Re: Get text from edit box

      Why the painful way? Just sendmessage with WM_GETTEXT and grab it all into a
      buffer in one go.

      --

      Randy Birch
      MS MVP Visual Basic


      Please reply to the newsgroups so all can participate.




      "Jay Taplin" <jtaplin@integr aware.com> wrote in message
      news:zzVyf.6001 $2x4.3941@trndn y05...
      : Here is a routine to do it. It is a modified version of what can be found
      : at http://www.freevbcode.com/ShowCode.asp?ID=2493
      :
      : Public Function GetTextBoxLine( hWnd As Long) As String
      : 'INPUT: hWnd = Handle to text box
      : 'OUTPUT: Text of specified TextBox Control
      :
      : Dim lngLineCount As Long
      : Dim lngLineNumber As Long
      : Dim lngRet As Long
      : Dim lngLen As Long
      : Dim lngFirstCharPos As Long
      : Dim bytBuffer() As Byte
      : Dim strAns As String
      :
      : 'get number of lines
      : lngLineCount = SendMessage(hWn d, EM_GETLINECOUNT , 0, 0&)
      :
      : For lngLineNumber = 0 To lngLineCount - 1
      : 'first character position of the line
      : lngFirstCharPos = SendMessage(hWn d, EM_LINEINDEX, lngLineNumber,
      0&)
      :
      : 'length of line
      : lngLen = SendMessage(hWn d, EM_LINELENGTH, lngFirstCharPos , 0&)
      :
      : ReDim bytBuffer(lngLe n) As Byte
      :
      : bytBuffer(0) = lngLen
      :
      : 'text of line saved to bytBuffer
      : lngRet = SendMessage(hWn d, EM_GETLINE, lngLineNumber, bytBuffer(0))
      :
      : If lngRet Then
      : strAns = strAns & Left$(StrConv(b ytBuffer, vbUnicode), lngLen)
      : End If
      : Next
      :
      : GetTextBoxLine = strAns
      : End Function
      :
      : You can call it by GetTextBoxLine([hWnd of Control])
      :
      : Jay Taplin MCP
      : "Apach" <nema@aha.com > wrote in message news:dqh2jn$ine $1@bagan.srce.h r...
      : > Hellou!
      : >
      : > Please can anybody tell me how to Get text from edit box in some window
      : > (etc. notepad) if i have HWND of this edit box.
      : >
      : > I try by using GetWindowText API butt this API return text only if this
      : > text is caption of some window in title bar.
      : >
      : > Thanks!
      : >
      :
      :

      Comment

      • Jay Taplin

        #4
        Re: Get text from edit box

        Nice. I searched and searched for that... I know I used it a long time ago,
        and couldn't find it! Like I said to someone else in a post the other day -
        "Randy is the man". :-)

        Thanks,
        Jay

        "Randy Birch" <rgb_removethis @mvps.org> wrote in message
        news:43cc2aac$0 $1620$c3e8da3@n ews.astraweb.co m...[color=blue]
        > Why the painful way? Just sendmessage with WM_GETTEXT and grab it all into
        > a
        > buffer in one go.
        >
        > --
        >
        > Randy Birch
        > MS MVP Visual Basic
        > http://vbnet.mvps.org/
        >
        > Please reply to the newsgroups so all can participate.
        >
        >
        >
        >
        > "Jay Taplin" <jtaplin@integr aware.com> wrote in message
        > news:zzVyf.6001 $2x4.3941@trndn y05...
        > : Here is a routine to do it. It is a modified version of what can be
        > found
        > : at http://www.freevbcode.com/ShowCode.asp?ID=2493
        > :
        > : Public Function GetTextBoxLine( hWnd As Long) As String
        > : 'INPUT: hWnd = Handle to text box
        > : 'OUTPUT: Text of specified TextBox Control
        > :
        > : Dim lngLineCount As Long
        > : Dim lngLineNumber As Long
        > : Dim lngRet As Long
        > : Dim lngLen As Long
        > : Dim lngFirstCharPos As Long
        > : Dim bytBuffer() As Byte
        > : Dim strAns As String
        > :
        > : 'get number of lines
        > : lngLineCount = SendMessage(hWn d, EM_GETLINECOUNT , 0, 0&)
        > :
        > : For lngLineNumber = 0 To lngLineCount - 1
        > : 'first character position of the line
        > : lngFirstCharPos = SendMessage(hWn d, EM_LINEINDEX, lngLineNumber,
        > 0&)
        > :
        > : 'length of line
        > : lngLen = SendMessage(hWn d, EM_LINELENGTH, lngFirstCharPos , 0&)
        > :
        > : ReDim bytBuffer(lngLe n) As Byte
        > :
        > : bytBuffer(0) = lngLen
        > :
        > : 'text of line saved to bytBuffer
        > : lngRet = SendMessage(hWn d, EM_GETLINE, lngLineNumber,
        > bytBuffer(0))
        > :
        > : If lngRet Then
        > : strAns = strAns & Left$(StrConv(b ytBuffer, vbUnicode),
        > lngLen)
        > : End If
        > : Next
        > :
        > : GetTextBoxLine = strAns
        > : End Function
        > :
        > : You can call it by GetTextBoxLine([hWnd of Control])
        > :
        > : Jay Taplin MCP
        > : "Apach" <nema@aha.com > wrote in message
        > news:dqh2jn$ine $1@bagan.srce.h r...
        > : > Hellou!
        > : >
        > : > Please can anybody tell me how to Get text from edit box in some
        > window
        > : > (etc. notepad) if i have HWND of this edit box.
        > : >
        > : > I try by using GetWindowText API butt this API return text only if
        > this
        > : > text is caption of some window in title bar.
        > : >
        > : > Thanks!
        > : >
        > :
        > :
        >[/color]


        Comment

        • Jay Taplin

          #5
          Re: Get text from edit box

          I knew I had done this before. Thanks to Randy Birch's post below, he
          reminded me that there was a much easier way. I found my code, so here it
          is:

          Public Function GetText(hwnd As Long) As String
          Dim strTemp As String
          Dim lngLength As Long

          lngLength = SendMessage(hwn d, WM_GETTEXTLENGT H, ByVal 0, ByVal 0)

          strTemp = Space(lngLength )

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

          GetText = strTemp
          End Function

          "Apach" <nema@aha.com > wrote in message news:dqh2jn$ine $1@bagan.srce.h r...[color=blue]
          > Hellou!
          >
          > Please can anybody tell me how to Get text from edit box in some window
          > (etc. notepad) if i have HWND of this edit box.
          >
          > I try by using GetWindowText API butt this API return text only if this
          > text is caption of some window in title bar.
          >
          > Thanks!
          >[/color]


          Comment

          • Jay Taplin

            #6
            Re: Get text from edit box

            And you'll probably want the API declaration and constants (sorry, running
            on too little sleep right now).

            Private Declare Function SendMessage Lib "user32" Alias "SendMessag eA"
            (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As
            Any) As Long
            Private Const WM_GETTEXT = &HD
            Private Const WM_GETTEXTLENGT H = &HE

            Jay Taplin MCP


            Comment

            • Randy Birch

              #7
              Re: Get text from edit box

              Just a FYI, the byval on the wparam members is unnecessary as the declare
              defines wparam byval. And I'd wrap the lngLength= code in an If test, as
              Space$() will fail if lngLength=0 (i.e. due to an invalid hwnd or a handle
              that doesn't support the WM_* messages sent).

              --

              Randy Birch
              MS MVP Visual Basic


              Please reply to the newsgroups so all can participate.




              "Jay Taplin" <jtaplin@integr aware.com> wrote in message
              news:3YCdnco357 YgqVHeRVn-hQ@adelphia.com ...
              :I knew I had done this before. Thanks to Randy Birch's post below, he
              : reminded me that there was a much easier way. I found my code, so here it
              : is:
              :
              : Public Function GetText(hwnd As Long) As String
              : Dim strTemp As String
              : Dim lngLength As Long
              :
              : lngLength = SendMessage(hwn d, WM_GETTEXTLENGT H, ByVal 0, ByVal 0)
              :
              : strTemp = Space(lngLength )
              :
              : SendMessage hwnd, WM_GETTEXT, ByVal lngLength + 1, ByVal strTemp
              :
              : GetText = strTemp
              : End Function
              :
              : "Apach" <nema@aha.com > wrote in message news:dqh2jn$ine $1@bagan.srce.h r...
              : > Hellou!
              : >
              : > Please can anybody tell me how to Get text from edit box in some window
              : > (etc. notepad) if i have HWND of this edit box.
              : >
              : > I try by using GetWindowText API butt this API return text only if this
              : > text is caption of some window in title bar.
              : >
              : > Thanks!
              : >
              :
              :

              Comment

              • Peter

                #8
                Re: Get text from edit box

                Hi
                I always keep an eye on this group as an occasional user of VB6 I've learnt
                a lot from other peoples problems.
                This one is again something new I'd like to try - how does one find out the
                hWnd of a window?

                TIA
                --
                Peter
                To err is human but it takes a computer to really mess things up!

                "Apach" <nema@aha.com > wrote in message news:dqh2jn$ine $1@bagan.srce.h r...[color=blue]
                > Hellou!
                >
                > Please can anybody tell me how to Get text from edit box in some window
                > (etc. notepad) if i have HWND of this edit box.
                >
                > I try by using GetWindowText API butt this API return text only if this
                > text is caption of some window in title bar.
                >
                > Thanks!
                >[/color]


                Comment

                • Randy Birch

                  #9
                  Re: Get text from edit box

                  If you know the class name and the current title of the window, you can use
                  FindWindow. If you know the window is the currently active (foreground)
                  window, you can use GetForegroundWi ndow. If you only know part of the title
                  name, you can use a "FindWindowLike " routine
                  (http://vbnet.mvps.org/code/system/fi...likesimple.htm (simple) /
                  http://vbnet.mvps.org/code/system/winclasstitle.htm (detailed)). If you
                  don't know anything about the window you can enumerate them all with
                  EnumWindows (http://vbnet.mvps.org/code/enums/enumwindows.htm) and
                  EnumChildWindow s (http://vbnet.mvps.org/code/enums/enumwindowsdemo.htm).

                  --

                  Randy Birch
                  MS MVP Visual Basic


                  Please reply to the newsgroups so all can participate.




                  "Peter" <gammaNO@SPAMbl ueyonder.co.uk> wrote in message
                  news:_lOzf.3518 7$lL4.13884@fe1 .news.blueyonde r.co.uk...
                  : Hi
                  : I always keep an eye on this group as an occasional user of VB6 I've
                  learnt
                  : a lot from other peoples problems.
                  : This one is again something new I'd like to try - how does one find out
                  the
                  : hWnd of a window?
                  :
                  : TIA
                  : --
                  : Peter
                  : To err is human but it takes a computer to really mess things up!
                  :
                  : "Apach" <nema@aha.com > wrote in message news:dqh2jn$ine $1@bagan.srce.h r...
                  : > Hellou!
                  : >
                  : > Please can anybody tell me how to Get text from edit box in some window
                  : > (etc. notepad) if i have HWND of this edit box.
                  : >
                  : > I try by using GetWindowText API butt this API return text only if this
                  : > text is caption of some window in title bar.
                  : >
                  : > Thanks!
                  : >
                  :
                  :

                  Comment

                  Working...