如何使用C#滚动到WinForms TextBox中的指定行?

时间:2022-09-13 08:31:33

How can I scroll to a specified line in a WinForms TextBox using C#?

如何使用C#滚动到WinForms TextBox中的指定行?

Thanks

4 个解决方案

#1


Here's how you scroll to the selection:

以下是滚动到选择的方式:

textBox.ScrollToCaret();

To scroll to a specified line, you could loop through the TextBox.Lines property, total their lengths to find the start of the specified line and then set TextBox.SelectionStart to position the caret.

要滚动到指定的行,可以循环遍历TextBox.Lines属性,总计它们的长度以查找指定行的开头,然后设置TextBox.SelectionStart以放置插入符。

Something along the lines of this (untested code):

这个(未经测试的代码)的东西:

int position = 0;

for (int i = 0; i < lineToGoto; i++)
{
    position += textBox.Lines[i].Length;
}

textBox.SelectionStart = position;

textBox.ScrollToCaret();

#2


    private void MoveCaretToLine(TextBox txtBox, int lineNumber)
    {
        txtBox.HideSelection = false;
        txtBox.SelectionStart = txtBox.GetFirstCharIndexFromLine(lineNumber - 1);
        txtBox.SelectionLength = txtBox.Lines[lineNumber - 1].Length;
        txtBox.ScrollToCaret();
    }

#3


This is the best solution I found:

这是我发现的最佳解决方案:

const int EM_GETFIRSTVISIBLELINE = 0x00CE;
const int EM_LINESCROLL = 0x00B6;

[DllImport("user32.dll")]
static extern int SendMessage(IntPtr hWnd, int wMsg, int wParam, int lParam);

void SetLineIndex(TextBox tbx, int lineIndex)
{
  int currentLine = SendMessage(textBox1.Handle, EM_GETFIRSTVISIBLELINE, 0, 0);
  SendMessage(tbx.Handle, EM_LINESCROLL, 0, lineIndex - currentLine);
}

It has the benefit, that the selection and caret position is not changed.

它的好处是选择和插入位置不会改变。

#4


The looping answer to find the proper caret position has a couple of problems. First, for large text boxes, it's slow. Second, tab characters seem to confuse it. A more direct route is to use the text on the line that you want.

找到合适的插入位置的循环答案有几个问题。首先,对于大文本框,它很慢。其次,制表符似乎混淆了它。更直接的方法是使用您想要的行上的文本。

String textIWantShown = "Something on this line.";
int position = textBox.Text.IndexOf(textIWantShown);
textBox.SelectionStart = position;
textBox.ScrollToCaret();

This text must be unique, of course, but you can obtain it from the textBox.Lines array. In my case, I had prepended line numbers to the text I was displaying, so this made life easier.

当然,此文本必须是唯一的,但您可以从textBox.Lines数组中获取它。在我的情况下,我在我正在显示的文本中添加了行号,因此这使生活更轻松。

#1


Here's how you scroll to the selection:

以下是滚动到选择的方式:

textBox.ScrollToCaret();

To scroll to a specified line, you could loop through the TextBox.Lines property, total their lengths to find the start of the specified line and then set TextBox.SelectionStart to position the caret.

要滚动到指定的行,可以循环遍历TextBox.Lines属性,总计它们的长度以查找指定行的开头,然后设置TextBox.SelectionStart以放置插入符。

Something along the lines of this (untested code):

这个(未经测试的代码)的东西:

int position = 0;

for (int i = 0; i < lineToGoto; i++)
{
    position += textBox.Lines[i].Length;
}

textBox.SelectionStart = position;

textBox.ScrollToCaret();

#2


    private void MoveCaretToLine(TextBox txtBox, int lineNumber)
    {
        txtBox.HideSelection = false;
        txtBox.SelectionStart = txtBox.GetFirstCharIndexFromLine(lineNumber - 1);
        txtBox.SelectionLength = txtBox.Lines[lineNumber - 1].Length;
        txtBox.ScrollToCaret();
    }

#3


This is the best solution I found:

这是我发现的最佳解决方案:

const int EM_GETFIRSTVISIBLELINE = 0x00CE;
const int EM_LINESCROLL = 0x00B6;

[DllImport("user32.dll")]
static extern int SendMessage(IntPtr hWnd, int wMsg, int wParam, int lParam);

void SetLineIndex(TextBox tbx, int lineIndex)
{
  int currentLine = SendMessage(textBox1.Handle, EM_GETFIRSTVISIBLELINE, 0, 0);
  SendMessage(tbx.Handle, EM_LINESCROLL, 0, lineIndex - currentLine);
}

It has the benefit, that the selection and caret position is not changed.

它的好处是选择和插入位置不会改变。

#4


The looping answer to find the proper caret position has a couple of problems. First, for large text boxes, it's slow. Second, tab characters seem to confuse it. A more direct route is to use the text on the line that you want.

找到合适的插入位置的循环答案有几个问题。首先,对于大文本框,它很慢。其次,制表符似乎混淆了它。更直接的方法是使用您想要的行上的文本。

String textIWantShown = "Something on this line.";
int position = textBox.Text.IndexOf(textIWantShown);
textBox.SelectionStart = position;
textBox.ScrollToCaret();

This text must be unique, of course, but you can obtain it from the textBox.Lines array. In my case, I had prepended line numbers to the text I was displaying, so this made life easier.

当然,此文本必须是唯一的,但您可以从textBox.Lines数组中获取它。在我的情况下,我在我正在显示的文本中添加了行号,因此这使生活更轻松。