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. (Rich) Text Box and cursor position

(Rich) Text Box and cursor position

Scheduled Pinned Locked Moved C#
questionhelp
3 Posts 2 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.
  • F Offline
    F Offline
    Frank Olorin Rizzi
    wrote on last edited by
    #1

    Hi all. I've searched through the forums but couldn't find an answer. I have a rich text box (but the question stands even for "simple" text boxes), and I would like to get the cursor's position within it. Something like "line X column Y", or even just "char Z". Any suggestions? In particular, I'm trying to get to this info from the handler of a mouse click event on the rich text box --just in case the System.Windows.Forms.MouseEventArgs parameter can help Thanks in advance, F.O.R.

    F 1 Reply Last reply
    0
    • F Frank Olorin Rizzi

      Hi all. I've searched through the forums but couldn't find an answer. I have a rich text box (but the question stands even for "simple" text boxes), and I would like to get the cursor's position within it. Something like "line X column Y", or even just "char Z". Any suggestions? In particular, I'm trying to get to this info from the handler of a mouse click event on the rich text box --just in case the System.Windows.Forms.MouseEventArgs parameter can help Thanks in advance, F.O.R.

      F Offline
      F Offline
      Frank Olorin Rizzi
      wrote on last edited by
      #2

      Well, so far here's what I found... if anyone has any better idea, feel free to post :-) //In the handler for the mouse-click on the rich text box... //where e is the System.Windows.Forms.MouseEventArgs parameter... int ndex = this.rBox_MarkedUp.GetCharIndexFromPosition( new System.Drawing.Point(e.X, e.Y)); int line = this.rBox_MarkedUp.GetLineFromCharIndex(ndex); ndex is now the 0-based index of the char clicked and line is the number of the line (0-based as well). We now return to the usual stream of questions :-) Thanks, F.O.R.

      G 1 Reply Last reply
      0
      • F Frank Olorin Rizzi

        Well, so far here's what I found... if anyone has any better idea, feel free to post :-) //In the handler for the mouse-click on the rich text box... //where e is the System.Windows.Forms.MouseEventArgs parameter... int ndex = this.rBox_MarkedUp.GetCharIndexFromPosition( new System.Drawing.Point(e.X, e.Y)); int line = this.rBox_MarkedUp.GetLineFromCharIndex(ndex); ndex is now the 0-based index of the char clicked and line is the number of the line (0-based as well). We now return to the usual stream of questions :-) Thanks, F.O.R.

        G Offline
        G Offline
        gonad
        wrote on last edited by
        #3

        Hi. I just went thru this as well, but this is what i worked out. it's basically the same thing you already figured out. private void rtb1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) { leftMouse(e.X, e.Y); } private void leftMouse(int X, int Y) { posStart = rtb1.GetCharIndexFromPosition(new Point(X, Y)); }

        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