【问题标题】:Get caret position in RichTextBox_Click event在 RichTextBox_Click 事件中获取插入符号位置
【发布时间】:2015-11-24 04:44:42
【问题描述】:

我正在开发一个包含RichTextBox 的文本编辑器。我想要实现的功能之一是随时在TextBox 中显示上述RichTextBox 的插入符号的当前行和列。

这是我使用的部分代码(我的其余代码与我的问题无关):

int selectionStart = richTextBox.SelectionStart;
int lineFromCharIndex = richTextBox.GetLineFromCharIndex(selectionStart);
int charIndexFromLine = richTextBox.GetFirstCharIndexFromLine(lineFromCharIndex);

currentLine = richTextBox.GetLineFromCharIndex(selectionStart) + 1;
currentCol = richTextBox.SelectionStart - charIndexFromLine + 1;

此时,我应该提一下,当有人使用RichTextBox 时,插入符号可以通过三种方式更改位置:

  • 通过更改TextRichTextBox
  • 使用键盘上的箭头键
  • 通过单击RichTextBox 上的任意位置

我在上面发布的代码在前两种情况下都没有问题。但是,在第三种情况下它并没有真正起作用。

我尝试使用Click 事件,我注意到selectionStart 变量的值总是0,这意味着我总是得到相同和错误的结果。此外,在 MouseClickMouseUp 等其他事件上使用相同的代码并不能解决我的问题,因为即使在这些事件期间,selectionStart 也是 0。

那么,每次用户点击RichTextBox时,如何获取当前行和列?

【问题讨论】:

  • 使用 RichTextBox.SelectionChanged 事件。并使用标签来显示位置。 codeproject.com 上有很多 RTB 编辑器项目
  • 使用RichTextBox_SelectionChanged 似乎可以解决问题。 SelectionStart 得到正确的值。谢谢。

标签: c# winforms click richtextbox


【解决方案1】:

你想要这样的东西:

private void richTextBox1_MouseUp(object sender, MouseEventArgs e)
{
        RichTextBox box = (RichTextBox)sender;
        Point mouseLocation = new Point(e.X, e.Y);
        box.SelectionStart = box.GetCharIndexFromPosition(mouseLocation);
        box.SelectionLength = 0;
        int selectionStart = richTextBox.SelectionStart;
        int lineFromCharIndex = box.GetLineFromCharIndex(selectionStart);
        int charIndexFromLine = box.GetFirstCharIndexFromLine(lineFromCharIndex);

        currentLine = box.GetLineFromCharIndex(selectionStart) + 1;
        currentCol = box.SelectionStart - charIndexFromLine + 1;
}

【讨论】:

  • 我收到一个错误:'EventArgs' does not contain a definition for 'Location' and no extension method 'Location' accepting a first argument of type 'EventArgs' could be found (are you missing a using directive or an assembly reference?)
  • 必须是MouseEventArgs,而不是EventArgs。谢谢。
  • 我遇到了一个错误,但我真的看不出这有什么帮助。当然,我得到了鼠标图标的位置,然后通过对 X 和 Y 值进行一些更改,我可以得到 Line 和 Column,但是在做了一些测试之后,我注意到它并不总是有帮助。我使用的是:e.X - rtb.Location.X / [the height of a character in pixels]e.Y - rtb.Location.Y / [the width of a character in pixels]。它有效,但仅当两个滚动条都位于最顶部/左侧位置时。进行任何更改来修复(我认为)有点太复杂了。
  • 您不必更改 e.X 和 e.Y。 e.X 和 e.Y 来自 MouseEventArgs,所以你必须使用这个事件。它从字面上告诉您鼠标点击的 RichTextBox 中的确切字符索引。那有意义吗?我完成了我的代码示例以将其与您的其他代码相吻合。看看它现在是如何工作的?
  • 另外,您必须使用RichTextBoxMouseUp 事件,而不是表单。如果你不这样做,正如你所说,那是不可能的。在您的代码或代码隐藏的某处确保有一个RichTextBox.MouseUp += richTextBox1_MouseUp。否则,您将无法捕捉到此事件。
【解决方案2】:

在我看来,您真正想要的是处理TextBoxBase.SelectionChanged 事件。然后 any 导致选择更改的操作将调用您的代码,并且作为一个额外的好处,当前选择将在您的事件处理程序被调用时更新,您将确保获得正确的值。

如果这不能满足您的特定需求,那么我一定没有理解这个问题。在这种情况下,请提供a good, minimal, complete code example,它清楚地显示了您要执行的操作,并准确描述该代码的功能以及与您希望它执行的操作有何不同。

【讨论】:

    猜你喜欢
    • 2014-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-23
    • 1970-01-01
    • 2015-04-22
    • 2013-05-08
    • 1970-01-01
    相关资源
    最近更新 更多