Results 1 to 6 of 6

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

  1. #1
    New Member
    Join Date
    May 05
    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 02
    Location
    Bristol, UK
    Posts
    35,562

    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
    New Member
    Join Date
    May 05
    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
    Lively Member
    Join Date
    Nov 05
    Posts
    114

    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 08
    Posts
    6,773

    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.
    The better the information you give to begin with and the sooner you reply the sooner you will get help and get your problem resolved


    When I was young and in my prime I used to program all the time but now I'm old and getting gray I only program once a day

  6. #6
    PowerPoster
    Join Date
    Jan 08
    Posts
    6,773

    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
    The better the information you give to begin with and the sooner you reply the sooner you will get help and get your problem resolved


    When I was young and in my prime I used to program all the time but now I'm old and getting gray I only program once a day

Posting Permissions

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