Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. The Location of the cursor in a Text Box

The Location of the cursor in a Text Box

Scheduled Pinned Locked Moved C#
question
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    max29297
    wrote on last edited by
    #1

    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.


    C M 2 Replies Last reply
    0
    • M max29297

      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.


      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      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 )

      1 Reply Last reply
      0
      • M max29297

        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.


        M Offline
        M Offline
        mav northwind
        wrote on last edited by
        #3

        There's the SelectionChanged event. This event is fired when the cursor is moved (because the cursor basically is a selection with SelectionLength==0) You'll have to calculate the line and column yourself from the given SelectionStart property.

        Regards, mav -- Black holes are the places where God divided by 0...

        M 1 Reply Last reply
        0
        • M mav northwind

          There's the SelectionChanged event. This event is fired when the cursor is moved (because the cursor basically is a selection with SelectionLength==0) You'll have to calculate the line and column yourself from the given SelectionStart property.

          Regards, mav -- Black holes are the places where God divided by 0...

          M Offline
          M Offline
          max29297
          wrote on last edited by
          #4

          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.


          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups