Results 1 to 6 of 6

Thread: How to - Find Handle of each Rich Text Box on a form

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2005
    Posts
    6

    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

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    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

  3. #3

    Thread Starter
    New Member
    Join Date
    May 2005
    Posts
    6

    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

  4. #4
    Addicted Member
    Join Date
    Nov 2005
    Posts
    169

    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.

  5. #5
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    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.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  6. #6
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    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.
    Attached Files Attached Files


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width