0

I spent many days for sending text value to focused textbox of other application in C#.

My code:

IntPtr txtLot = FindWindowEx(orderdialog, new IntPtr(0), "Edit", null);

SendMessage(txtLot, WM_SETTEXT, 0, lot.ToString());

I want any one solution of my two choice.

Choice 1: There are three edit controls that I found in spy++. In the Above code, I got only 1st edit control. But I need to get IntPtr of 2nd edit control value to send some string.

Choice 2: We can use tab key to focus the 2nd edit control. In that, 2nd control is focused but I can't able to send string. Because I don't know the IntPtr of focused control textbox. How to get the IntPtr of focused control.

Please suggest me which choice is suitable.

Please help me.

Drew Kennedy
  • 4,118
  • 4
  • 24
  • 34
Praveen
  • 52
  • 1
  • 8

1 Answers1

0

You can iterate through all controls using the second parameter of FindWindowEx method.

According to the MSDN:

hwndChildAfter [in, optional]

Type: HWND

A handle to a child window.

The search begins with the next child window in the Z order. The child window must be a direct child window of hwndParent, not just a descendant window. If hwndChildAfter is NULL, the search begins with the first child window of hwndParent.

So you can use simple loop:

IntPtr fromHandle = IntPtr.Zero;
while (true) 
{
    IntPtr txtLot = FindWindowEx(orderdialog, fromHandle, "Edit", null);
    if (txtLot == IntPtr.Zero) break; // not found
    if (/*check if control satisfy some conditions*/) {
        SendMessage(txtLot, WM_SETTEXT, 0, lot.ToString());
    }
    fromHandle = txtLot;
}
kreig
  • 990
  • 11
  • 18
  • Thanks for your reply. But the above code is same as my code. "orderdialog" has 3 edit control, if we use this "FindWindowEx(orderdialog, fromHandle, "Edit", null);", it will get only the 1st edit control. I want 2nd edit control. **For Ex**: txtlot is 1st edit control and txtprice is 2nd edit control. I want is txtprice 2nd edit control. – Praveen May 23 '15 at 03:54
  • No, it's not the same. It will iterate through ALL your textboxes. I've specially mentioned the second parameter. We are changing `fromHandle` in the loop (setting to current found control) so next `FindWindowEx` call will find next textbox. It's a slightly modified working example from my old project. – kreig May 23 '15 at 07:52
  • I will try.. Any possibilities of choice 2. How to get the IntPtr of focused textbox control. – Praveen May 23 '15 at 08:53
  • Tou can find focused control hwnd using `GetFocus()` API call: http://www.pinvoke.net/default.aspx/user32.getfocus – kreig May 23 '15 at 12:57