How to - Find Handle of each Rich Text Box on a form
Hello All,
Hope you can help?
I am trying to pass text from one form to another. On the target form I have two RichTextBoxes ( named RichTextBox1 and RichTextBox2 ). Now I can pass text ok to one of them ( I guess the first in the handle list ) but don't know how to specify each one individually. Do you know what I need to do?
Code below, thanks,
Public Declare Function SendMessageString Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Integer, ByVal lParam As Any) As Long
Public Const WM_SETTEXT = &HC
Public Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Sub Command1_Click()
lhWnd = FindWindow(vbNullString, "Form2")
lhWnd = FindWindowEx(lhWnd, 0, "RichTextWndClass", vbNullString)
SendMessageString lhWnd, WM_SETTEXT, 0, Text1.Text
End Sub
Re: How to - Find Handle of each Rich Text Box on a form
Doesn't the RTB have an hWnd property? (or perhaps Handle if you are using VB.Net)
If so, you don't need to use FindWindow, eg:
Code:
SendMessageString Form2.RichTextBox1.hWnd, WM_SETTEXT, 0, Text1.Text
Just in case you aren't aware, you can also set the text directly:
Code:
Form2.RichTextBox1.Text = Text1.Text
Re: How to - Find Handle of each Rich Text Box on a form
Thanks for the info.
Sadly not that simple.
I forgot to mention that the two forms are in different applications.
I want to be able to send text from app1 (form1) to app2 (form2/richtextbox1) and app2 (form2/richtextbox2).
So far I cannot differentiate between richtexbox1 and 2 and all my text sent from app1 to app2 goes to richtextbox1.
Hope this is clear, thanks.
Using Spy++ then I can see both RichTextBoxes and their handles.
Any info appreciated, thanks
Chris
Re: How to - Find Handle of each Rich Text Box on a form
Before you start sending text to app2, set the focus of the proper RTB. Using RTB.SetFocus
You could send a window message to WM_SETFOCUS or use API SetFocus.
Re: How to - Find Handle of each Rich Text Box on a form
The problem sounds more like he doesn't have the handle of richtextbox2. Setting focus is really not an issue as I can send messages to any textbox (regular or rich) on another app without setting the focus.
1 Attachment(s)
Re: How to - Find Handle of each Rich Text Box on a form
Download zip file. Compile and run App1 and App2. Make sure App2 is running. Click on Get Objects From App2 button then click on the other two buttons.