5

我正在努力在DataGridView 内的文本框编辑控件中移动插入符号,向上一行和向下一行,就像用户在按下向上和向下箭头时会得到的一样。

所以我的意思不是换行符之间的行,我的意思是文本框左右两侧之间的行。

我不能使用 GetCharIndexFromPosition 和 GetPositionFromCharIndex,因为并非所有文本都会始终显示在文本框显示区域中。

编辑: 我无法模拟 KeyPress,因为我正在处理 DataGridView 中的文本框单元格。我的目标实际上是让箭头键做他们在普通文本框中会做的事情,而不是从一行跳到另一行。

4

3 回答 3

1

这应该有效。

Point pOld = textBox1.GetPositionFromCharIndex(textBox1.SelectionStart);
Point pNew = new Point(pOld.X, pOld.Y + textBox1.Font.Height)
int charIndex = textBox1.GetCharIndexFromPosition(pNew);
textBox1.SelectionStart = charIndex;

我不认为这是最干净的解决方案。也许您应该查看 DataGridView 属性/键处理。

于 2012-06-20T16:59:03.383 回答
1

该方法GetPositionFromCharIndex()GetCharIndexFromPosition()两个限制:

  1. 它们不适用于超出文本框边界的文本
  2. 的字符索引TextBox.SelectionStart对于行尾的插入符号和下一行开头的插入符号是相同的。

要纠正此问题,您可以:

  1. 在使用上述方法之前,滚动文本框以显示相关行。
  2. 使用 user32.dll 中的 GetCaretPos 函数将其与 SelectionStart 的位置进行比较。如果它们不相等,则表示插入符号位于行尾。
  3. 模拟 {END} 键按下以将插入符号定位在行尾。

我遇到的另一个问题是TextBox.Lines指用换行符分隔的逻辑行,而函数TextBox.GetLineFromCharIndex()TextBox.GetFirstCharIndexFromLine()指的是在文本框中显示的视觉线(即从文本框的一侧到另一侧,不必有换行符人物)。不要把它们混在一起。

生成的代码(你可能声称丑陋,但有效)如下:

class Utils
{
    [DllImport("user32.dll")]
    static extern bool GetCaretPos(out System.Drawing.Point lpPoint);

    public static void LineUp(TextBox tb)
    {
        int oldCharIndex = tb.SelectionStart;
        int oldLineNo = tb.GetLineFromCharIndex(oldCharIndex);
        System.Drawing.Point oldCharPos = tb.GetPositionFromCharIndex(oldCharIndex);
        System.Drawing.Point oldCaretPos;
        if (GetCaretPos(out oldCaretPos))
        {
            if (oldCharPos == oldCaretPos)
            {
                if (oldLineNo > 0)
                {
                    tb.SelectionStart = tb.GetFirstCharIndexFromLine(oldLineNo - 1);
                    tb.ScrollToCaret();
                    System.Drawing.Point newPos = new System.Drawing.Point(oldCaretPos.X, oldCaretPos.Y - tb.Font.Height);
                    int newCharIndex = tb.GetCharIndexFromPosition(newPos);
                    if (tb.GetPositionFromCharIndex(newCharIndex).Y == newPos.Y)
                    {
                        tb.SelectionStart = newCharIndex;
                    }
                    else
                    {
                        tb.SelectionStart = tb.GetFirstCharIndexFromLine(oldLineNo - 1);
                        System.Windows.Forms.SendKeys.Send("{END}");
                    }
                }
            }
            else
            {
                if (oldLineNo > 1)
                {
                    tb.SelectionStart = tb.GetFirstCharIndexFromLine(oldLineNo - 2);
                    tb.ScrollToCaret();
                    System.Drawing.Point newPos = new System.Drawing.Point(oldCaretPos.X, oldCaretPos.Y - tb.Font.Height);
                    int newCharIndex = tb.GetCharIndexFromPosition(newPos);
                    if (tb.GetPositionFromCharIndex(newCharIndex).Y == newPos.Y)
                    {
                        tb.SelectionStart = newCharIndex;
                    }
                    else
                    {
                        tb.SelectionStart = tb.GetFirstCharIndexFromLine(oldLineNo - 2);
                        System.Windows.Forms.SendKeys.Send("{END}");
                    }
                }
            }
        }
    }

    public static void LineDown(TextBox tb)
    {
        int oldCharIndex = tb.SelectionStart;
        int oldLineNo = tb.GetLineFromCharIndex(oldCharIndex);
        System.Drawing.Point oldCharPos = tb.GetPositionFromCharIndex(oldCharIndex);
        System.Drawing.Point oldCaretPos;
        if (GetCaretPos(out oldCaretPos))
        {
            if (oldCharPos == oldCaretPos)
            {
                if (oldLineNo < tb.GetLineFromCharIndex(tb.Text.Length - 1))
                {
                    tb.SelectionStart = tb.GetFirstCharIndexFromLine(oldLineNo + 1);
                    tb.ScrollToCaret();
                    System.Drawing.Point newPos = new System.Drawing.Point(oldCaretPos.X, oldCaretPos.Y + tb.Font.Height);
                    int newCharIndex = tb.GetCharIndexFromPosition(newPos);
                    if (tb.GetPositionFromCharIndex(newCharIndex).Y == newPos.Y)
                    {
                        tb.SelectionStart = newCharIndex;
                    }
                    else
                    {
                        tb.SelectionStart = tb.GetFirstCharIndexFromLine(oldLineNo + 1);
                        System.Windows.Forms.SendKeys.Send("{END}");
                    }
                }
            }
            else
            {
                System.Drawing.Point newPos = new System.Drawing.Point(oldCaretPos.X, oldCaretPos.Y + tb.Font.Height);
                int newCharIndex = tb.GetCharIndexFromPosition(newPos);
                if (tb.GetPositionFromCharIndex(newCharIndex).Y == newPos.Y)
                {
                    tb.SelectionStart = newCharIndex;
                }
                else
                {
                    tb.SelectionStart = tb.GetFirstCharIndexFromLine(oldLineNo);
                    System.Windows.Forms.SendKeys.Send("{END}");
                }
            }
        }
    }
}

这个想法的功劳归功于这个答案,您可能还想看看MSDN 参考上的 GetCaretPos其他 Caret 函数

于 2012-06-22T16:19:41.837 回答
0
    /// ------------------------------------------------------------------------------------
    /// <summary>
    /// Processes up key when a grid cell is in the edit mode. This overrides the default
    /// behavior in a grid cell when it's being edited so using the up arrow will move the
    /// IP up one line rather than moving to the previous row.
    /// </summary>
    /// ------------------------------------------------------------------------------------
    protected virtual bool ProcessUpKey(TextBox txtBox)
    {
        // Don't override the default behavior if all the text is selected or not multi-line.
        if (txtBox.SelectedText == txtBox.Text || !txtBox.Multiline)
            return false;

        int selectionPosition = txtBox.SelectionStart;
        // Getting the position after the very last character doesn't work.
        if (selectionPosition == txtBox.Text.Length && selectionPosition > 0)
            selectionPosition--;
        Point pt = txtBox.GetPositionFromCharIndex(selectionPosition);

        if (pt.Y == 0)
            return false;

        pt.Y -= TextRenderer.MeasureText("x", txtBox.Font).Height;
        txtBox.SelectionStart = txtBox.GetCharIndexFromPosition(pt);
        return true;
    }

    /// ------------------------------------------------------------------------------------
    /// <summary>
    /// Processes down key when a grid cell is in the edit mode. This overrides the default
    /// behavior in a grid cell when it's being edited so using the down arrow will move the
    /// IP down one line rather than moving to the next row.
    /// </summary>
    /// ------------------------------------------------------------------------------------
    protected virtual bool ProcessDownKey(TextBox txtBox)
    {
        // Don't override the default behavior if all the text is selected or not multi-line.
        if (txtBox.SelectedText == txtBox.Text || !txtBox.Multiline)
            return false;

        int chrIndex = txtBox.SelectionStart;
        Point pt = txtBox.GetPositionFromCharIndex(chrIndex);
        pt.Y += TextRenderer.MeasureText("x", txtBox.Font).Height;
        var proposedNewSelection = txtBox.GetCharIndexFromPosition(pt);
        if (proposedNewSelection <= chrIndex)
            return false; // Don't let "down" take you *up*.
        txtBox.SelectionStart = proposedNewSelection;
        return true;
    }
于 2013-08-15T18:15:23.847 回答