The Location of the cursor in a Text Box
-
Is there a property of the Text Box that tells you the line and column of the cursor? And also, is there an event that tells you when these properties have changed? I've looked for both, but I can't seem to find them. I'll look again, to make sure...
If I had a sig, it would probably go here.
-
Is there a property of the Text Box that tells you the line and column of the cursor? And also, is there an event that tells you when these properties have changed? I've looked for both, but I can't seem to find them. I'll look again, to make sure...
If I had a sig, it would probably go here.
There are two properties for selected text, something like SelectionStart and SelectionLength. The first one will give you the cursor position, but not line position in a multiline text box. I don't think there's an event that's fired whenever any property is changed.
Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
-
Is there a property of the Text Box that tells you the line and column of the cursor? And also, is there an event that tells you when these properties have changed? I've looked for both, but I can't seem to find them. I'll look again, to make sure...
If I had a sig, it would probably go here.
There's the
SelectionChanged
event. This event is fired when the cursor is moved (because the cursor basically is a selection withSelectionLength==0
) You'll have to calculate the line and column yourself from the givenSelectionStart
property.Regards, mav -- Black holes are the places where God divided by 0...
-
There's the
SelectionChanged
event. This event is fired when the cursor is moved (because the cursor basically is a selection withSelectionLength==0
) You'll have to calculate the line and column yourself from the givenSelectionStart
property.Regards, mav -- Black holes are the places where God divided by 0...
Thanks guys! Here's what I ended up doing...
char[] before = textBox.Substring(0, textBox.SelectionStart); int lineBreaks; int lastLineIndex; for (int a = 0; a < before; a++) { if (before[a] == '\n') { lineBreaks++; lastLineIndex = a + 1; } } cursorPositionLabel.Text = "Ln " + lineBreaks.ToString() cursorPositionLabel += " , Col " + (textBox.SelectionStart - lastLineIndex).ToString();
So much for preserving the tabs... :rolleyes:
If I had a sig, it would probably go here.