为什么 GetCharIndexFromPosition() 不能正确返回文本框的最后一个字符? [英] Why the GetCharIndexFromPosition() doesn't return properly for last character of textbox?

查看:19
本文介绍了为什么 GetCharIndexFromPosition() 不能正确返回文本框的最后一个字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public partial class Form1 : Form
{
    TextBox textBox;
    public Form1()
    {
        InitializeComponent();
        textBox = new TextBox() { Height = 30, Width = 200, Text = "Syncfusion Software", Font = new Font("Arial", 11) };
        textBox.MouseMove += textBox_MouseMove;
        this.Controls.Add(textBox);

    }

    void textBox_MouseMove(object sender, MouseEventArgs e)
    {
        var selectionStart = textBox.GetCharIndexFromPosition(e.Location);
        textBox.SelectionStart = selectionStart;
        textBox.SelectionLength = 0;
    }
}

这是我的代码,这是我试图获得有关 TextBoxGetCharIndexFromPosition() 方法的清晰信息的简单示例.

Here is my code, this is simple sample that i tried to get clear information about GetCharIndexFromPosition() method of TextBox.

在鼠标移动中,我使用文本框的当前鼠标位置获取字符索引位置,并根据它设置文本框的选择开始.因此,如果我移动鼠标,则将根据鼠标移动设置选择开始或插入符号位置.但是当鼠标在文本末尾移动时存在问题,选择开始未设置为最后.它设置最后但更早.

In Mouse move, i get the char index position using the current mouse position of the textbox and set selection start of the textbox based on it. So that if i mouse move, then the selection start or caret position will be set based on the mouse move. But there was an issue with when mouse moved at the end of text, selection start is not set to last. it sets last but before.

例如,如果一个文本框包含文本stack",那么如果鼠标位置在k"之后,那么插入符号位置应该在末尾,但它显示在c"和k"之间.GetCharIndexPosition() 没有正确返回最后一个字符的值.让我知道这个问题的解决方案

For example, if a text box contains text "stack", then if mouse position is after the "k" then caret position should be at the end, but it displayed inbetween "c" and "k". GetCharIndexPosition() doesn't returns value properly for last character. Let me know solution for this

提前致谢.

问候,

文卡特桑 R

推荐答案

这是已知的记录行为.GetCharIndexFromPosition 方法的备注部分文档 包含以下重要注意事项:

This is a known documented behavior. The Remarks section of the GetCharIndexFromPosition method documentation contains the following Important note:

如果指定的位置不在控件的客户端矩形内,或者超出控件的最后一个字符,则返回值为最后一个字符的索引.

If the specified location is not within the client rectangle of the control, or is beyond the last character in the control, the return value is the index of the last character.

解决方法是使用反向方法 GetPositionFromCharIndex 调整返回的索引.

The workaround is to use the reverse method GetPositionFromCharIndex to adjust the returned index.

像这样

void textBox_MouseMove(object sender, MouseEventArgs e)
{
    var charIndex = textBox.GetCharIndexFromPosition(e.Location);
    var charPosition = textBox.GetPositionFromCharIndex(charIndex);
    if (e.Location.X > charPosition.X) charIndex++;
    textBox.Select(charIndex, 0);
}

P.S. 作为旁注,我不知道这种方法试图实现什么,但它肯定会阻止鼠标行为的标准文本选择.

P.S. As a side note, I have no idea what this method is trying to achieve, but for sure it prevents the standard text selection by mouse behavior.

这篇关于为什么 GetCharIndexFromPosition() 不能正确返回文本框的最后一个字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆