rcherne1
asked on
If I have the hWnd of a textbox how do I get it's text?
I use API, But I don't know how to get the text from a text box using API. Can someone help me? I can get the hWnd or ID of the textbox or any window or control, but what is the function to extract the text from a textbox? Thanks for your help.
ASKER
BrianGEFF719,
I make a new projet and put two textboxes on a form, Then I drop this code in. The only thing I get is a crash of the app. What am I doing wrong?
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
Const WM_GETTEXT = &HD
Private Sub Form_Load()
Dim Txt1_hWnd As Long
Txt1_hWnd = Text1.hwnd
Text1.Text = "This is a text"
'Your Code
Dim buffer As String
buffer = String(255, Chr(0))
Call SendMessage(Txt1_hWnd, WM_GETTEXT, 255, buffer)
buffer = Left(buffer, InStr(1, buffer, Chr(0)) - 1)
'End Your Code
Text2.Text = buffer
End Sub
I make a new projet and put two textboxes on a form, Then I drop this code in. The only thing I get is a crash of the app. What am I doing wrong?
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
Const WM_GETTEXT = &HD
Private Sub Form_Load()
Dim Txt1_hWnd As Long
Txt1_hWnd = Text1.hwnd
Text1.Text = "This is a text"
'Your Code
Dim buffer As String
buffer = String(255, Chr(0))
Call SendMessage(Txt1_hWnd, WM_GETTEXT, 255, buffer)
buffer = Left(buffer, InStr(1, buffer, Chr(0)) - 1)
'End Your Code
Text2.Text = buffer
End Sub
I dont think you can do it from your own program, try a different text box that is not owned by your application.
-Brian
-Brian
ASKER
Actually, that’s exactly what I’m trying to do. I used the code only to simplify the problem. Can you write an example that will work? I’m changing the points to 500 because I feel this is more challenging than I first though.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I forgot to put dims for 2 lines: add dims !
Dim s As String
Dim q As Integer
Dim s As String
Dim q As Integer
OMG!!!
It took me a couple of hours to find out why this example code crashes!
Change the following line:
q = SendMessage(Text1.hwnd, WM_GETTEXT, 254, s$)
to
q = SendMessage(Text1.hwnd, WM_GETTEXT, 254, ByVal s$)
If you do not pass it "ByVal", your app will crash when you make this call.
:-)
--Nathan
It took me a couple of hours to find out why this example code crashes!
Change the following line:
q = SendMessage(Text1.hwnd, WM_GETTEXT, 254, s$)
to
q = SendMessage(Text1.hwnd, WM_GETTEXT, 254, ByVal s$)
If you do not pass it "ByVal", your app will crash when you make this call.
:-)
--Nathan
Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Public Const WM_GETTEXT = &HD
code:
dim buffer as string
Buffer = String(255, Chr(0))
SendMessage hwnd, WM_GETTEXT, 255, Buffer
Buffer = Left(Buffer, InStr(1, Buffer, Chr(0)) - 1)