473,466 Members | 1,430 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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!
Jan 16 '06 #1
8 22331
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(hWnd, EM_GETLINECOUNT, 0, 0&)

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

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

ReDim bytBuffer(lngLen) As Byte

bytBuffer(0) = lngLen

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

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

GetTextBoxLine = strAns
End Function

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

Jay Taplin MCP
"Apach" <ne**@aha.com> wrote in message news:dq**********@bagan.srce.hr...
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!

Jan 16 '06 #2
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" <jt*****@integraware.com> wrote in message
news:zzVyf.6001$2x4.3941@trndny05...
: 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(hWnd, EM_GETLINECOUNT, 0, 0&)
:
: For lngLineNumber = 0 To lngLineCount - 1
: 'first character position of the line
: lngFirstCharPos = SendMessage(hWnd, EM_LINEINDEX, lngLineNumber,
0&)
:
: 'length of line
: lngLen = SendMessage(hWnd, EM_LINELENGTH, lngFirstCharPos, 0&)
:
: ReDim bytBuffer(lngLen) As Byte
:
: bytBuffer(0) = lngLen
:
: 'text of line saved to bytBuffer
: lngRet = SendMessage(hWnd, EM_GETLINE, lngLineNumber, bytBuffer(0))
:
: If lngRet Then
: strAns = strAns & Left$(StrConv(bytBuffer, vbUnicode), lngLen)
: End If
: Next
:
: GetTextBoxLine = strAns
: End Function
:
: You can call it by GetTextBoxLine([hWnd of Control])
:
: Jay Taplin MCP
: "Apach" <ne**@aha.com> wrote in message news:dq**********@bagan.srce.hr...
: > 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!
: >
:
:

Jan 16 '06 #3
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" <rg************@mvps.org> wrote in message
news:43*********************@news.astraweb.com...
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" <jt*****@integraware.com> wrote in message
news:zzVyf.6001$2x4.3941@trndny05...
: 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(hWnd, EM_GETLINECOUNT, 0, 0&)
:
: For lngLineNumber = 0 To lngLineCount - 1
: 'first character position of the line
: lngFirstCharPos = SendMessage(hWnd, EM_LINEINDEX, lngLineNumber,
0&)
:
: 'length of line
: lngLen = SendMessage(hWnd, EM_LINELENGTH, lngFirstCharPos, 0&)
:
: ReDim bytBuffer(lngLen) As Byte
:
: bytBuffer(0) = lngLen
:
: 'text of line saved to bytBuffer
: lngRet = SendMessage(hWnd, EM_GETLINE, lngLineNumber,
bytBuffer(0))
:
: If lngRet Then
: strAns = strAns & Left$(StrConv(bytBuffer, vbUnicode),
lngLen)
: End If
: Next
:
: GetTextBoxLine = strAns
: End Function
:
: You can call it by GetTextBoxLine([hWnd of Control])
:
: Jay Taplin MCP
: "Apach" <ne**@aha.com> wrote in message
news:dq**********@bagan.srce.hr...
: > 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!
: >
:
:

Jan 17 '06 #4
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(hwnd, WM_GETTEXTLENGTH, ByVal 0, ByVal 0)

strTemp = Space(lngLength)

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

GetText = strTemp
End Function

"Apach" <ne**@aha.com> wrote in message news:dq**********@bagan.srce.hr...
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!

Jan 17 '06 #5
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 "SendMessageA"
(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_GETTEXTLENGTH = &HE

Jay Taplin MCP
Jan 17 '06 #6
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
http://vbnet.mvps.org/

Please reply to the newsgroups so all can participate.


"Jay Taplin" <jt*****@integraware.com> wrote in message
news:3Y********************@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(hwnd, WM_GETTEXTLENGTH, ByVal 0, ByVal 0)
:
: strTemp = Space(lngLength)
:
: SendMessage hwnd, WM_GETTEXT, ByVal lngLength + 1, ByVal strTemp
:
: GetText = strTemp
: End Function
:
: "Apach" <ne**@aha.com> wrote in message news:dq**********@bagan.srce.hr...
: > 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!
: >
:
:

Jan 19 '06 #7
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" <ne**@aha.com> wrote in message news:dq**********@bagan.srce.hr...
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!

Jan 19 '06 #8
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 GetForegroundWindow. 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
EnumChildWindows (http://vbnet.mvps.org/code/enums/enumwindowsdemo.htm).

--

Randy Birch
MS MVP Visual Basic
http://vbnet.mvps.org/

Please reply to the newsgroups so all can participate.


"Peter" <ga*****@SPAMblueyonder.co.uk> wrote in message
news:_l*******************@fe1.news.blueyonder.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" <ne**@aha.com> wrote in message news:dq**********@bagan.srce.hr...
: > 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!
: >
:
:

Jan 20 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Justin Kennedy | last post by:
Hi, I have a client that wants to format content in Word, paste it into a textarea, and have the html render exactly as it matches the Word doc. I've tried some scripts out there that offer...
14
by: Seth Russell | last post by:
I'm running Kevin Roth's rte box and i want to deactivate the ability to past inside the box. People sometimes paste outrageous things in there that might break my site. How can I deactivate the...
3
by: Nick Haines | last post by:
I need to write my own custom text edit control.. but I'm not sure where to start - I've never written a custom control... the features I want are somewhat similar to the VS .Net text editor - text...
2
by: Ed A | last post by:
Hi all: I'm using a Rich Edit Text box to display debug info for my app, I can't seem to make the control scroll down to the bottom autmatically whenever I append a new text string...
0
by: Alex | last post by:
Interested in more .NET stuff visit www.dedicatedsolutions.co.uk The DataList is not as powerful as the DataGrid. It requires more work from you since it has no default data presentation format....
4
by: DC | last post by:
Newbie's question about .NET datagrid If I use: <asp:EditCommandColumn></asp:EditCommandColumn> When I edit a row, it will automatically show Update and Cancel Link. But when I use:...
4
by: Glenn M | last post by:
I have a shared XML file on a server . i also have one xslt file that performs a simple transform on in to view the data. now i want to have another page that lets users modify the shared xml...
1
by: Brian Heibert | last post by:
Can anyone tell me how to create a Edit menu I am new to VB .Net and this news group Thanks, Brian
0
by: RoxyBeginner | last post by:
I created a form with a switchboard where the user click on one of the buttons. When the form load i want the user to be able to add and edit data by clicking the appropriate button. When I click...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.