By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,693 Members | 1,132 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,693 IT Pros & Developers. It's quick & easy.

inserting text at mouse position in text or rich edit box

P: n/a
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 )
}
Nov 16 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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" <He*****@discussions.microsoft.com> wrote in message
news:40**********************************@microsof t.com...
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 )
}

Nov 16 '05 #2

P: n/a
Hi

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

"Stoitcho Goutsev (100) [C# MVP]" wrote:
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" <He*****@discussions.microsoft.com> wrote in message
news:40**********************************@microsof t.com...
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 )
}


Nov 16 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.