如何使用C#找到文本框中插入符的位置?

4
我有一些 TextBoxMultiLine TextBoxRichTextBox 在我的 WindowsFormApps 中,使用 Visual Studio 2015。现在我想让我的用户在这些字段中输入文本,并且我想连续知道 Caret Pointer 的位置(X轴,Y轴),无论我的用户是在文本的末尾、开头还是文本的中间任何地方输入文本。

enter image description here

我能否在某些类级变量中持续获取正确的运行时位置(X轴,Y轴),以便在其他地方使用...???

添加的代码:

    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;
            }
        }
    }
2个回答

4

根据您编辑后的问题,我检查了代码并发现当光标位于最后一个字符时,该函数会返回空位置,所以我稍作更改,以下函数应该会给您想要的结果:

 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;
        }
    }
}

你能解释一下如何使用它,并将X轴和Y轴分别存储在两个不同的类级变量中吗? - Muhammad Hassan
@MuhammadHassan 根据您的评论,我编辑了我的答案。如果它解决了您的问题,请接受它以关闭问题。 - Mukesh Adhvaryu
你能否发布你的代码,这样我就可以看到为什么会出现错误。 - Mukesh Adhvaryu
太好了,这正是我想要的。非常感谢... :-) - Muhammad Hassan
提示:如果我们使用Point locationOnForm = textBox1.FindForm().PointToClient(textBox1.Parent.PointToScreen(textBox1.Location));,那么我们就可以获取相对于父窗体的textBox1位置,然后可以将其与您的代码相加以获取相对于窗体的CARET轴。 :) - Muhammad Hassan
显示剩余5条评论

2
你可以使用TextBox的.SelectionStart属性来获取光标的位置。该属性详细信息请参考.SelectionStart
int positionOfcarett = txtSample.SelectionStart;

假设文本框 txtSample 中的文本为 ABCD,并且光标当前位于 BC 之间,上述代码将给出 positionOfcarett =2

根据评论更新:

如果您想获取鼠标在文本框上的位置,您需要使用 TextBox1_MouseMove 事件。从中您将获得 X 轴和 Y 轴的值。

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

如果你想引用表单,则使用Form__MouseMove

抱歉,我不明白你的意思。在文本框内,x轴和y轴是什么意思? - sujith karivelil
我已经获取了闪烁光标的插入点位置,现在我想找出它相对于整个窗体的鼠标指针位置,即(X轴,Y轴)的位置...??? - Muhammad Hassan
你应该使用文本框的 TextBox1_MouseMove 事件。 - sujith karivelil
它会回复鼠标光标位置(X轴,Y轴)还是插入符号位置(X轴,Y轴)...??? - Muhammad Hassan
@MuhammadHassan:那么你可以使用第一种选项。并将其用于Textbox的Click事件中。 - sujith karivelil

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接