Cursor position in Rich textbox?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: Cursor position in Rich textbox?

  1. #1
    Join Date
    Feb 2008
    Posts
    20

    Cursor position in Rich textbox?

    How can I know the cursor position in Rich Textbox?
    Thank you.

  2. #2
    Join Date
    Jan 2006
    Location
    18 32' N / 73 52' E
    Posts
    416

    Thumbs up Re: Cursor position in Rich textbox?

    Quote Originally Posted by dorjoo
    How can I know the cursor position in Rich Textbox?
    Thank you.
    Here is one way to find out the cursor position in Rich Textbox.

    Code:
    private static int EM_LINEINDEX = 0xbb;
    [DllImport("user32.dll")]
    extern static int SendMessage(IntPtr hwnd, int message, int wparam, int lparam);
    private void UpdateCaretPos()
    {
    	  int line, col, index;
    	  index = rtfText.SelectionStart;
    	  line = rtfText.GetLineFromCharIndex(index);
    	  col = index - SendMessage(rtfText.Handle, EM_LINEINDEX, -1, 0);
    	  panCaret.Text = (++line).ToString() + ", " + (++col).ToString();
    }
    private void rtfText_TextChanged(object sender, EventArgs e)
    {
    	  UpdateCaretPos();
    }
    private void rtfText_KeyDown(object sender, EventArgs e)
    {
    	  UpdateCaretPos();
    }
     
    private void rtfText_KeyUp(object sender, EventArgs e)
    {
    	  UpdateCaretPos();
    }

  3. #3
    Join Date
    Dec 2006
    Posts
    203

    Re: Cursor position in Rich textbox?

    Easiest way would be to subscribe to the MouseMove event, and then you'll have the mouse position inside the richTextBox.
    Sincerely,

    Martin Svendsen

  4. #4
    Join Date
    Feb 2008
    Posts
    20

    Re: Cursor position in Rich textbox?

    Quote Originally Posted by MMH
    Here is one way to find out the cursor position in Rich Textbox.

    Code:
    private static int EM_LINEINDEX = 0xbb;
    [DllImport("user32.dll")]
    extern static int SendMessage(IntPtr hwnd, int message, int wparam, int lparam);
    private void UpdateCaretPos()
    {
    	  int line, col, index;
    	  index = rtfText.SelectionStart;
    	  line = rtfText.GetLineFromCharIndex(index);
    	  col = index - SendMessage(rtfText.Handle, EM_LINEINDEX, -1, 0);
    	  panCaret.Text = (++line).ToString() + ", " + (++col).ToString();
    }
    private void rtfText_TextChanged(object sender, EventArgs e)
    {
    	  UpdateCaretPos();
    }
    private void rtfText_KeyDown(object sender, EventArgs e)
    {
    	  UpdateCaretPos();
    }
     
    private void rtfText_KeyUp(object sender, EventArgs e)
    {
    	  UpdateCaretPos();
    }

    Hi.
    I am getting an error on DllImport. It says "Type or namespace could notbe found.
    Thanks.

  5. #5
    Join Date
    Jan 2006
    Location
    18 32' N / 73 52' E
    Posts
    416

    Thumbs up Re: Cursor position in Rich textbox?

    Sorry i forgot to mention about the namespace usage.

    it is..

    Code:
    using System.Runtime.InteropServices;
    Also you need to register RichText Box events for text changed, keyDown and keyUp. So register them in the forms constructor as show below, or do it in the form's designer file.

    Code:
    public frmMain()
    {
    	InitializeComponent();
    
    	this.rtfText.TextChanged += new System.EventHandler(this.rtfText_TextChanged);
    	this.rtfText.KeyDown += new System.Windows.Forms.KeyEventHandler(rtfText_KeyDown);
    	this.rtfText.KeyUp += new System.Windows.Forms.KeyEventHandler(rtfText_KeyUp);
    
    }
    Last edited by MMH; March 4th, 2008 at 12:30 AM.

  6. #6
    Join Date
    Aug 2012
    Posts
    2

    Re: Cursor position in Rich textbox?

    But I can't seem to get it to work for arrow keys? If I type it works, if I select a spot in the text it works, but if I move backwards with the arrow key the cursor seems to me to move but it doesn't update the position. It's as if index = richTextBox1.SelectionStart; doesn't function if you are just moving the cursor with the arrow keys and haven't actually inserted anything. Sorry if I am missing something basic - I am just learning. Maybe I put the code in the wrong place? Do arrow keys update for others? Thanks for any and all thoughts!!!

  7. #7
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,101

    Re: Cursor position in Rich textbox?

    Please show us your code. It makes it easier to identify problems.

    OH, BTW this thread is 4 YEARS OLD. Please do not revive old threads!
    Last edited by HanneSThEGreaT; August 2nd, 2012 at 07:16 AM.

  8. #8
    Join Date
    Aug 2012
    Posts
    2

    Re: Cursor position in Rich textbox?

    Oops - I didn't realize it was poor form to revive old threads. Sorry! Now that it is revived I will assume it is okay to post responses here in this thread - if this is not the case please let me know and I will start a brand new thread instead.

    Here is my code:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;

    namespace WindowsFormsApplication1
    {
    public partial class Form1 : Form{
    public Form1(){
    InitializeComponent();
    }

    OpenFileDialog ofd = new OpenFileDialog();
    SaveFileDialog sfd = new SaveFileDialog();
    string printit;

    // try adding that suggested code . . .
    private static int EM_LINEINDEX = 0xbb;
    [DllImport("user32.dll")]
    extern static int SendMessage(IntPtr hwnd, int message, int wparam, int lparam);
    private void UpdateCaretPos(){
    int line, col, index;
    index = richTextBox1.SelectionStart;
    line = richTextBox1.GetLineFromCharIndex(index);
    col = index - SendMessage(richTextBox1.Handle, EM_LINEINDEX, -1, 0);
    // panCaret.Text = (++line).ToString() + ", " + (++col).ToString();
    // try replacing . . .
    printit = (++line).ToString() + ", " + (++col).ToString();
    richTextBox2.Text = printit;
    }

    private void fileToolStripMenuItem_Click(object sender, EventArgs e){
    }

    private void openToolStripMenuItem_Click(object sender, EventArgs e) {

    ofd.Title = "RTF Opener";
    ofd.Filter = "Rich Text Files (*.rtf)|*.rtf|All files (*.*)|*.*";
    ofd.FileName = "";
    ofd.FilterIndex = 0;
    ofd.InitialDirectory = @"c:\_LAWPROG";
    if (ofd.ShowDialog() == DialogResult.OK) {
    richTextBox1.LoadFile(ofd.FileName);
    };

    }

    private void saveAsToolStripMenuItem_Click(object sender, EventArgs e){
    sfd.DefaultExt = ".rtf";
    sfd.OverwritePrompt = true;
    sfd.Title = "Save File as";
    if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK){
    richTextBox1.SaveFile(sfd.FileName, RichTextBoxStreamType.RichText);
    }
    }

    private void richTextBox1_TextChanged(object sender, EventArgs e){
    UpdateCaretPos();
    }
    }
    }


    Basically I set up two richtext boxes, the first one I type in, the second one is used to print output of where the cursor/caret is, so I can see if it is working. If I type in the first box, it works great as long as I am typing or inserting text - the text in richtextbox2 appears and updates appropriately. However if I then move back and forth with the arrow key, the text in richtextbox2 does not update. It ignores the cursor movement from the arrow key as if richTextBox1.SelectionStart is not triggered by that type of movement. If I move back with the arrow key and then hit the spacebar, then it picks up with where we are supposed to be and works properly.

    I thought it might be that the event of richTextBox1_TextChanged might be the problem - maybe the arrow key movement doesn't count as text changed. But I have tried other approaches, such as events for keydown or key up and those don't seem to fix the problem. I have also tried simpler approaches without the send message and dll, but again I get similar results. I have also seen some odd behavior on loading files in some of the variants that I have tried, but for the moment I would put that aside to see if I can get the basic approach to work.

    Any thoughts? It seems as if moving by arrows keys doesn't generate the results desired, I have to actually be typing in new text. Clicking to move the cursor by the mouse is similar ignored.

    Sorry if I am msising something basic - I am still new to visual studio.

    Thanks in advance for any and all thoughts!

  9. #9
    Join Date
    Jan 2014
    Posts
    3

    Re: Cursor position in Rich textbox?

    Quote Originally Posted by MMH View Post
    Here is one way to find out the cursor position in Rich Textbox.

    Code:
    private static int EM_LINEINDEX = 0xbb;
    [DllImport("user32.dll")]
    extern static int SendMessage(IntPtr hwnd, int message, int wparam, int lparam);
    private void UpdateCaretPos()
    {
    	  int line, col, index;
    	  index = rtfText.SelectionStart;
    	  line = rtfText.GetLineFromCharIndex(index);
    	  col = index - SendMessage(rtfText.Handle, EM_LINEINDEX, -1, 0);
    	  panCaret.Text = (++line).ToString() + ", " + (++col).ToString();
    }
    private void rtfText_TextChanged(object sender, EventArgs e)
    {
    	  UpdateCaretPos();
    }
    private void rtfText_KeyDown(object sender, EventArgs e)
    {
    	  UpdateCaretPos();
    }
     
    private void rtfText_KeyUp(object sender, EventArgs e)
    {
    	  UpdateCaretPos();
    }
    I can't agree more with you!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center