Solved

If I have the hWnd of a textbox how do I get it's text?

Posted on 2004-04-19
7
541 Views
Last Modified: 2006-11-17
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.
0
Comment
Question by:rcherne1
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 19

Expert Comment

by:BrianGEFF719
ID: 10863979
       
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)
0
 

Author Comment

by:rcherne1
ID: 10864334
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
0
 
LVL 19

Expert Comment

by:BrianGEFF719
ID: 10864350
I dont think you can do it from your own program, try a different text box that is not owned by your application.


-Brian
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:rcherne1
ID: 10864450
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.
0
 
LVL 15

Accepted Solution

by:
unknown_routine earned 500 total points
ID: 10864640
Hi

Use this :


Option Explicit

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Integer, ByVal lParam As Any) As Long
Private Const WM_GETTEXT = &HD
Private Const WM_GETTEXTLENGTH = &HE


Private Sub Command1_Click()
    Dim n As Integer
 s As String
 q As Integer
s$ = Space$(255)
Dim k1, k2 As Long
n = SendMessage(Text1.hwnd, WM_GETTEXTLENGTH, k1, k2)
q = SendMessage(Text1.hwnd, WM_GETTEXT, 254, s$)
 s = Left(s, n)
 MsgBox s
End Sub

0
 
LVL 15

Expert Comment

by:unknown_routine
ID: 10864649
I forgot to put dims for 2 lines: add dims !


Dim s As String
Dim  q As Integer
0
 

Expert Comment

by:GentooJava
ID: 11011319
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
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

919 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

16 Experts available now in Live!

Get 1:1 Help Now