TextBox: get text at/around mouse position

  • Thread starter Thread starter Jed Ozone
  • Start date Start date
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.
 
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.
 
Back
Top