TextBox: get text at/around mouse position

J

Jed Ozone

In a TextBox, I'm using a MouseMove event to track the cursor position.

I want to get able to tell what text the current cursor position is at so I
can set the SelectionStart to that position.

private void textBox1_MouseMove(object sender, MouseEventArgs e)
{
Point p = textBox1.PointToClient(new Point(e.X, e.Y));
/* now what to do I do with Point to figure out the character
position in the text box??? */

SelectionStart = ????
}

The only solution I can figure out so far is to call the Windows API
user.dll and send a click event. This effectively sets the SelectionStart
position. Is there a way to send a click all within .NET and not use the
API?

Better yet would just to be able to determine directly the current character
position in the (multi-line) TextBox.
 
T

Tim Bücker

Jed Ozone said:
In a TextBox, I'm using a MouseMove event to track the cursor position.

I want to get able to tell what text the current cursor position is at so I
can set the SelectionStart to that position.

What about the SelectionStart property?

private void textBox1_MouseDown(object sender,
System.Windows.Forms.MouseEventArgs e)
{
MessageBox.Show(this.textBox1.SelectionStart.ToString());
}

Is that what you need?
Greetings,
Tim.
 

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