如何倒在一个文本框,在C#代码滚动(How to scroll down in a textbox

2019-07-29 12:24发布

我使用的WinForms,我在一段时间更新一次的文本框(显示信息)。 然而,当文本到达框的末尾它产生滚动条,我不知道如何向下滚动至底部。 我唯一看到的是ScrollToCaret,但插入符号是在文本的开头。 什么是滚动的命令?

Answer 1:

您可以通过使用一个名为ScrollToCaret功能做到这一点。 您需要先插入符位置设置为文本框的末尾,那么你可以滚动到它。 以下是如何做到这一点:

        //move the caret to the end of the text
        textBox.SelectionStart = textBox.TextLength;
        //scroll to the caret
        textBox.ScrollToCaret();


Answer 2:

这是一个有点老问题,但没有建议的答案为我工作(当文本框具有焦点ScrollToCaret()只适用)。 因此,在任何情况下,更多的应该在某个时候寻找这一点,我想我会分享我所发现的解决方案:

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);
    }
}

信贷的解决方案应该去这个帖子在bytes.com: http://bytes.com/topic/c-sharp/answers/248500-scroll-bottom-textbox#post1005377



Answer 3:

如果使用文本框的AppendText通过()的方法,该文本将被添加到任何现有的文本的底部,并且控制将滚动,以显示它。



Answer 4:

你需要在你的文字的末尾来设置插入符:

textBox1.Text += "your new text";
textBox1.Select(textBox1.Text.Length - 1, 0);
textBox1.ScrollToCaret();


Answer 5:

C#中使用向上/向下滚动由Windows API(user32.dll中)

首先,我们必须定义一个恒定值:

const int EM_LINESCROLL = 0x00B6;
const int SB_HORZ = 0;
const int SB_VERT = 1;

然后,我们必须声明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);

最后,使用这些方法做真实的东西:

SetScrollPos(myTextBox.Handle,SB_VERT,myTextBox.Lines.Length-1,true);
SendMessage(myTextBox.Handle,EM_LINESCROLL,0,myTextBox.Lines.Length-1);

完成! 简单易用! 经测试! 原帖



Answer 6:

您可以使用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);
    }
}

(另)



Answer 7:

搜索和从未发现有和没有重点,以及水平和垂直方向工作的合法的解决方案后,我碰到的作品( - Win7的/ .NET4的WinForms至少我的平台)的API解决方案绊倒了。

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);
        }
    }
}

随着多文本框(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);


文章来源: How to scroll down in a textbox by code in C#