Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 664425
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:35:55+00:00 2026-05-13T23:35:55+00:00

How do I get the current line and column numbers in a RichTextBox in

  • 0

How do I get the current line and column numbers in a RichTextBox in a Winforms application?

NOTE

Folks, I just want a simple solution, if it’s available by someone and he’s willing to share it with us, and not links to do research! Just give me some code please! Otherwise, I’ll have to buy a control…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T23:35:56+00:00Added an answer on May 13, 2026 at 11:35 pm

    For row and column, check out the Richer RichTextBox project. It is an extended version of the RichTextBox which supports the row and column numbers. The code from the article:

    this.rtb.CursorPositionChanged += 
        new System.EventHandler(this.rtb_CursorPositionChanged);
    this.rtb.SelectionChanged += 
        new System.EventHandler(this.rtb_SelectionChanged);
    .
    .
    .
    private void rtb_CursorPositionChanged(object sender, System.EventArgs e)
    {
        int line = rtb.CurrentLine;
        int col = rtb.CurrentColumn;
        int pos = rtb.CurrentPosition;
    
        statusBar.Text = "Line " + line + ", Col " + col + 
                         ", Position " + pos;
    }
    
    private void rtb_SelectionChanged(object sender, System.EventArgs e)
    {
        int start = rtb.SelectionStart;
        int end = rtb.SelectionEnd;
        int length = rtb.SelectionLength;
    
        statusBar.Text = "Start " + start + ", End " + end + 
                         ", Length " + length;
    }
    

    To achieve such behavior, we need to extend the RichTextBox class in the following manner:

    using System;
    using System.Drawing;
    using System.Windows.Forms;
    
    namespace Nik.UserControls
    {
        public class RicherTextBox2 : System.Windows.Forms.RichTextBox
        {
            public event EventHandler CursorPositionChanged;
    
            protected virtual void OnCursorPositionChanged( EventArgs e )
            {
                if ( CursorPositionChanged != null )
                    CursorPositionChanged( this, e );
            }
    
            protected override void OnSelectionChanged( EventArgs e )
            {
                if ( SelectionLength == 0 )
                    OnCursorPositionChanged( e );
                else
                    base.OnSelectionChanged( e );
            }
    
            public int CurrentColumn
            {
                get { return CursorPosition.Column( this, SelectionStart ); }
            }
    
            public int CurrentLine
            {
                get { return CursorPosition.Line( this, SelectionStart ); }
            }
    
            public int CurrentPosition
            {
                get { return this.SelectionStart; }
            }
    
            public int SelectionEnd
            {
                get { return SelectionStart + SelectionLength; }
            }
        }
    
        internal class CursorPosition
        {
            [System.Runtime.InteropServices.DllImport("user32")] 
            public static extern int GetCaretPos(ref Point lpPoint);
    
            private static int GetCorrection(RichTextBox e, int index)
            {
                Point pt1 = Point.Empty;
                GetCaretPos(ref pt1);
                Point pt2 = e.GetPositionFromCharIndex(index);
    
                if ( pt1 != pt2 )
                    return 1;
                else
                    return 0;
            }
    
            public static int Line( RichTextBox e, int index )
            {
                 int correction = GetCorrection( e, index );
                 return e.GetLineFromCharIndex( index ) - correction + 1;
            }
    
            public static int Column( RichTextBox e, int index1 )
            {
                 int correction = GetCorrection( e, index1 );
                 Point p = e.GetPositionFromCharIndex( index1 - correction );
    
                 if ( p.X == 1 )
                     return 1;
    
                 p.X = 0;
                 int index2 = e.GetCharIndexFromPosition( p );
    
                 int col = index1 - index2 + 1;
    
                 return col;
             }
        }
    }
    

    Displaying line number in RichTextBox for the line number portion.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just trying to get my irb sessions to actually list the current line of
Is it possible to get the current source line number in Perl? The equivalent
How do I get the current wallpaper on a Mac? Just point me to
How do I get the current size of a matrix stack (GL_MODELVIEW, GL_PROJECTION, GL_TEXTURE)
How do you get the current directory where your app is running?
How do I get the current time in Python?
Is there a way to get the current xml data when we make our
does any one know how to get the current motherboard, processor or HD temperature
What is the best way to get the current system time milliseconds?
Is there a common way to get the current time in or with milliseconds?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.