Muhammad Hassan
Muhammad Hassan

Reputation: 1304

How To Find The Caret Position In A TextBox Using C#?

I have some TextBox, MultiLine TextBox, RichTextBox in my WindowsFormApps using Visual Studio 2015. Now I want to let my user type text in these fields and along with I want to know the Caret Pointer position (X-Axis,Y-Axis) continuously whether my user type text in the end of text or in start of the text or any where in the middle of the text.

enter image description here

Can I get the correct position (X-Axis,Y-Axis) at Run Time continuously in some class level variables to use it on other places...???

CODE ADDED:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;

    namespace DEMO_Apps
    {
        public partial class MainForm : Form
        {
            int xpos;
            int ypos;
            public MainForm()
            {
                InitializeComponent();
            }
            private void textBox1_TextChanged(object sender, EventArgs e)
            {
                var o = Utility.GetCaretPoint(textBox1);
                xpos = o.X;
                ypos = o.Y;
                textBox2.Text = Convert.ToString(xpos + "," +  ypos);                
            }     
        }

        public static class Utility
        {
            ///for System.Windows.Forms.TextBox
            public static System.Drawing.Point GetCaretPoint(System.Windows.Forms.TextBox textBox)
            {
                return textBox.GetPositionFromCharIndex(textBox.SelectionStart);
            }

            ///for System.Windows.Controls.TextBox
            public static System.Windows.Point GetCaretPoint(System.Windows.Controls.TextBox textBox)
            {
                return textBox.GetRectFromCharacterIndex(textBox.SelectionStart).Location;
            }
        }
    }

Upvotes: 4

Views: 6604

Answers (2)

Mukesh Adhvaryu
Mukesh Adhvaryu

Reputation: 646

Based on your edited question, I checked the code and found that when caret is at last character, the function gives empty position so I changed a bit, the following Functions should give you what you want:

 namespace DEMO_Apps
{
    public partial class MainForm : Form
    {
        int xpos;
        int ypos;
        public MainForm()
        {
            InitializeComponent();
        }
        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            var o = Utility.GetCaretPoint(textBox1);
            xpos = o.X;
            ypos = o.Y;
            textBox2.Text = Convert.ToString(xpos + "," +  ypos);                
        }     
    }

    public static class Utility
    {
        ///for System.Windows.Forms.TextBox
        public static System.Drawing.Point GetCaretPoint(System.Windows.Forms.TextBox textBox)
        {
            int start = textBox.SelectionStart;
            if (start == textBox.TextLength)
            start --;

            return textBox.GetPositionFromCharIndex(start);
        }

        ///for System.Windows.Controls.TextBox requires reference to the following dlls: PresentationFramework, PresentationCore & WindowBase.
        //So if not using those dlls you should comment the code below:

        public static System.Windows.Point GetCaretPoint(System.Windows.Controls.TextBox textBox)
        {
            return textBox.GetRectFromCharacterIndex(textBox.SelectionStart).Location;
        }
    }
}

Upvotes: 4

sujith karivelil
sujith karivelil

Reputation: 29036

you can use .SelectionStart property of TextBox to get the Position of the caret.

int positionOfcarett = txtSample.SelectionStart;

Let the Text in the TextBox txtSample be ABCD and the caret is currently in between B and C the above code will give you positionOfcarett =2;

Updates as per comment:

If you want to get the mouse position over the Textbox you need to use TextBox1_MouseMove event From that you will get the X-axis values and Y-axis values as well.

 int xpos = e.X;
 int ypos = e.Y;

If you want to get it reference to the Form then use Form__MouseMove

Upvotes: 2

Related Questions