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. Finding Char Index in current line

Finding Char Index in current line

Scheduled Pinned Locked Moved C#
questiondatabasehelp
2 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.
  • G Offline
    G Offline
    gonad
    wrote on last edited by
    #1

    this code shows me the current line and character index in a richtextbox control. the char index, however, is counted from position 0 at line 0 but i need the char index to start from 0 at each line and NOT from line 0. how can i do this? private void rtb1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) { //Shows the current Line of selection int Line = rtb1.GetLineFromCharIndex(rtb1.SelectionStart); MessageBox.Show(Line.ToString()); //Shows the Character Index of the selection int pos = rtb1.GetCharIndexFromPosition(new Point(e.X, e.Y)); MessageBox.Show(pos.ToString()); } Thanks for your help. .gonad

    I 1 Reply Last reply
    0
    • G gonad

      this code shows me the current line and character index in a richtextbox control. the char index, however, is counted from position 0 at line 0 but i need the char index to start from 0 at each line and NOT from line 0. how can i do this? private void rtb1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) { //Shows the current Line of selection int Line = rtb1.GetLineFromCharIndex(rtb1.SelectionStart); MessageBox.Show(Line.ToString()); //Shows the Character Index of the selection int pos = rtb1.GetCharIndexFromPosition(new Point(e.X, e.Y)); MessageBox.Show(pos.ToString()); } Thanks for your help. .gonad

      I Offline
      I Offline
      Ista
      wrote on last edited by
      #2

      the only way I might see is if you do this: int Line = rtb1.GetLineFromCharIndex(rtb1.SelectionStart); int pos = rtb1.SelectionStart; while ( (newLine = rtb1.GetLineFromCharIndex(pos--) == Line) MessageBox.Show("The beginning char index of the line is " + ++pos.ToString()); now that code is not tested I just typed it in but it should work for the char index of the first Line MessageBox.Show(String.Format("Your caret is {0} characters away from the beginning",rtf1.SelectionStart - pos); I'm not an expert yet, but I play one at work. Yeah and here too.

      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