Reputation: 390
When I try to paste text in textbox another program, the text is inserted, but the program does not recognize it.
[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
const uint WM_SETTEXT = 0x000C;
IntPtr text = Marshal.StringToCoTaskMemUni("100");
IntPtr thisWindow = FindWindow(null, "AnotherWindow");
IntPtr handle = FindWindowEx(thisWindow, IntPtr.Zero, "AnotherTextBox", null);
SendMessage(handle, WM_SETTEXT, IntPtr.Zero, text);
Marshal.FreeCoTaskMem(text);
Maybe I should send to the parent a message that the textbox is updated? Like this:
//Wrong code, because I do not know how correctly send a message
SendMessage(handle, WM_COMMAND, EM_SETMODIFY, handle);
Upvotes: 0
Views: 3971
Reputation: 177
some Textboxes have been set so that you can not set your text by WM_SETTEXT immediately, especially those which accept digits and perform calculation according these digits. I had similar problem and solved it by bellow code. I applied WM_PASTE, EM_REPLACESEL to conquer that.
SendMessage(child, WM_SETFOCUS,0 , IntPtr.Zero); // go to text box
System.Windows.Forms.Clipboard.SetText("1"); // set something in clipboard. it does not matter what it is.
SendMessage(child, WM_PASTE, 0, IntPtr.Zero); // paste to get control of text box
SendMessage(child, WM_SETTEXT, IntPtr.Zero, string.Empty); // clear textbox to insert your desired text.
SendMessage(child, EM_REPLACESEL, IntPtr.Zero, "your text"); // insert your desired text. i inserted digits as text.
you need to Import user32.dll file at first:
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
private static extern int SendMessage(IntPtr hWnd, uint msg, IntPtr wParam, string lParam);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
private static extern int SendMessage(IntPtr hWnd, uint msg,int wParam, IntPtr lParam);
Upvotes: 0
Reputation: 390
And again...help came from another site
[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
//...
IntPtr boo = new IntPtr(1);
SendMessage(handle, EM_SETMODIFY, boo, IntPtr.Zero);
Upvotes: 1
Reputation: 37202
You'd be looking to do something like:
PostMessage(GetParent(handle), WM_COMMAND, MAKEWPARAM(GetWindowLong(handle, GWL_ID), EN_CHANGE), (LPARAM)handle);
Upvotes: 1