Capture cursor location in a TextBox

  • Thread starter Shane Poppleton via .NET 247
  • Start date
S

Shane Poppleton via .NET 247

I am trying to capture the current line and current column position in a text box, i have successfully created a function that returns the line and column number for display in the status bar e.g.
"Ln 2 Col 5", but when do i fire it, i at first tried the TextChanged event, but unfortunately the line and col number is only right when someone is changing the text, if up, down, left, right, home, end, page down or page up is pressed it doesn't update.

I have also tried the KeyDown Event, but the event get fired before the cursor moves so the display is always wrong. I thought a solution was to use the following code.

string key;
switch (e.KeyData)
{
case Keys.Up:
key="{UP}";
break;
/// Etc., etc.
}
if (key!=null)
{
SendKeys.Send(key);
e.Handled = true;
}
statusBar1.Text = GetPosition(textBox1);


But it still doesn't work, any ideas.
 
M

Morten Wennevik

Hi Shane,

Try the KeyUp event. It's not entirely clear to me how your code works, but the display should update before you release the key, so the KeyUp event will fire after the update.
 

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