不一致的结果与RichTextBox的ScrollToCaret [英] Inconsistent Results with RichTextBox ScrollToCaret

查看:170
本文介绍了不一致的结果与RichTextBox的ScrollToCaret的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C#一个RichTextBox工作。它存在于一个TabPage的。当选择TabPage的,我的目标是填充RichTextBox中,并滚动到最后。我曾尝试对解决方案的细微变化这个共同的问题,主要的一个沿着线为:

I am working with a RichTextBox in C#. It exists on a TabPage. When the TabPage is selected, I aim to populate the RichTextBox, and scroll to the end. I have tried the slight variations on solutions for this common question, the main one being along the lines of:

MyRichTextBox.Select(MyRichTextBox.Text.Length, 0);  
MyRichTextBox.ScrollToCaret();  

MyRichTextBox.SelectionStart = MyRichTextBox.Text.Length;  
MyRichTextBox.ScrollToCaret();  

这是产生不一致的结果,尽管在可预见的方式。它将滚动至底部,和滚动一行短底部之间交替。分别所示(抱歉的联系,新的用户,所以我不能张贴图片):结果
成功滚动到滚动至底部的一行短底部的结果,
结果
我很惊讶地发现没有提过我的搜索这种行为,并已决定要问,如果有人在这里遇到了这一点,和/或心中都有一个解决方案。如果它归结到它,我想我可以沿的 itsmatt的回答

This is producing inconsistent results, albeit in a predictable manner. It will alternate between scrolling to the bottom, and scrolling one line short of the bottom. Respectively illustrated (sorry for the links, new user so I can't post the images):
Successfully scrolled to bottom
Scrolled to one line short of the bottom
I am surprised to find nothing mentioning this behaviour through my searches, and have decided to ask if anyone here has encountered this, and/or has a solution in mind. If it comes down to it, I suppose I can go with something along the lines of itsmatt's answer.

推荐答案

我做了一些ScrollToCaret进一步的实验,它只是没有在同一位置,每次结束。由于我的目标仅限于一路滚动至底部,它是那么发送WM_VSCROLL消息一个很好的候选人(277或0x115)的控制,SB_PAGEBOTTOM的wParam参数(7)。这一直滚动一路最底部酷似我需要:

I did some further experimentation with ScrollToCaret and it just does not end up in the same position every time. Since my goal is limited to only scrolling all the way to the bottom, it was then a good candidate for sending the WM_VSCROLL message (277, or 0x115) to the control, with wParam of SB_PAGEBOTTOM (7). This consistently scrolls all the way to the very bottom exactly like I needed:

[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern int SendMessage(IntPtr hWnd, int wMsg, IntPtr wParam, IntPtr lParam);
private const int WM_VSCROLL = 277;
private const int SB_PAGEBOTTOM = 7;

public static void ScrollToBottom(RichTextBox MyRichTextBox)
{
    SendMessage(MyRichTextBox.Handle, WM_VSCROLL, (IntPtr)SB_PAGEBOTTOM, IntPtr.Zero);
}

这篇关于不一致的结果与RichTextBox的ScrollToCaret的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆