如何获取 RichTextBox 控件中的当前行?

发布于 2024-08-30 09:45:36 字数 77 浏览 7 评论 0原文

假设我单击了 RichTextBox 控件内的某处。如何获取插入符当前所在的行?

顺便说一句,这是检索该行的整个文本字符串。

Say I clicked somewhere inside a RichTextBox control. How can I get the current line the caret is currently on?

Btw this is to retrieve the whole text string of that line.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

在梵高的星空下 2024-09-06 09:45:36

这对我有用:

this.WordWrap = false;
int cursorPosition = this.SelectionStart;
int lineIndex = this.GetLineFromCharIndex(cursorPosition);
string lineText = this.Lines[lineIndex];
this.WordWrap = true;

This worked for me:

this.WordWrap = false;
int cursorPosition = this.SelectionStart;
int lineIndex = this.GetLineFromCharIndex(cursorPosition);
string lineText = this.Lines[lineIndex];
this.WordWrap = true;
梦途 2024-09-06 09:45:36

这就是 RichTextBox.GetLineFromCharIndex()做。传递 SelectionStart 属性值。

That's what RichTextBox.GetLineFromCharIndex() does. Pass the SelectionStart property value.

淡看悲欢离合 2024-09-06 09:45:36

我的答案甚至比文森特的更棒,因为在获得正确的行号后,我注意到它在闪烁,非常可怕。因此,我添加了一个内存中的富文本框来在那里完成工作。

private int GetCharacterIndexOfSelection()
{
    var wordWrappedIndex = this.SelectionStart;

    RichTextBox scratch = new RichTextBox();
    scratch.Lines = this.Lines;
    scratch.SelectionStart = wordWrappedIndex;
    scratch.SelectionLength = 1;
    scratch.WordWrap = false;
    return scratch.SelectionStart;
}

private int GetLineNumberOfSelection()
{
    var selectionStartIndex = GetCharacterIndexOfSelection();

    RichTextBox scratch = new RichTextBox();
    scratch.Lines = this.Lines;
    scratch.SelectionStart = selectionStartIndex;
    scratch.SelectionLength = 1;
    scratch.WordWrap = false;
    return scratch.GetLineFromCharIndex(selectionStartIndex);
}

My answer is even more awesome than Vincent's because after getting the right line number I noticed it was flickering, horribly. So I added an in memory rich text box to do the work there.

private int GetCharacterIndexOfSelection()
{
    var wordWrappedIndex = this.SelectionStart;

    RichTextBox scratch = new RichTextBox();
    scratch.Lines = this.Lines;
    scratch.SelectionStart = wordWrappedIndex;
    scratch.SelectionLength = 1;
    scratch.WordWrap = false;
    return scratch.SelectionStart;
}

private int GetLineNumberOfSelection()
{
    var selectionStartIndex = GetCharacterIndexOfSelection();

    RichTextBox scratch = new RichTextBox();
    scratch.Lines = this.Lines;
    scratch.SelectionStart = selectionStartIndex;
    scratch.SelectionLength = 1;
    scratch.WordWrap = false;
    return scratch.GetLineFromCharIndex(selectionStartIndex);
}
脸赞 2024-09-06 09:45:36

One way is to send it the EM_LINEFROMCHAR Message. I'm sure there are other ways.

何处潇湘 2024-09-06 09:45:36

这是通过从文档的开头开始计数来实现的。

                var startlinenumber = FindLineNumber(RichText, RichText.Selection.Start);
                var endlinenumber = FindLineNumber(RichText, RichText.Selection.End);

这是实际的功能。

int FindLineNumber(RichTextBox rtb, TextPointer p)
{
    var findStart = p?.GetLineStartPosition(0);
    if (findStart == null) return 0;
    var startLinePosition = rtb.Document.ContentStart.GetLineStartPosition(0);
    if (startLinePosition == null) return 0;

    var foundLineNumber = 0;
    while (true)
    {
        if (startLinePosition.CompareTo(findStart) >= 0)
        {
            break;
        }

        startLinePosition = startLinePosition.GetLineStartPosition(1, out var result);
        if (result == 0)
        {
            break;
        }

        foundLineNumber++;
    }

    return foundLineNumber;
}

This works by counting from the beginning of the document.

                var startlinenumber = FindLineNumber(RichText, RichText.Selection.Start);
                var endlinenumber = FindLineNumber(RichText, RichText.Selection.End);

Here is the actual function.

int FindLineNumber(RichTextBox rtb, TextPointer p)
{
    var findStart = p?.GetLineStartPosition(0);
    if (findStart == null) return 0;
    var startLinePosition = rtb.Document.ContentStart.GetLineStartPosition(0);
    if (startLinePosition == null) return 0;

    var foundLineNumber = 0;
    while (true)
    {
        if (startLinePosition.CompareTo(findStart) >= 0)
        {
            break;
        }

        startLinePosition = startLinePosition.GetLineStartPosition(1, out var result);
        if (result == 0)
        {
            break;
        }

        foundLineNumber++;
    }

    return foundLineNumber;
}
著墨染雨君画夕 2024-09-06 09:45:36

如果您想从现在正在调试的编辑器控件中获取当前行号,那么

int lineNumber = (new StackTrace()).GetFrame(1).GetFileLineNumber();

我认为这对您的问题很有帮助。

If you want to get current Line Number from the editor Control where you are debugging now then

int lineNumber = (new StackTrace()).GetFrame(1).GetFileLineNumber();

I think it is helpful for your problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文