[DllImport("user32.dll")]
private static extern int SendMessage(IntPtr hWnd, int wMsg, int
wParam, [MarshalAs(UnmanagedType.LPStr)] string lParam);
private const int WM_SETTEXT = 0x0C;
private const int EM_REPLACESEL = 0xC2;
private void button1_Click(object sender, EventArgs e)
{
string txt = "_&*^%";
SendMessage(richTextBox1.Handle, EM_REPLACESEL,0, txt);
}
The EM_REPLACESEL will as it says replace the selection, if the caret
is at the end of the edit box it will just append and move the
selection to the end. It will append the "_%*^%" characters that you
mention just fine Note you can also set the selection manually as well
if you need to programatically make sure that the selection is at the
end (or for that matter in the middle), for that just use the message
EM_SETSEL.
Note, you only want to send key down messages to controls if you want
those controls to actually process the key, in the case of EditBoxes
and RichEdits this usually just means displaying it, if your app is the
one getting the key strokes its probably just the results of the
processing i.e. the characters that you want to send to the control,
i.e. settext replacesel much simpler than doing it char by char.
If what you want to do really and truely is to send keystrokes to the
control then use WM_KEYDOWN in the same kind of way as I demonstrated
above but with the lparam changed to an int instead of marshalled as a
lpstr. You will also need to know how to set the bits in the lparam
parameter, there is some doco here....
http://msdn.microsoft.com/en-us/library/ms646280(VS.85).aspx
Hope this helps.
Regards Tim.
--