inserting text at mouse position in text or rich edit box

G

Guest

Hi
I have searched extensively for help on inserting text at the position of
the mouse pointer in a text box as the final step of drag and drop process
What I have come with is listed below - the problem is that the SendMessage
function doesn't seem to perform the expected operation of moving the caret
to position in the textbox where the mouse is pointing to. (can be text or
richtext box either is good - sample code is based of attemp with richtext)

I would be greatfull if someone could perhaps assist me by pointing out my
error
Thanks

my attempt:

[DllImport("user32.dll", CharSet=CharSet.Auto)]
public static extern int SendMessage(IntPtr hWnd, uint Msg, int wParam, int
lParam);

//DragOver event of the RichTextBox
private void txtMDXExpression_DragOver(object sender,
System.Windows.Forms.DragEventArgs e)
{
const int EM_SETSEL = 0x0400 + 177;
txtMDXExpression.Focus();
txtMDXExpression.SelectionLength = 0;

//make a point object from argument parameters
Point mouseP = new Point(e.X,e.Y);
//get position of mouse within textbox
Point scrPt = txtMDXExpression.PointToClient(mouseP);

SendMessage(txtMDXExpression.Handle, EM_SETSEL, scrPt.X, scrPt.X )
}
 
S

Stoitcho Goutsev \(100\) [C# MVP]

Hendrik,
If using RichTextBox take a look at
RichTextBox.GetCharIndexFromPosition method. This is what you are after, I
believe.
 
G

Guest

Hi

Many thanks
Can't believe I couldn't find that - works like a charm

Stoitcho Goutsev (100) said:
Hendrik,
If using RichTextBox take a look at
RichTextBox.GetCharIndexFromPosition method. This is what you are after, I
believe.


--
HTH
Stoitcho Goutsev (100) [C# MVP]

Hendrik said:
Hi
I have searched extensively for help on inserting text at the position of
the mouse pointer in a text box as the final step of drag and drop process
What I have come with is listed below - the problem is that the
SendMessage
function doesn't seem to perform the expected operation of moving the
caret
to position in the textbox where the mouse is pointing to. (can be text or
richtext box either is good - sample code is based of attemp with
richtext)

I would be greatfull if someone could perhaps assist me by pointing out my
error
Thanks

my attempt:

[DllImport("user32.dll", CharSet=CharSet.Auto)]
public static extern int SendMessage(IntPtr hWnd, uint Msg, int wParam,
int
lParam);

//DragOver event of the RichTextBox
private void txtMDXExpression_DragOver(object sender,
System.Windows.Forms.DragEventArgs e)
{
const int EM_SETSEL = 0x0400 + 177;
txtMDXExpression.Focus();
txtMDXExpression.SelectionLength = 0;

//make a point object from argument parameters
Point mouseP = new Point(e.X,e.Y);
//get position of mouse within textbox
Point scrPt = txtMDXExpression.PointToClient(mouseP);

SendMessage(txtMDXExpression.Handle, EM_SETSEL, scrPt.X, scrPt.X )
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top