如何通过C#中的代码在文本框中向下滚动
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1069497/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to scroll down in a textbox by code in C#
提问by Nefzen
I am using winforms, and I update a text box once in a while (showing messages). however, when the text reaches the end of the box it produces scrollbars and I don't know how to scroll down to the bottom. The only thing I see is ScrollToCaret, but Caret is at the beginning of the text. What is the command to scroll?
我正在使用 winforms,我不时更新一个文本框(显示消息)。但是,当文本到达框的末尾时,它会产生滚动条,我不知道如何向下滚动到底部。我唯一看到的是 ScrollToCaret,但 Caret 在文本的开头。滚动的命令是什么?
采纳答案by Doctor Jones
You can do this by making use of a function called ScrollToCaret. You need to first set the caret position to the end of the text box, then you can scroll to it. Here's how to do it:
您可以通过使用名为 ScrollToCaret 的函数来完成此操作。您需要先将插入符号位置设置到文本框的末尾,然后才能滚动到它。这是如何做到的:
//move the caret to the end of the text
textBox.SelectionStart = textBox.TextLength;
//scroll to the caret
textBox.ScrollToCaret();
回答by Francis B.
You need to set your caret at the end of your text:
您需要在文本末尾设置插入符号:
textBox1.Text += "your new text";
textBox1.Select(textBox1.Text.Length - 1, 0);
textBox1.ScrollToCaret();
回答by Thomas Levesque
You can use the SetScrollPos API :
您可以使用 SetScrollPos API:
[DllImport("user32.dll")]
static extern int SetScrollPos(IntPtr hWnd, int nBar, int nPos, bool bRedraw);
[DllImport("user32.dll")]
static extern bool GetScrollRange(IntPtr hWnd, int nBar, out int lpMinPos, out int lpMaxPos);
const int SB_HORZ = 0;
const int SB_VERT = 1;
const int SB_CTL = 2;
...
void ScrollToBottom(Control ctl)
{
int min;
int max;
if (GetScrollRange(ctl.Handle, SB_VERT, out min, out max))
{
SetScrollPos(ctl.Handle, SB_VERT, max, true);
}
}
(untested)
(未经测试)
回答by Metro
If you use the AppendText() method of the Textbox, the text will be added to the bottom of any existing text and the control will scroll to display it.
如果您使用文本框的 AppendText() 方法,文本将被添加到任何现有文本的底部,并且控件将滚动以显示它。
回答by Julian
This is a bit of an old question, but none of the suggested answers worked for me (ScrollToCaret() only works when the TextBox has focus). So in case any more should be searching for this at some point, I thought I'd share what I found to be the solution:
这是一个老问题,但没有一个建议的答案对我有用(ScrollToCaret() 仅在 TextBox 具有焦点时才有效)。因此,如果在某个时候还有更多人应该搜索这个,我想我会分享我发现的解决方案:
public class Utils
{
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern int SendMessage(IntPtr hWnd, int wMsg, IntPtr wParam, IntPtr lParam);
private const int WM_VSCROLL = 0x115;
private const int SB_BOTTOM = 7;
/// <summary>
/// Scrolls the vertical scroll bar of a multi-line text box to the bottom.
/// </summary>
/// <param name="tb">The text box to scroll</param>
public static void ScrollToBottom(TextBox tb)
{
SendMessage(tb.Handle, WM_VSCROLL, (IntPtr)SB_BOTTOM, IntPtr.Zero);
}
}
Credit for the solution should go to this post at bytes.com: http://bytes.com/topic/c-sharp/answers/248500-scroll-bottom-textbox#post1005377
解决方案的功劳应该转到 bytes.com 上的这篇文章:http://bytes.com/topic/c-sharp/answers/248500-scroll-bottom-textbox#post1005377
回答by Toxig
C# Use Scroll Up/Down by Windows API (user32.dll)
C# 使用 Scroll Up/Down by Windows API (user32.dll)
First, we have to define a constant value:
首先,我们必须定义一个常量值:
const int EM_LINESCROLL = 0x00B6;
const int SB_HORZ = 0;
const int SB_VERT = 1;
Then, we have to declare two external methods of user32.dll:
然后,我们要声明 user32.dll 的两个外部方法:
[DllImport("user32.dll")]
static extern int SetScrollPos(IntPtr hWnd, int nBar, int nPos, bool bRedraw);
[DllImport("user32.dll")]
static extern int SendMessage(IntPtr hWnd, int wMsg, int wParam, int lParam);
Finally, use these methods to do the real thing:
最后,使用这些方法来做真正的事情:
SetScrollPos(myTextBox.Handle,SB_VERT,myTextBox.Lines.Length-1,true);
SendMessage(myTextBox.Handle,EM_LINESCROLL,0,myTextBox.Lines.Length-1);
Done! Simple and easy! Tested! original post
完毕!简单易行!测试! 原帖
回答by Joe
After searching and never finding a legitimate solution that works with and without focus as well as horizontally and vertically, I stumbled across an API solution that works (at least for my platform - Win7 / .Net4 WinForms).
在搜索但从未找到可以在有焦点和没有焦点以及水平和垂直方向上工作的合法解决方案之后,我偶然发现了一个有效的 API 解决方案(至少对于我的平台 - Win7 / .Net4 WinForms)。
using System;
using System.Runtime.InteropServices;
namespace WindowsNative
{
/// <summary>
/// Provides scroll commands for things like Multiline Textboxes, UserControls, etc.
/// </summary>
public static class ScrollAPIs
{
[DllImport("user32.dll")]
internal static extern int GetScrollPos(IntPtr hWnd, int nBar);
[DllImport("user32.dll")]
internal static extern int SetScrollPos(IntPtr hWnd, int nBar, int nPos, bool bRedraw);
[DllImport("user32.dll")]
internal static extern int SendMessage(IntPtr hWnd, int wMsg, IntPtr wParam, IntPtr lParam);
public enum ScrollbarDirection
{
Horizontal = 0,
Vertical = 1,
}
private enum Messages
{
WM_HSCROLL = 0x0114,
WM_VSCROLL = 0x0115
}
public static int GetScrollPosition(IntPtr hWnd, ScrollbarDirection direction)
{
return GetScrollPos(hWnd, (int)direction);
}
public static void GetScrollPosition(IntPtr hWnd, out int horizontalPosition, out int verticalPosition)
{
horizontalPosition = GetScrollPos(hWnd, (int)ScrollbarDirection.Horizontal);
verticalPosition = GetScrollPos(hWnd, (int)ScrollbarDirection.Vertical);
}
public static void SetScrollPosition(IntPtr hwnd, int hozizontalPosition, int verticalPosition)
{
SetScrollPosition(hwnd, ScrollbarDirection.Horizontal, hozizontalPosition);
SetScrollPosition(hwnd, ScrollbarDirection.Vertical, verticalPosition);
}
public static void SetScrollPosition(IntPtr hwnd, ScrollbarDirection direction, int position)
{
//move the scroll bar
SetScrollPos(hwnd, (int)direction, position, true);
//convert the position to the windows message equivalent
IntPtr msgPosition = new IntPtr((position << 16) + 4);
Messages msg = (direction == ScrollbarDirection.Horizontal) ? Messages.WM_HSCROLL : Messages.WM_VSCROLL;
SendMessage(hwnd, (int)msg, msgPosition, IntPtr.Zero);
}
}
}
With a multiline textbox (tbx_main) use like:
使用多行文本框(tbx_main)使用如下:
int horzPos, vertPos;
ScrollAPIs.GetScrollPosition(tbx_main.Handle, out horzPos, out vertPos);
//make your changes
//something like the following where m_buffer is a string[]
tbx_main.Text = string.Join(Environment.NewLine, m_buffer);
tbx_main.SelectionStart = 0;
tbx_main.SelectionLength = 0;
ScrollAPIs.SetScrollPosition(tbx_main.Handle, horzPos, vertPos);